# -*-mode: makefile; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

PROG=keepassxc_cmd2
BOX_OS_FLAVOR=`cat /etc/hostname`
TMPDIR=/var/tmp

OVERLAY=/var/local/share/genkernel/overlay/
PYVER=3
PYMVER=38
PLAT=linux
# pex broken
# PYMVER=37
# PLAT=windows

build=build-$(BOX_OS_FLAVOR)
dist=dist-$(BOX_OS_FLAVOR)

refresh::

update:: install

install:: pex bins # pyi

pyi:: pyinstaller_$(PROG)-$(BOX_OS_FLAVOR)
pyinstaller_$(PROG)-$(BOX_OS_FLAVOR):: $(dist)_$(PROG)/$(PROG)
$(dist)_$(PROG)/$(PROG):: Makefile $(PWD)/keepassxc_cmd2.py
	[ -d $(build)_$(PROG) ] || mkdir $(build)_$(PROG)
	[ -d $(dist)_$(PROG)  ] || mkdir $(dist)_$(PROG)
	[ -f $(PROG).log -a \
	  -e $(dist)_$(PROG)/$(PROG) -a \
	  $(dist)_$(PROG)/$(PROG) -nt $(PWD)/keepassxc_cmd2.py ] || \
		python$(PYVER).bash -m PyInstaller --clean \
			--distpath $(dist)_$(PROG) --workpath $(build)_$(PROG) \
			--onefile -c --ascii \
			--name $(PROG)_pyi-$(BOX_OS_FLAVOR) \
			--additional-hooks-dir packaging/pyinstaller \
			--hidden-import wcwidth \
			--exclude-module macOS \
			$(PWD)/keepassxc_cmd2.py \
			> $(PROG).log 2>&1
	! grep ERROR: $(PROG).log
	[ -e $(dist)_$(PROG)/$(PROG) -a -e $(dist)_$(PROG)/$(PROG)_pyi-$(BOX_OS_FLAVOR) ] || \
		ln $(dist)_$(PROG)/$(PROG)_pyi-$(BOX_OS_FLAVOR) $(dist)_$(PROG)/$(PROG)

cxFreeze:: cxFreeze-$(BOX_OS_FLAVOR)
cxFreeze-$(BOX_OS_FLAVOR)::
	[ -e cxfreeze_main.py ] || ln -s ../cxfreeze_main.py
	[ -f $(PROG).spec ] || pyi-makespec ./$(PROG).py -F -c
	[ -f $(PROG).spec.Linux ] || cp -p $(PROG).spec $(PROG).spec.Linux
	[ -d cxFreeze-$(BOX_OS_FLAVOR) ] || mkdir cxFreeze-$(BOX_OS_FLAVOR)
	python3 ./cxfreeze_main.py -c \
	        --target-dir cxFreeze-$(BOX_OS_FLAVOR) $(PROG).py \
	        >cxFreeze-$(BOX_OS_FLAVOR).log 2>&1

# No bundling of so's - not a freezer
pex:: clean
	[ -d dist/dist-$(BOX_OS_FLAVOR)/ ] || mkdir -p dist/dist-$(BOX_OS_FLAVOR)
	[ -n "${MSYSTEM}" ] || $(MAKE) $(MFLAGS) dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex

dist/dist-Windows/$(PROG).pex:: Makefile $(PROG).py
	env PYTHONPATH=/usr/lib/python3/dist-packages:/usr/local/src \
	/usr/local/bin/pex3.bash -D $(PWD) -m $(PROG):main -o $@ \
		--no-index --ignore-errors  --inherit-path=prefer \
		--platform windows_x86_64-cp-27-cp27mu \
		--platform windows_x86_64-cp-37-cp37mu	
	zip -d $@ build-\*/\* dist-\*/\* \
		\*/.git/\* \*.log __pycache__/\* >/dev/null

# ERROR: No matching distribution found for wcwidth (from -r keepassxc_cmd2.reqs (line 1))
# 		-r $(PROG).reqs
dist/dist-Kick150154/$(PROG).pex:: Makefile $(PROG).py
	env PYTHONPATH=/usr/lib/python3/dist-packages:/usr/local/src \
	/usr/local/bin/pex3.bash -D $(PWD) -m $(PROG):main -o $@ \
		-f /usr/lib/python3/dist-packages/ \
		--no-index --ignore-errors  --inherit-path=prefer \
		--platform linux_x86_64-cp-27-cp27mu \
		--platform linux_x86_64-cp-37-cp37mu	
	zip -d $@ build-\*/\* dist-\*/\* \
		\*/.git/\* \*.log __pycache__/\* >/dev/null

dist/dist-pentoo/$(PROG).pex:: Makefile $(PROG).py
	env PYTHONPATH=/usr/lib/python3/dist-packages:/usr/local/src \
	/usr/local/bin/pex3.bash -D $(PWD) -m $(PROG):main -o $@ \
		--no-index --ignore-errors  --inherit-path=prefer \
		--platform linux_x86_64-cp-27-cp27mu \
		--platform linux_x86_64-cp-38-cp38mu
	zip -d $@ build-\*/\* dist-\*/\* \
		\*/.git/\* \*.log __pycache__/\* >/dev/null

dist/dist-devuan/$(PROG).pex:: Makefile $(PROG).py
	env PYTHONPATH=/usr/lib/python3/dist-packages:/usr/local/src \
	/usr/local/bin/pex3.bash -D $(PWD) -m $(PROG):main -o $@ \
		--no-index --ignore-errors  --inherit-path=prefer \
		--platform linux_x86_64-cp-39-cp39mu
	zip -d $@ build-\*/\* dist-\*/\* \
		\*/.git/\* \*.log __pycache__/\* >/dev/null

bins:: pex
	[ -f dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex -a \
	  ../../../bin/$(PROG).pex -nt dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex ] || \
		cp -p dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex ../../../bin

test::
# ./keepassxc_cmd2.py < *2 keepassxc_cmd2.cmd2
	[ -f ${TMPDIR}/test4.kdbx -o -f ${TMPDIR}/test4.key ] || \
		cp -p ../pykeepass/tests/*y ../pykeepass/tests/*x ${TMPDIR}
	/usr/local/bin/python3.sh $(PROG).py < keepassxc_kpy.cmd2
	/usr/local/bin/python3.sh $(PROG).py run_script keepassxc_cli.cmd2
	echo -n password | /usr/local/bin/python3.sh $(PROG).py \
		run_script keepassxc_stdin.cmd2
	[ ! -f dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex ] || \
	{	dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex < keepassxc_kpy.cmd2 ; \
		echo INFO: tested dist/dist-$(BOX_OS_FLAVOR)/$(PROG).pex ; }

clean::
	rm -f */*~ *~

up:: clean
	$(MAKE) $(MFLAGS) -C .. $@

down:: clean
	$(MAKE) $(MFLAGS) -C .. $@
