#!/bin/sh
# -*- mode: sh; tab-width: 8; encoding: utf-8-unix -*-

prog=`basename $0`
PREFIX=/usr/local
## . /usr/local/etc/testforge/testforge.bash
ROLE=gpgkey

PYVER=3
P="BASE_PYTHON${PYVER}_MINOR"
PYTHON_MINOR="$(eval echo \$$P)"
PYTHON_EXE_MSYS=$PREFIX/bin/python$PYVER.sh
PYTHON_EXE=$PYTHON_EXE_MSYS
. /var/local/src/var_local_src.bash || exit 2

MOD="pykeepass"
PKG="pykeepass"
DIR="${PKG}"
VER=3.2.0

GIT_HUB=github.com
GIT_USER="libkeepass"
GIT_DIR="$DIR"

ols_funtoo_requires dev-python/connect dev-python/argon2_cffi dev-python/future dev-python/pycryptodome dev-python/future

cd $PREFIX/src/gridfire || exit 2
WD=$PWD

if [ !  -d "$DIR" ] ; then
    if [ ! -d "$PREFIX/net/Git/$GIT_HUB/$GIT_USER/$GIT_DIR" ] ; then
	[ -d "$PREFIX/net/Git/$GIT_HUB/$GIT_USER" ] || \
		mkdir "$PREFIX/net/Git/$GIT_HUB/$GIT_USER"
	route | grep -q '^default' || { DEBUG not connected ; exit 0 ; }
	(cd "$PREFIX/net/Git/$GIT_HUB/$GIT_USER" && \
		git clone --depth=1 "https://$GIT_HUB/$GIT_USER/$GIT_DIR" ) ||\
		exit 2
	fi
    cp -rip "$PREFIX/net/Git/$GIT_HUB/$GIT_USER/$GIT_DIR" . || \
    exit 3
fi

cd "$DIR" || exit 4

if [ ! -f requirements.txt.dst ] ; then
    mv requirements.txt requirements.txt.dst
    sed -e 's/=.*//' >requirements.txt requirements.txt.dst
fi
if [ ! -f README.rst.dst ] ; then
    mv README.rst README.rst.dst
    sed -e 's/  from pykeepass import PyKeePass/  >>> from pykeepass import PyKeePass/' >README.rst README.rst.dst
fi

ELTS="
lxml==4.3.5
pycryptodome==3.9.4
construct==2.9.51
argon2-cffi==19.2.0
python-dateutil==2.8.0
future==0.17.1
"
ELTS=""

ols_setup_zip_unsafe

ls $PREFIX/$LIB/python${PYTHON_MINOR}/site-packages/${DIR}-${VER}-py${PYTHON_MINOR}.egg >/dev/null || \
  [ -d $PREFIX/$LIB/python${PYTHON_MINOR}/site-packages/${DIR}-${VER}-py${PYTHON_MINOR}.egg ] || \
    $PYTHON_EXE_MSYS setup.py 2>&1 || { ERROR "code $?" ; cat install$PYVER.log  ; exit 6 ; }

"$PYTHON_EXE_MSYS" -c "import $MOD" 2>/dev/null || exit 10

[ "$#" -eq 0 ] && exit 0

if [ "$1" = 'test' ] ; then
#    $PYTHON_EXE_MSYS -m doctest -f -o FAIL_FAST README.rst
    # Ran 83 tests in 17.777s
    $PYTHON_EXE_MSYS tests/tests.py || exit 11
fi
