# hoardom makefile
# supports: make, make install, make deb, make pkg, make clean

NAME        := hoardom
VERSION     := $(shell grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/')
DESCRIPTION := Domain hoarding made less painful
MAINTAINER  := crt

PREFIX      ?= /usr/local
BINDIR      := $(PREFIX)/bin
DATADIR     := $(PREFIX)/share
APPDIR      := $(DATADIR)/applications
ICONDIR     := $(DATADIR)/icons/hicolor/256x256/apps
ICON_FILE   := dist/AppIcon.ico

CARGO       ?= cargo
CARGO_FLAGS ?=

BUILDDIR    := target/release
BINARY      := $(BUILDDIR)/$(NAME)
GUI_BINARY  := $(BUILDDIR)/$(NAME)-app

# packaging scratch dirs
PKG_ROOT    := target/pkg-root
DEB_ROOT    := target/deb-root
MAC_ROOT    := target/mac-root
MAC_APP     := target/mac-root/$(NAME).app

# ---- build ----

.PHONY: all build release debug clean install uninstall deb pkg app

all: release

release:
	$(CARGO) build --release $(CARGO_FLAGS)

debug:
	$(CARGO) build $(CARGO_FLAGS)

# build the gui wrapper (requires gui feature)
app: release
	$(CARGO) build --release --features gui $(CARGO_FLAGS)

# ---- install (linux / mac) ----

install: release
	@echo "installing $(NAME) to $(DESTDIR)$(BINDIR)"
	install -d $(DESTDIR)$(BINDIR)
	install -m 755 $(BINARY) $(DESTDIR)$(BINDIR)/$(NAME)
	@# install gui wrapper too if it was built
	@if [ -f $(GUI_BINARY) ]; then \
		install -m 755 $(GUI_BINARY) $(DESTDIR)$(BINDIR)/$(NAME)-app; \
	fi
ifeq ($(shell uname), Darwin)
	@echo "installing macOS app bundle"
	install -d $(DESTDIR)/Applications
	$(MAKE) _mac_app APP_DEST=$(DESTDIR)/Applications
else
	@echo "installing desktop file"
	install -d $(DESTDIR)$(APPDIR)
	install -m 644 dist/$(NAME).desktop $(DESTDIR)$(APPDIR)/$(NAME).desktop
	install -d $(DESTDIR)$(ICONDIR)
	install -m 644 $(ICON_FILE) $(DESTDIR)$(ICONDIR)/$(NAME).ico
endif
	@echo "done, $(NAME) is ready to go"

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/$(NAME)
	rm -f $(DESTDIR)$(BINDIR)/$(NAME)-app
	rm -f $(DESTDIR)$(APPDIR)/$(NAME).desktop
	rm -f $(DESTDIR)$(ICONDIR)/$(NAME).ico
ifeq ($(shell uname), Darwin)
	rm -rf $(DESTDIR)/Applications/$(NAME).app
endif
	@echo "uninstalled"

# ---- debian .deb package ----

deb: release
	@echo "building deb package v$(VERSION)"
	rm -rf $(DEB_ROOT)

	# binary
	install -d $(DEB_ROOT)/usr/bin
	install -m 755 $(BINARY) $(DEB_ROOT)/usr/bin/$(NAME)

	# desktop file + icon
	install -d $(DEB_ROOT)/usr/share/applications
	install -m 644 dist/$(NAME).desktop $(DEB_ROOT)/usr/share/applications/$(NAME).desktop
	install -d $(DEB_ROOT)/usr/share/icons/hicolor/256x256/apps
	install -m 644 $(ICON_FILE) $(DEB_ROOT)/usr/share/icons/hicolor/256x256/apps/$(NAME).ico

	# control file
	install -d $(DEB_ROOT)/DEBIAN
	printf 'Package: $(NAME)\n\
Version: $(VERSION)\n\
Section: utils\n\
Priority: optional\n\
Architecture: $(shell dpkg --print-architecture 2>/dev/null || echo amd64)\n\
Maintainer: $(MAINTAINER)\n\
Description: $(DESCRIPTION)\n\
 TUI and CLI tool for searching domain availability across TLD lists.\n\
 Includes favorites, scratchpad, export, and custom list support.\n' > $(DEB_ROOT)/DEBIAN/control

	dpkg-deb --build --root-owner-group $(DEB_ROOT) target/$(NAME)_$(VERSION)_$(shell dpkg --print-architecture 2>/dev/null || echo amd64).deb
	@echo "deb built: target/$(NAME)_$(VERSION)_*.deb"

# ---- macOS .pkg package ----

pkg: app
	@echo "building macOS pkg v$(VERSION)"
	rm -rf $(PKG_ROOT) $(MAC_ROOT)

	# cli binary package
	install -d $(PKG_ROOT)/cli$(PREFIX)/bin
	install -m 755 $(BINARY) $(PKG_ROOT)/cli$(PREFIX)/bin/$(NAME)

	pkgbuild \
		--root $(PKG_ROOT)/cli \
		--identifier ch.teleco.$(NAME).cli \
		--version $(VERSION) \
		--install-location / \
		target/$(NAME)-cli.pkg

	# app bundle package
	install -d $(PKG_ROOT)/app
	$(MAKE) _mac_app APP_DEST=$(PKG_ROOT)/app

	pkgbuild \
		--component $(PKG_ROOT)/app/$(NAME).app \
		--identifier ch.teleco.$(NAME).app \
		--version $(VERSION) \
		--install-location /Applications \
		target/$(NAME)-app.pkg

	# distribution xml so the installer actually has permission to write to /Applications
	printf '<?xml version="1.0" encoding="utf-8"?>\n\
<installer-gui-script minSpecVersion="1">\n\
    <title>$(NAME) $(VERSION)</title>\n\
    <options customize="never" require-scripts="false"/>\n\
    <domains enable_localSystem="true"/>\n\
    <choices-outline>\n\
        <line choice="cli"/>\n\
        <line choice="app"/>\n\
    </choices-outline>\n\
    <choice id="cli" title="CLI Tool">\n\
        <pkg-ref id="ch.teleco.$(NAME).cli"/>\n\
    </choice>\n\
    <choice id="app" title="App Bundle">\n\
        <pkg-ref id="ch.teleco.$(NAME).app"/>\n\
    </choice>\n\
    <pkg-ref id="ch.teleco.$(NAME).cli" version="$(VERSION)" installKBytes="$(shell echo $$(( $$(stat -f%z $(BINARY)) / 1024 )))">#$(NAME)-cli.pkg</pkg-ref>\n\
    <pkg-ref id="ch.teleco.$(NAME).app" version="$(VERSION)" installKBytes="$(shell echo $$(( $$(stat -f%z $(BINARY)) / 1024 )))">#$(NAME)-app.pkg</pkg-ref>\n\
</installer-gui-script>\n' > $(PKG_ROOT)/distribution.xml

	productbuild \
		--distribution $(PKG_ROOT)/distribution.xml \
		--package-path target \
		target/$(NAME)-$(VERSION).pkg
	rm -f target/$(NAME)-cli.pkg target/$(NAME)-app.pkg
	rm -rf $(PKG_ROOT)
	@echo "pkg built: target/$(NAME)-$(VERSION).pkg"

# ---- internal: macOS .app bundle ----

_mac_app:
	@test -n "$(APP_DEST)" || (echo "APP_DEST not set" && exit 1)
	install -d $(APP_DEST)/$(NAME).app/Contents/MacOS
	install -d $(APP_DEST)/$(NAME).app/Contents/Resources

	# gui wrapper as the executable (or shell launcher as fallback)
	@if [ -f $(GUI_BINARY) ]; then \
		echo "using native gui wrapper"; \
		install -m 755 $(GUI_BINARY) $(APP_DEST)/$(NAME).app/Contents/MacOS/$(NAME)-app; \
	else \
		echo "gui wrapper not built, using shell launcher fallback"; \
		install -m 755 dist/mac-launcher.sh $(APP_DEST)/$(NAME).app/Contents/MacOS/$(NAME)-app; \
	fi

	# the actual tui binary (gui wrapper spawns this, shell launcher also needs it)
	install -m 755 $(BINARY) $(APP_DEST)/$(NAME).app/Contents/MacOS/$(NAME)

	# Info.plist
	printf '<?xml version="1.0" encoding="UTF-8"?>\n\
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n\
<plist version="1.0">\n\
<dict>\n\
    <key>CFBundleExecutable</key>\n\
    <string>$(NAME)-app</string>\n\
    <key>CFBundleIdentifier</key>\n\
    <string>ch.teleco.$(NAME)</string>\n\
    <key>CFBundleName</key>\n\
    <string>$(NAME)</string>\n\
    <key>CFBundleDisplayName</key>\n\
    <string>hoardom</string>\n\
    <key>CFBundleVersion</key>\n\
    <string>$(VERSION)</string>\n\
    <key>CFBundleShortVersionString</key>\n\
    <string>$(VERSION)</string>\n\
    <key>CFBundlePackageType</key>\n\
    <string>APPL</string>\n\
    <key>CFBundleIconFile</key>\n\
    <string>icon.icns</string>\n\
    <key>LSMinimumSystemVersion</key>\n\
    <string>10.12</string>\n\
    <key>NSHighResolutionCapable</key>\n\
    <true/>\n\
</dict>\n\
</plist>\n' > $(APP_DEST)/$(NAME).app/Contents/Info.plist

	# app icon
	cp dist/AppIcon.icns $(APP_DEST)/$(NAME).app/Contents/Resources/icon.icns

# ---- clean ----

clean:
	$(CARGO) clean
	rm -rf $(PKG_ROOT) $(DEB_ROOT) $(MAC_ROOT)
	rm -f target/$(NAME)_*.deb target/$(NAME)-*.pkg
