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

PROG=gridfire
BOX_OS_FLAVOR="$$(cat /etc/hostname)"
OVERLAY=/var/local/share/genkernel/overlay/
PYVER=3
ROLE=gpgkey

TMPDIR=/mnt/tmp/Pentoo19/pyi
build=${TMPDIR}/build/build-$(BOX_OS_FLAVOR)
dist=dist/dist-$(BOX_OS_FLAVOR)

BINS=\
    gridfire \
    gridfire.bash \
    truecrypt-console.bash \
    veracrypt-console.bash \
    truecrypt.bash \
    veracrypt.bash \
    veracrypt-create.bash \
    keepassxc-cli.bash \
    pinentry_gridfire.bash \
    pinentry3.bash \
    pyassuan_password.bash \
    pyassuan_keepassxc-cli.bash \
    gridfire_ansible-vault.py \
    gridfire_ansible-vault.bash \
    gridfire_keepassxc-cli.bash \
    gridfire_keepassxc.bash \
    keyring_gridfire.bash \
    gridfire_kpsh.bash \
    gridfire_openssl.bash \
    gridfire_secret-tool.bash \
    gridfire_staticgpg.bash \
    gridfire_tomb.bash \
    gridfire_truecrypt.bash \
    gridfire_veracrypt.bash \
    gridfire_truecrypt-console.bash \
    gridfire_veracrypt-console.bash \
    pinentry_gridfire.py \
    gpgkey_pinentry_test.exp

refresh:: install_bins

install:: $(dist)/$(PROG).pex
	sh pykeepass.bash
	python3.sh -c 'from keyring_keyutils import backend' || \
	  python-keyring-keyutils.bash
	cd keepassxc_cmd2 && env PWD=${PWD}/keepassxc_cmd2 $(MAKE) $(FLAGS) $@
#	sh pyassuan.bash
	[ -f /usr/local/bin/get-info.py -a /usr/local/bin/pinentry.py -a \
	-e /usr/local/lib/python3.8/site-packages/pyassuan-0.2-py3.8.egg-info ] || \
	( cd /usr/local/src/gridfire/TremilyUsPyassuan && \
		python3.sh setup.py install --prefix=/usr/local ) \
		 2>&1|tee install.log
	$(MAKE) $(MFLAGS) overlay

update:: install install_bins pyi # cxFreeze  pex

$(PROG).spec.Linux::
	[ -f $(PROG).spec ] || pyi-makespec ./$(PROG).py -F -c
	[ -f $(PROG).spec.Linux ] || cp -p $(PROG).spec $(PROG).spec.Linux 

$(PROG).spec.Msys:: $(PROG).spec.Linux
	grep -q $$PWD $(PROG).spec || sed -e "s@pathex=.'.*'.@pathex=['$$PWD']@" \
		< $(PROG).spec.Linux > $(PROG).spec.Msys

pyi::  $(dist)/$(PROG).pyi
$(dist)/$(PROG).pyi::
	[ -d $(build) ] || mkdir -p $(build)
	[ -d $(dist)  ] || mkdir -p $(dist)
	[ -e $(dist)/$(PROG).pyi -a $(dist)/$(PROG).pyi -nt ./$(PROG).py ] || \
		 pyinstaller --distpath $(dist) --workpath $(build) \
		--exclude tkinter --exclude matplotlib  --exclude ctypes \
		--exclude twisted --exclude jedi  --exclude jaraco \
		--exclude sphinx --exclude coverage  --exclude nose \
		--exclude PIL --exclude numpy --exclude OpenGL \
		--exclude PySide2 --exclude PyQt5 --exclude IPython \
			--onefile -c --ascii \
			--name $(PROG)_pyi-$(BOX_OS_FLAVOR) $(PROG).py \
			>> $(dist)/$(PROG).log 2>&1
	[ ! -f  $(dist)/$(PROG).log ] || ! grep ERROR: $(dist)/$(PROG).log

