# This need and IFDEF WINDOWS !!
LAZBUILD := $(shell command -v lazbuild)
OPTS := -B --bm=Release
OPTSQT5 := --ws=qt5
OPTSQT6 := --ws=qt6
LPI := heidisql.lpi

BIN := ./out/heidisql
BINWIN64 := ./out/win64/heidisql.exe
BINGTK := ./out/gtk2/heidisql
BINQT5 := ./out/qt5/heidisql
BINQT6 := ./out/qt6/heidisql
BINMACOS := ./out/macos/heidisql

# Make shell magic to get version from somewhere
#VERSION := shell magic

# Check for environment variable GITHUB
ifeq ($(origin GITHUB), undefined)
    # GITHUB is not set
	tag := $(shell git describe --tags $(shell git rev-list --tags --max-count=1) 2>/dev/null || echo "v0.0.0")
else
    # GITHUB is set
    $(info GITHUB is set)
endif

VERSION := $(shell echo $(tag) | sed "s/v//")

.PHONY: all clean copy-locale build-mo build-win64 run-win64 build-gtk2 run-gtk2 build-qt5 run-qt5 build-qt6 run-qt6 build-macos deb-package tar-gtk2 tar-qt5 tar-qt6

all: clean build-win64 build-gtk2 build-qt5 build-qt6 build-macos deb-package tar-gtk2 tar-qt5 tar-qt6

