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

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=toxcore
[ -n "$PYDEV_VAR_LOCAL" ] && PREFIX=$PYDEV_VAR_LOCAL

DESC=""
. /usr/local/src/usr_local_src.bash || exit 1
HTTP_DIR=$PREFIX/net/Http

DIR=tinfoilhat.shmoo.com
URL=web.archive.org/web/20121116091222/http:/

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

if [ $# -eq 0 ] ; then
    if [ ! -d $DIR ] ; then
	route|grep -q ^default  || exit 0
	mkdir $DIR $DIR/source
	wget -cP  $DIR/source http://$URL/$DIR/source/bb-random.c \
	     http://$URL/$DIR/source/gpggrid-version-on-floppy.c \
	     http://$URL/$DIR/source/gpggrid.c || exit 3    
    fi

    cd $PREFIX/src/$DIR/source || exit 4

    [ -x gpggrid ] || \
	cc -o gpggrid --static gpggrid.c || exit 5

    [ -f staticgpggrid.c ] || \
	sed -e 's/"gpg"/"staticgpg"/' gpggrid.c > staticgpggrid.c
    [ -x staticgpggrid ] || \
	cc -o staticgpggrid --static staticgpggrid.c || exit 6

    [ -x $PREFIX/bin/gpggrid -a $PREFIX/bin/gpggrid -nt gpggrid ] || \
	cp -p gpggrid $PREFIX/bin/  || exit 7

    [ -x $PREFIX/bin/staticgpggrid -a $PREFIX/bin/staticgpggrid -nt gpggrid ] || \
	cp -p staticgpggrid $PREFIX/bin/ || exit 8

    OPREFIX=$PREFIX/share/genkernel/overlay
    [ -d $OPREFIX/bin ] || mkdir $OPREFIX/bin
    [ -x $OPREFIX/bin/staticgpggrid ] || \
	ln $OPREFIX/bin/staticgpggrid $OPREFIX/bin/ || exit 9
fi

exit 0
