# Lilt from source.
#
#   make                           build (Rust 1.80+ / cargo, a C compiler, pkg-config, ALSA headers)
#   sudo make install              install to /usr/local (PREFIX=/usr for the system path)
#   make install PREFIX=~/.local   install just for you
#   sudo make uninstall            remove what install put there
#
# Build dependencies:
#   Arch:           sudo pacman -S --needed rust base-devel alsa-lib pkgconf
#   Debian/Ubuntu:  sudo apt install cargo build-essential pkg-config libasound2-dev
#   Fedora:         sudo dnf install cargo gcc make pkgconf-pkg-config alsa-lib-devel
#
# A copy built this way doesn't update itself; get a newer source and `make install` again
# (or build with MANAGED=0 to have it update from nitaimaarek.com like the prebuilt ones).

PREFIX  ?= /usr/local
DESTDIR ?=
CARGO   ?= cargo
MANAGED ?= 1

BIN     := target/release/lilt
VERSION := $(shell sed -n 's/^version *= *"\(.*\)"/\1/p' Cargo.toml | head -1)
SIZES   := 32 48 64 128 256 512
BINDIR  := $(DESTDIR)$(PREFIX)/bin
SHARE   := $(DESTDIR)$(PREFIX)/share
TMP     := .lilt-install

.PHONY: all build install uninstall clean run

all: build

build:
	LILT_MANAGED=$(MANAGED) $(CARGO) build --release --locked

run: build
	$(BIN) --portable

install:
	@test -x $(BIN) || { echo "Run 'make' first (as yourself, not root)."; exit 1; }
	mkdir -p $(TMP)
	install -Dm755 $(BIN) $(BINDIR)/lilt
	install -Dm644 packaging/linux/lilt.desktop $(SHARE)/applications/lilt.desktop
	sed -e 's/@VERSION@/$(VERSION)/' -e "s/@DATE@/$$(date -u +%Y-%m-%d)/" \
		packaging/linux/com.nitaimaarek.Lilt.metainfo.xml > $(TMP)/metainfo.xml
	install -Dm644 $(TMP)/metainfo.xml $(SHARE)/metainfo/com.nitaimaarek.Lilt.metainfo.xml
	for s in $(SIZES); do \
		$(BIN) --export-icon $$s $(TMP)/$$s.png && \
		install -Dm644 $(TMP)/$$s.png $(SHARE)/icons/hicolor/$${s}x$${s}/apps/lilt.png || exit 1; \
	done
	rm -r $(TMP)
	-[ -n "$(DESTDIR)" ] || update-desktop-database -q $(SHARE)/applications 2>/dev/null
	-[ -n "$(DESTDIR)" ] || gtk-update-icon-cache -q -t -f $(SHARE)/icons/hicolor 2>/dev/null
	@echo "Lilt $(VERSION) installed to $(BINDIR)/lilt"

uninstall:
	rm -f $(BINDIR)/lilt $(SHARE)/applications/lilt.desktop $(SHARE)/metainfo/com.nitaimaarek.Lilt.metainfo.xml
	for s in $(SIZES); do rm -f $(SHARE)/icons/hicolor/$${s}x$${s}/apps/lilt.png; done
	-update-desktop-database -q $(SHARE)/applications 2>/dev/null

clean:
	$(CARGO) clean
