APP     := ears
VERSION := 1.0.0
OUT     := bin/linux/installer
DIST    := dist
STAGE   := $(HOME)/.cache/$(APP)-build
PKGDIR  := $(STAGE)/$(APP)_$(VERSION)_all

SRC     := $(filter-out Form1.cs Form2.cs, $(wildcard *.cs))
GLADE   := $(wildcard *.glade)
ASSETS  := $(wildcard map sound)

.ONESHELL:
.SHELLFLAGS := -ec
.PHONY: all run clean deb init-packaging list

all: $(OUT)/program.exe

$(OUT)/program.exe: $(SRC) $(GLADE)
	mkdir -p $(OUT)
	mcs -target:winexe -pkg:gtk-sharp-3.0 \
	    $(foreach f,$(GLADE),-resource:$(f),$(notdir $(f))) \
	    $(SRC) -out:$@
run: all
	mono $(OUT)/program.exe

deb: all
	rm -rf "$(PKGDIR)"
	install -Dm755 packaging/launcher.sh    "$(PKGDIR)/usr/bin/$(APP)"
	install -Dm644 $(OUT)/program.exe       "$(PKGDIR)/usr/lib/$(APP)/program.exe"
	install -Dm644 packaging/app.desktop    "$(PKGDIR)/usr/share/applications/$(APP).desktop"
	install -Dm644 packaging/udev.rules     "$(PKGDIR)/lib/udev/rules.d/99-$(APP).rules"
	install -Dm644 packaging/control        "$(PKGDIR)/DEBIAN/control"
	install -Dm755 packaging/postinst       "$(PKGDIR)/DEBIAN/postinst"
	install -Dm755 packaging/postrm         "$(PKGDIR)/DEBIAN/postrm"
	if [ -n "$(ASSETS)" ]; then cp -r $(ASSETS) "$(PKGDIR)/usr/lib/$(APP)/"; fi
	find "$(PKGDIR)" -type d -exec chmod 755 {} +
	find "$(PKGDIR)" -type f -exec chmod 644 {} +
	chmod 755 "$(PKGDIR)/usr/bin/$(APP)" \
	          "$(PKGDIR)/DEBIAN/postinst" "$(PKGDIR)/DEBIAN/postrm"
	sed -i 's/\r$$//' "$(PKGDIR)/usr/bin/$(APP)" \
	                  "$(PKGDIR)/DEBIAN/postinst" "$(PKGDIR)/DEBIAN/postrm"
	mkdir -p $(DIST)
	dpkg-deb --build --root-owner-group "$(PKGDIR)" $(DIST)/$(APP)_$(VERSION)_all.deb
	dpkg -c $(DIST)/$(APP)_$(VERSION)_all.deb

clean:
	rm -rf $(OUT) $(DIST) "$(STAGE)"

list:
	@grep -E '^[a-zA-Z_-]+:' Makefile | cut -d: -f1

init-packaging:
	mkdir -p packaging
	cat > packaging/launcher.sh <<-'EOF'
	#!/bin/sh
	cd /usr/lib/$(APP) || exit 1
	exec /usr/bin/mono ./program.exe "$$@"
	EOF
	cat > packaging/app.desktop <<-EOF
	[Desktop Entry]
	Type=Application
	Name=EARS
	Comment=Auscultation Training Software
	Exec=$(APP)
	Icon=$(APP)
	Terminal=false
	Categories=Utility;Electronics;
	EOF
	cat > packaging/control <<-EOF
	Package: $(APP)
	Version: $(VERSION)
	Section: utils
	Priority: optional
	Architecture: all
	Depends: mono-runtime (>= 6.0), gtk-sharp3, libgtk-3-0
	Maintainer: kazi <kazi.chiba-u.hp>
	Description: Arduino serial control tool
	 GTK# application that connects to the device over USB serial.
	EOF
	cat > packaging/udev.rules <<-'EOF'
	SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", TAG+="uaccess", ENV{ID_MM_DEVICE_IGNORE}="1"
	SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", TAG+="uaccess", ENV{ID_MM_DEVICE_IGNORE}="1"
	SUBSYSTEM=="tty", ATTRS{idVendor}=="303a", TAG+="uaccess", ENV{ID_MM_DEVICE_IGNORE}="1"
	SUBSYSTEM=="tty", ATTRS{idVendor}=="2341", TAG+="uaccess", ENV{ID_MM_DEVICE_IGNORE}="1"
	EOF
	cat > packaging/postinst <<-'EOF'
	#!/bin/sh
	set -e
	udevadm control --reload-rules || true
	udevadm trigger --subsystem-match=tty || true
	update-desktop-database -q /usr/share/applications || true
	exit 0
	EOF
	cat > packaging/postrm <<-'EOF'
	#!/bin/sh
	set -e
	udevadm control --reload-rules || true
	exit 0
	EOF
	chmod 755 packaging/launcher.sh packaging/postinst packaging/postrm
	echo "packaging/ created — edit control and udev.rules, then run: make deb"