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

prog=`basename $0`
PREFIX=/usr/local
[ -f /usr/local/etc/testforge/testforge.bash ] && \
    . /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

MOD="pyassuan"
DIR="${MOD}"
BINS="get-info pinentry"

GIT_HUB=http-git.tremily.us
GIT_DIR=pyassuan

cd $PREFIX/src/gridfire || exit 2
WD=$PWD
# http://http-git.tremily.us/pyassuan.git

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

cd "$DIR" || exit 4

if [ ! -f setup.py.dst ] ; then
    cp -p setup.py setup.py.dst
    sed -e 's@^    )@     zip_safe=False)@' -i setup.py
fi

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

"$PYTHON_EXE_MSYS" -c "import $MOD" || exit 10

cd ..

for elt in ${BINS} ; do \
	[ -f ../../bin/$$elt -a ../../bin/$$elt -nt bin/$$elt ] || \
	  {  cp -p bin/$$elt ../../bin/$$elt ; \
		chmod 775 ../../bin/$$elt ; } ; \
	done

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

if [ "$1" = 'test' ] ; then
    cd $WD/DIR
    $PYTHON_EXE_MSYS -m unittest discover || exit 11
fi
