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

#See /var/local/src/ZeroNet.bash

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=toxcore

PYVER=3
P="BASE_PYTHON${PYVER}_MINOR"
[ -z "$PYTHON_MINOR" ] && PYTHON_MINOR="$(eval echo \$$P)"
PYTHON_EXE_MSYS=$PREFIX/bin/python$PYTHON_MINOR.sh
PYTHON_EXE=$PYTHON_EXE_MSYS
PYTHON_PIP_MSYS=$PREFIX/bin/pip$PYTHON_MINOR.sh

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

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

#ols_funtoo_requires

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

if [ "$#" -eq 0 ] ; then
    
    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 ^def || { DEBUG not connected ; exit 0 ; }
	    (cd "$PREFIX/net/Git/$GIT_HUB" && \
		 git clone --depth=1 "http://http-git.tremily.us/pyassuan.git" ) ||\
		exit 2
	fi
	cp -rip "$PREFIX/net/Git/$GIT_HUB/$GIT_DIR" . || \
	    exit 3
    fi

    cd "$DIR" || exit 4

    #    ols_setup_zip_unsafe 's@^    )@     zip_safe=False)@'

    #? [ -e /var/local/src/var_local_local.bash ] && . /var/local/src/var_local_local.bash

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

    #    msys_python_bins $BINS

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

    exit 0

elif [ $1 = 'check' ] ; then # 1*
    "$PYTHON_EXE_MSYS" -c "import $MOD" 2>/dev/null || exit 20
    #    ols_test_bins
    exit $?

elif [ "$1" = 'test' ] ; then # 3*
    cd $WD/$DIR
    $PYTHON_EXE_MSYS -m unittest discover >>test.log || exit 31$?
fi
