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

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

# https://security.stackexchange.com/questions/46197/force-a-specific-ssl-cipher
# https://code.google.com/p/chromium/issues/detail?id=58831

DIR=testssl.sh
GITHUB_USER=drwetter
GITHUB_DIR=$DIR

. $PREFIX/src/var_local_src.bash

BINS=testssl

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

if [ "$#" -eq 0 ]  ; then
    [ -d $DIR ] || git clone --depth=1 https://github.com/$GITHUB_USER/$DIR

    for elt in $BINS ; do
	file=$PREFIX/bin/$elt.bash
	if [ ! -f $file ] ; then
	    cat > $file << EOF
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-
cd $PREFIX/src/$DIR
exec bash testssl.sh "\$@"
EOF
	    chmod +x $PREFIX/bin/testssl.bash
	fi
    done

    exit 0

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

elif [ $1 = 'lint' ] ; then # 2*
    /var/local/bin/pydev_shellcheck.bash testssl.sh/testssl.sh || exit 2$?
    
elif [ "$1" = 'test' ] ; then # 3*
    for bin in $BINS ; do
	$PREFIX/bin/$bin.bash --help >/dev/null || exit 3$?
    done
    
elif [ "$1" = 'update' ] ; then # 7*
    ols_are_we_connected || exit 0
    cd $PREFIX/src/$DIR || exit 70
    git pull || exit 7$?

    #error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly before end of the underlying stream
    #error: 1970 bytes of body are still expected
    #fetch-pack: unexpected disconnect while reading sideband packet
    #fatal: early EOF
    #fatal: fetch-pack: invalid index-pack output

fi

# wget -P https://testssl.sh/testssl.sh

exit 0
