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

prog=$( basename $0 .bash )
PREFIX=/usr/local
ROLE=proxy

USAGE="host|to_tor|tor|from_tor|client|whonix|gateway|vda|nat|workstation|ping|ssl|status|test|refresh|update|up|down"

[ -f /usr/local/etc/testforge/testforge.bash ] && . /usr/local/etc/testforge/testforge.bash
[ $( id -u ) -eq 0 ] || { ERROR $prog should be run as root ; exit 1 ; }

. /usr/local/sbin/proxy_whonix_lib.bash || \
    { ERROR loading /usr/local/sbin/proxy_whonix_host_lib.bash ; exit 2; }
#. /usr/local/bin/proxy_ping_lib.bash || \
#    { ERROR loading /usr/local/bin/proxy_ping_lib.bash ; exit 2; }
. /usr/local/bin/usr_local_base.bash || exit 2
proxy_whonix_mode

## proxy_whonix_test_mode - proxy_whonix_host.bash test
proxy_whonix_test_mode () { DBUG proxy_whonix_test_mode $* ;
    if [ $MODE = tor -o $MODE = whonix -o $MODE = host ]; then
	/usr/local/sbin/proxy_whonix_host.bash test || return 1$?
    fi

    return 0
}

## proxy_whonix_status - proxy_ping_test.bash "$MODE"
proxy_whonix_status () { DBUG proxy_whonix_status $* ;
    $PREFIX/bin/proxy_ping_test.bash "$MODE" || return 1$?
    return 0
}

## proxy_whonix_refresh
proxy_whonix_refresh () { DBUG proxy_whonix_refresh $* ;
    local USAGE="python|pip"

    if [ "$#" -eq 0 ] ; then
	echo USAGE: $prog $USAGE
    elif [ "$1" = '-h'  ] || [ "$1" = '--help' ] ; then
	echo USAGE: $prog $USAGE or:
    elif [ "$1" = 'python' ] ; then
	/usr/local/bin/testforge_clean_usr_local_lib.bash
    elif [ "$1" = 'pip' ] ; then
	/usr/local/bin/base_pip_upgrade.bash -p 2 -i 0
	/usr/local/bin/base_pip_upgrade.bash -p 3 -i 0
	base_sheebang_after_pip.bash
    else
	:
    fi
}

UPGRADE_USAGE="python|pip"
## proxy_whonix_update - $UPGRADE_USAGE
proxy_whonix_update() {

    if [ "$#" -eq 0 ] ; then
	echo USAGE: $prog $UPGRADE_USAGE
    elif [ "$1" = '-h'  ] || [ "$1" = '--help' ] ; then
	echo USAGE: $prog $USAGE or:
    elif [ "$1" = 'python' ] ; then
	:
    elif [ "$1" = 'pip' ] ; then
	:
    else
	:
    fi
}

TEST_USAGE="ssl|ping|mode|libvirt|ga"
## proxy_whonix_test - test $TEST_USAGE
proxy_whonix_test () { DBUG proxy_whonix_test $* ;
    if [ "$#" -eq 0 ] ; then
	echo USAGE: $prog $TEST_USAGE
    elif [ "$1" = '-h'  ] || [ "$1" = '--help' ] ; then
	echo USAGE: $prog $USAGE or:
    elif [ "$1" = 'ssl' ] ; then
	$PREFIX/sbin/proxy_test_ssl.bash "$@" || exit 3$?
    elif [ "$1" = 'mode' ] ; then
	proxy_whonix_test_mode || exit 4$?
    elif [ "$1" = 'ping' ] ; then
	$PREFIX/sbin/proxy_ping_test.bash "$@" || exit 4$?
    elif [ "$1" = 'libvirt' ] ; then
	$PREFIX/bin/proxy_ping_lib.bash proxy_libvirt_test || exit 5$?
    elif [ "$1" = 'ga' ] ; then
	$PREFIX/bin/proxy_libvirt_ga_test.bash || exit 5$?
    else
	:
    fi
    return 0
}

WD=$PWD
if [ "$#" -eq 0 ] ; then
    echo USAGE: $prog $USAGE
    exit 0

  elif [ "$1" = '-h'  ] || [ "$1" = '--help' ] ; then
    echo USAGE: $prog $USAGE or:
    grep '^## ' $0 | sed -e 's/^## //'
    exit 0

  elif [ "$1" = client ] ; then
    shift

  elif [ "$1" = ws -o "$1" = 'workstation' -o "$1" = 'gateway' ] ; then
    [ $1 = ws ] && arg=workstation || arg=$1
    shift
    $PREFIX/sbin/proxy_whonix_guest_$arg.bash "$*" || exit 3$?

  elif [ 'to_tor' -o "$1" = 'tor' -o "$1" = 'from_tor' -o "$1" = 'whonix' ] ; then
    $PREFIX/sbin/proxy_whonix_host.bash "$@" || exit $?

  elif [ "$1" = host ] ; then
    shift
    $PREFIX/sbin/proxy_whonix_host.bash "$@" || exit $?

  elif [ "$1" = refresh -o  "$1" = update -o "$1" = 'test' -o "$1" = 'status' ] ; then
    arg=$1;shift
    proxy_whonix_$arg "$@"

  elif [ "$1" = 'down' -o "$1" = 'up' ] ; then
    arg=$1;shift
    proxy_whonix_$arg "$@"

  elif [ "$1" = hourly -o "$1" = 'refresh' ] ; then
    :

  else
    DBUG $prog $*
    eval "$@"
    exit $?

  fi

exit 0
