#!/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=toxcore
DIR=c-$PKG
GIT_HUB=github.com
GIT_USER=TokTok
GIT_DIR=$DIR
GIT_BRAN=master
VERS=2.18.0

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

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

    WD=$PWD
    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"
	    ols_are_we_connected || { DEBUG not connected ; exit 0 ; }
	    cd  "$PREFIX/net/Git/$GIT_HUB/$GIT_USER"
	    git clone  -b $GIT_BRAN --depth=1 https://$GIT_HUB/$GIT_USER/$GIT_DIR || exit 4
	    git clone --depth=1 https://$GIT_HUB/$GIT_USER/dockerfiles
	    cd $WD
	    #  wget -xcP ../net/Http/ https://github.com/TokTok/c-toxcore/releases/download/v0.2.18/c-toxcore-0.2.18.tar.gz
	fi
	cp -rip "$PREFIX/net/Git/$GIT_HUB/$GIT_USER"/$GIT_DIR $DIR
    fi

    cd "$DIR" || exit 5

    [ -f third_party/cmp/Makefile ] || git submodule update --init || exit 6

#    ols_apply_testforge_patches
#    # [ -f CMakeLists.txt.dst ] || patch -b -z.dst < toxcore.diff || exit 7

    [ -f cmake.sh ] || cat > cmake.sh << EOF
#!/bin/sh
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

PREFIX=$PREFIX
ROLE=$ROLE

CORE=$PREFIX/src/c-toxcore
DIR=_build
LIB=\$CORE/\$DIR

cd \$CORE | exit 3

mkdir _build
cd _build
cmake  \
    -DCMAKE_BUILD_TYPE="Debug" \
  -DCMAKE_UNITY_BUILD=ON \
  -DMIN_LOGGER_LEVEL=TRACE \
  -DMUST_BUILD_TOXAV=ON \
  -DNON_HERMETIC_TESTS=ON \
  -DSTRICT_ABI=ON \
  -DTEST_TIMEOUT_SECONDS=120 \
  -DUSE_IPV6=OFF \
  -DAUTOTEST=ON \
  -DBUILD_MISC_TESTS=ON \
  -DBUILD_FUN_UTILS=ON \
  -DBOOTSTRAP_DAEMON=ON \
  .. > cmake.log 2>&1
#sed -e 's/-DNDEBUG/-g/' -i CMakeCache.txt
make .. > make.log 2>&1

ls \$LIB/*so* || { echo ERROR \$LIB ; exit 2 ; }

EOF
    bash cmake.sh || {
	retval=$?
	ERROR cmake $retval
	exit 3$retval
    }
    cd _build
    make >> make.log 2>&1 || {
	retval=$?
	ERROR cmake $retval
	exit 3$retval
    }
    
    cp -p other/bootstrap_daemon/tox-bootstrapd $PREFIX/bin
    cp -p other/bootstrap_daemon/tox-bootstrapd.sh $PREFIX/etc/init.d/tox-bootstrapd
#    ln -s $PREFIX/etc/init.d/tox-bootstrapd /etc/init.d
    exit 0

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

    [ ! -d $DIR/_build ] && WARN not built yet $DIR && exit 11
    [ -f $DIR/_build/libtoxcore.so.${VERS} ] && WARN not compiled yet $DIR && exit 12
    ldd $DIR/_build/libtoxcore.so.${VERS} | grep found && ERROR ldd fails $DIR && exit 13
    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
