#!/bin/sh
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

prog=`basename $0 .bash`
PREFIX=/usr/local
[ -f /usr/local/etc/testforge/testforge.bash ] && \
  . /usr/local/etc/testforge/testforge.bash
ROLE=toxcore

. /usr/local/src/usr_local_src.bash || exit 2
x
PKG="kernel-expect"

GIT_HUB=github.com
GIT_USER="perkint"
GIT_DIR="kernelexpect"
DIR="${GIT_DIR}"

BINS=$PKG

ols_funtoo_requires dev-perl/Expect dev-perl/File-Which

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

if [ "$#" -eq 0 ] ; then
    
    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"
	    ip 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" $DIR || exit 3
    fi

    if [ ! -f $PREFIX/bin/$PKG ] ; then
	cp -p $DIR/$PKG $PREFIX/bin/$PKG
    fi

    if [ ! -f $PREFIX/bin/$PKG.bash ] ; then
	echo -e "#!/bin/sh\n# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-\nROLE=$ROLE\nexec $PREFIX/bin/$PKG \"\$@\"" > $PREFIX/bin/$PKG.bash
	chmod 755 $PREFIX/bin/$PKG.bash
    fi

    [ -d "$HTTP_DIR/$GIT_HUB/$GIT_USER/$GIT_DIR/" ] || \
	ols_are_we_connected || exit 0
    ols_wget_xc -P $HTTP_DIR/ \
		https://$GIT_HUB/$GIT_USER/$GIT_DIR/

elif [ $1 = 'check' ] ; then # 1*
    ols_test_bins && exit 0 || exit 1$?

if [ $1 = 'test' ] ; then # 3*
    $PREFIX/bin/$PKG.bash --help
  fi
