#!/bin/sh
# -*- mode: sh; tab-width: 8; encoding: 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

DESC=""
. $PREFIX/bin/usr_local_tput.bash || exit 1

PKG=negotiator
DIR=negatiator

declare -a FILES
FILES=(
    1a/dd/3fcb75aebaa0a28f4f4e4a5773833d5cc7fecd47f2b535fc7e445f289539/negotiator-common-0.12.2.tar.gz
    65/e5/bddc148f12aa8e81cfb0fbe504541436d0d38c6cb1546fa4fb5fbefcb5ce/negotiator-host-0.12.2.tar.gz
    fe/a1/3d77020b8c5034f7ef65434d0510d1262840550155ce5f433c8189499326/negotiator-guest-0.12.2.tar.gz
)
declare -a GUESTS
GUESTS=(
    coloredlogs-15.0.1-py2.py3-none-any.whl
    executor-23.2-py2.py3-none-any.whl
    fasteners-0.19-py3-none-any.whl
    humanfriendly-10.0-py2.py3-none-any.whl
    negotiator_common-0.12.2-py3-none-any.whl
    negotiator_guest-0.12.2-py3-none-any.whl
    property_manager-3.0-py2.py3-none-any.whl
    setuptools-69.0.2-py3-none-any.whl
    six-1.16.0-py2.py3-none-any.whl
    supervisor-4.2.5-py2.py3-none-any.whl
    verboselogs-1.7-py2.py3-none-any.whl
)

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

if [ "$#" -eq 0 ] ; then

    WD=$PWD
    if true || [ !  -d "$DIR" ] ; then
	route | grep -q ^def || { DBUG not connected ; exit 0 ; }
	wget -xcP $PREFIX/net/Http/ https://pypi.org/project/negotiator-common/
	for f in "${FILES[@]}" ; do
	    wget -xcP $PREFIX/net/Http/ https://files.pythonhosted.org/packages/$f
	done
	if [ ! -d "$WD/$DIR" ] ; then
	    [ -d $WD/$DIR ] || mkdir $WD/$DIR
	    pip3.sh download -d  $WD/$DIR \
		    negotiator-guest supervisor humanfriendly
	    cd $WD
	fi
	for f in "${FILES[@]}" ; do
	    tar xvfkz  $PREFIX/net/Http/$f 2>/dev/null
	done

    fi
    for f in "${FILES[@]}" ; do
	base=`basename $f .tar.gz`
	[ -d base ] && continue
	tar xvfkz  $PREFIX/net/Http/$f 2>/dev/null
	cd $base
	pip3.sh install --prefix=/usr/local . >> install.log 2>&1 || \
	    WARN problems installing $base retval=$retval
	cd ..
    done

    exit 0

elif [ "$1" = 'test' ] ; then # 3*
    cd $PREFIX/src/$DIR/_build || exit 30
    ctest || exit 31
    
elif [ "$1" = 'refresh' ] ; then # 6*

    cd $PREFIX/src/$DIR || exit 60
	/usr/local/sbin/base_diff_from_dst.bash $ROLE || exit 6$?

elif [ "$1" = 'update' ] ; then # 7*
    ols_are_we_connected || exit 0
    cd $PREFIX/src/$DIR || exit 70
    git pull || exit 7$?
fi