clean:
	@echo "=== Cleaning"
	@rm -rf ./bin/lib/x86_64-linux/*
	@rm -f ./out/win64/* ./out/gtk2/* ./out/qt5/* ./out/qt6/* ./out/macos/*
	@rm -rf ./deb ./rpm ./tar ./dist

copy-locale:
	@echo "=== Copying .mo from extra/locale to out/locale"
	@mkdir -p ./out/locale
	@cp -fv ./extra/locale/*.mo ./out/locale

build-win64:
	@echo "=== Building Win64"
	lazbuild $(OPTS) $(LPI)
	@mkdir -p ./out/win64
	@mv -v "$(BIN).exe" "$(BINWIN64)"

run-win64:
	@echo "Not implemented yet."

build-gtk2:
	@echo "=== Building GTK2"
	$(LAZBUILD) $(OPTS) $(LPI)
	@mkdir -p ./out/gtk2
	@mv -v $(BIN) $(BINGTK)

run-gtk2: build-gtk2
	@echo "=== Running GTK2"
	@mkdir -p ./run/locale
	@cp -vf ./extra/locale/*.mo ./run/locale
	@cp -vf ./extra/ini/*.ini ./run
	@cp -v $(BINGTK) ./run/heidisql
	@./run/heidisql

build-qt5:
	@echo "=== Building QT5"
	$(LAZBUILD) $(OPTS) $(OPTSQT5) $(LPI)
	@mkdir -p ./out/qt5
	@mv -v $(BIN) $(BINQT5)

run-qt5: build-qt5
	@echo "=== Running QT5"
	@mkdir -p ./run/locale
	@cp -vf ./extra/locale/*.mo ./run/locale
	@cp -vf ./extra/ini/*.ini ./run
	@cp -v $(BINQT5) ./run/heidisql
	@./run/heidisql

build-qt6:
	@echo "=== Building QT6"
	$(LAZBUILD) $(OPTS) $(OPTSQT6) $(LPI)
	@mkdir -p ./out/qt6
	@mv -v $(BIN) $(BINQT6)

run-qt6: build-qt6
	@echo "=== Running QT6"
	@mkdir -p ./run/locale
	@cp -vf ./extra/locale/*.mo ./run/locale
	@cp -vf ./extra/ini/*.ini ./run
	@cp -v $(BINQT6) ./run/heidisql
	@./run/heidisql

build-macos:
	@echo "=== Building macOS"
	$(LAZBUILD) $(OPTS) $(LPI)
	@mkdir -p ./out/macos
	@mv -v $(BIN) $(BINMACOS)

deb-package:
	@echo "=== Creating debian package"
	rm -vrf deb
	cp -R package-skeleton deb
	find deb -iname ".gitkeep" -exec rm -v {} +
	cp -vR extra/locale/*.mo deb/usr/share/heidisql/locale
	cp -v extra/ini/*.ini  deb/usr/share/heidisql
	cp -v res/deb-package-icon.png deb/usr/share/pixmaps/heidisql.png
	cp -v $(BINQT6) deb/usr/share/heidisql/heidisql
	chmod +x deb/usr/share/heidisql/heidisql
	cp -v README.md LICENSE deb/usr/share/doc/heidisql
	mkdir -p dist
	@sed "s/%VERSION%/$(VERSION)/" deb-control.txt > control.txt
	rm -vf dist/*.deb
	fpm -s dir -t deb -n heidisql -v $(VERSION) \
	  -p dist \
	  --verbose \
	  --deb-custom-control control.txt \
	  --deb-no-default-config-files \
	  ./deb/=/
	rm control.txt

rpm-package:
	@echo "=== Creating rpm package"
	rm -vrf rpm
	cp -R package-skeleton rpm
	find rpm -iname ".gitkeep" -exec rm -v {} +
	cp -vR extra/locale/*.mo rpm/usr/share/heidisql/locale
	cp -v extra/ini/*.ini  rpm/usr/share/heidisql
	cp -v res/deb-package-icon.png rpm/usr/share/pixmaps/heidisql.png
	cp -v $(BINQT6) rpm/usr/share/heidisql/heidisql
	chmod +x rpm/usr/share/heidisql/heidisql
	cp -v README.md LICENSE rpm/usr/share/doc/heidisql
	mkdir -p dist
	rm -vf dist/*.rpm

	fpm -s dir -t rpm -n heidisql -v $(VERSION) \
	  -p dist \
	  --verbose \
	  --rpm-os linux \
	  --description "HeidiSQL SQL client (Qt6)" \
	  --url "https://www.heidisql.com" \
	  --license "GPL-2.0-or-later" \
	  --depends "libQt6Widgets.so.6" \
	  --depends "libQt6Gui.so.6" \
	  --depends "libQt6Core.so.6" \
	  --depends "libQt6Pas.so.6" \
	  --depends "libssl.so.3" \
	  --depends "libmariadb.so.3" \
	  --depends "libpq.so.5" \
	  --depends "libsqlite3.so.0" \
	  --depends "libsybdb.so.5" \
	  ./rpm/=/

tar-gtk2:
	@echo "=== Creating GTK2 archive"
	rm -vrf tar
	mkdir -p tar/locale dist
	cp -v README.md LICENSE tar
	cp -v res/deb-package-icon.png tar/heidisql.png
	cp -v extra/locale/*.mo tar/locale
	cp -v extra/ini/*.ini tar
	cp -v out/gtk2/heidisql tar
	chmod +x tar/heidisql
	cd tar && tar -zcvf ../dist/build-gtk2-$(tag).tgz *

tar-qt5:
	@echo "=== Creating QT5 archive"
	rm -vrf tar
	mkdir -p tar/locale dist
	cp -v README.md LICENSE tar
	cp -v res/deb-package-icon.png tar/heidisql.png
	cp -v extra/locale/*.mo tar/locale
	cp -v extra/ini/*.ini tar
	cp -v out/qt5/heidisql tar
	chmod +x tar/heidisql
	cd tar && tar -zcvf ../dist/build-qt5-$(tag).tgz *

tar-qt6:
	@echo "=== Creating QT6 archive"
	rm -vrf tar
	mkdir -p tar/locale dist
	cp -v README.md LICENSE tar
	cp -v res/deb-package-icon.png tar/heidisql.png
	cp -v extra/locale/*.mo tar/locale
	cp -v extra/ini/*.ini tar
	cp -v out/qt6/heidisql tar
	chmod +x tar/heidisql
	cd tar && tar -zcvf ../dist/build-qt6-$(tag).tgz *