overlay::
	[ -x $(OVERLAY)/bin/$(PROG) -a $(OVERLAY)/bin/$(PROG) -nt $(dist)/$(PROG) ] || \
	  cp -p $(dist)/$(PROG) $(OVERLAY)/bin/$(PROG)
	[ -x $(OVERLAY)/bin/keyctl -a $(OVERLAY)/bin/keyctl -nt /bin/keyctl ] || \
	  cp -p /bin/keyctl $(OVERLAY)/bin/keyctl

dont_install::
	[ -f ../../bin/$(PROG).sh -a ../../bin/$(PROG).sh -nt ./bin/$(PROG).sh ] || \
	    cp -p ./bin/$(PROG).sh ../../bin/$(PROG).sh

install_bins:: install bins
	$(MAKE) $(MFLAGS) -C keepassxc_cmd2 $@

bins::
	for elt in ${BINS} ; do \
		[ -f ../../bin/$$elt -a ../../bin/$$elt -nt bin/$$elt ] || \
		  {  cp -p bin/$$elt ../../bin/$$elt ; \
			echo bin/$$elt ; \
			chmod 775 ../../bin/$$elt ; } ; \
		done
	[ ! -x dist/dist-$(BOX_OS_FLAVOR)/gridfire_pyi-$(BOX_OS_FLAVOR) ] || \
	  [ -x $(dist)/$(PROG) -a -f ../../bin/$(PROG) -a ../../bin/$(PROG) -nt $(dist)/$(PROG) ] || \
	    cp -p dist/dist-$(BOX_OS_FLAVOR)/gridfire_pyi-$(BOX_OS_FLAVOR) /usr/local/bin/gridfire3.pyi
# $(dist)/$(PROG) ../../bin/$(PROG)
	[ ! -x dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex ] || \
	  [ -x dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex -a -f ../../bin/$(PROG) -a ../../bin/$(PROG) -nt dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex ] || \
	    cp -p dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex /usr/local/bin/gridfire.pex
# $(dist)/$(PROG) ../../bin/$(PROG)
	cp -p TremilyUsPyassuan/bin/pinentry.py ../../bin/pinentry_.py
	[ -d /boot ] || cp -pi bin/gridfire.rc.win ../../bin/gridfire.rc
	[ -d /boot ] && cp -ip bin/gridfire.rc.lin ../../bin/gridfire.rc

pex:: # keepassxc_cmd2/keepassxc_cmd2.pex
	$(MAKE) $(MFLAGS) -f Makefile.$@
	$(MAKE) $(MFLAGS) -C keepassxc_cmd2 $@

test:: # pex
	$(MAKE) $(FLAGS) -C keepassxc_cmd2 $@
	[ ! -f dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex ] || \
	{	echo 0 | dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex test ; \
		echo INFO tested dist/dist-$(BOX_OS_FLAVOR)/gridfire.pex ; }
	sh pykeepass.bash test
	$(MAKE) $(FLAGS) doctest

DOC_TESTS=tomb3.txt # keyutils3.txt keyring3.txt pykeepass3.txt keyctl3.txt
doctest::
	for file in $(DOC_TESTS) ; do \
	    /var/local/bin/testforge_python_doctest3.bash \
		doc/txt/$$file ; \
	    done
	/var/local/share/doc/txt/pykeepass3.bash

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

down:: clean
	( cd /o/data/TestForge/src/ansible ; \
	  rsync -vax --exclude \*.pyc --exclude __pycache__ --exclude \*~ \
		     --exclude _Old  --exclude build --exclude dist \
		--delete /usr/local/src/gridfire/ \
		roles/gpgkey/o*/L*/usr/local/src/gridfire/ )
up::
	( rsync -vax --exclude \*.pyc --exclude __pycache__ --exclude \*~ \
		     --exclude _Old  --exclude build --exclude dist \
		--exclude /usr/local/src/gridfire/dist \
		--exclude /usr/local/src/gridfire/build \
		--delete /usr/local/src/gridfire/ \
		/q/Pg64/Msys64/usr/local/src/gridfire/ )

