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

ROLE=proxy
[ -n "$prog" ] || prog= # no qa
prog=$( basename $0 .bash )
. /usr/local/bin/usr_local_tput.bash

USAGE="[config|start|stop|status|restart|test]"

SHARED_MNTS="o"

[ "$#" -eq 0 ] && set -- install

## proxy_workstation_fix_getty_timeout
proxy_workstation_fix_getty_timeout () {
    # fix_getty_timeout - wheres inittab
    grep -l '^Exec.*agetty -o' /lib/systemd/system/*service | while read file ; do
	[ -f $file.dst ] && continue
	cp -p $file $file.dst
	sed -e 's/agetty -o/agetty -t 120 -o/' -i $file
    done
    return 0
}


## proxy_workstation__shutup_verbosity
proxy_workstation_shutup_verbosity () {
    for file in /etc/issue* /etc/issue.d/* ; do
	[ -f $file ] || continue
	[ -s $file ] && cp /dev/null $file
    done
    return 0
}

## proxy_workstation_install_fstab
proxy_workstation_install_fstab () {
    # /etc/fstab
    options=noauto,rw,trans=virtio,version=9p2000.L,cache=none
    for elt in $SHARED_MNTS ; do
	[ -d /mnt/$elt ] || mkdir /mnt/$elt
	grep -q /mnt/$elt /etc/fstab && continue
	echo "$elt	/mnt/$elt	9p	$options	0	0" \
	     >> /etc/fstab
    done
    # root
    return 0
}

## proxy_workstation_install_gagent
proxy_workstation_install_gagent () {
    [ -d /etc/apt ]  && proxy_workstation_install_gagent_debian || return 1$?
    [ -d /etc/gentoo ]  && proxy_workstation_install_gagent_gentoo || return 2$?
    return 0
}

## proxy_workstation_install_gagent
proxy_workstation_install_gagent_gentoo () {
    [ -x /usr/bin/qemu-ga -a -x /etc/init.d/qemu-guest-agent ] || \
	emerge -vb app-emulation/qemu-guest-agent || return 1$?
    return 0
}

## proxy_workstation_install_gagent
proxy_workstation_install_gagent_debian () {
    [ -e /dev/virtio-ports/org.qemu.guest_agent.0 ] || {
	echo ERROR: /dev/virtio-ports/org.qemu.guest_agent.0 not found
	ERROR "check the host xml for <target type='virtio' name='org.qemu.guest_agent.0'/>"
	ERROR "or blame Pottyring's systemd"
	}
    [ -x /usr/sbin/qemu-ga ] && return 0

    # /mnt/shared/qemu-guest-agent_3.1+dfsg-8+deb10u8_amd64.deb
    if [ -f /var/cache/apt/archives/qemu-guest-agent_3.1+dfsg-8+deb10u8_amd64.deb ] ; then
	dpkg -i /var/cache/apt/archives/qemu-guest-agent_3.1+dfsg-8+deb10u8_amd64.deb
    fi

    # start guest-service - its failing on the device prerequisite
    systemctl is-enabled qemu-guest-agent || systemctl enable --now qemu-guest-agent
    false && \
    [ -f /lib/systemd/system/qemu-guest-agent.service ] && \
	[ ! -h /etc/systemd/system/multi-user.target/qemu-guest-agent.service ] && \
	ln -s /lib/systemd/system/qemu-guest-agent.service \
	   /etc/systemd/system/multi-user.target.wants
    return 0
}

## proxy_workstation_test
proxy_workstation_test () {
    service qemu-guest-agent status >/dev/null || return 1$?
    proxy_whonix_test ws || return 2$?
    return 0
}

## proxy_workstation_config
proxy_workstation_config () {
    proxy_whonix_guest_config || return 1$?
    proxy_guest_firewall_config || return 2$?

    proxy_ws_whonix_config ws || return 3$?

    variables_defaults
    [ -n "$GATEWAY_IP_HARDCODED" ] || GATEWAY_IP_HARDCODED="10.152.152.10"

    ## Control Port Filter Proxy Port
    [ -n "$CONTROL_PORT_FILTER_PROXY_PORT" ] || CONTROL_PORT_FILTER_PROXY_PORT="9051"
    [ -n "$SOCKS_PORT_TOR_DEFAULT" ] || SOCKS_PORT_TOR_DEFAULT="9050"

    ## Socks Ports for per application circuits.
    [ -n "$SOCKS_PORT_TB" ] || SOCKS_PORT_TB="9100"
    [ -n "$SOCKS_PORT_IRC" ] || SOCKS_PORT_IRC="9101"
    [ -n "$SOCKS_PORT_TORBIRDY" ] || SOCKS_PORT_TORBIRDY="9102"
    [ -n "$SOCKS_PORT_IM" ] || SOCKS_PORT_IM="9103"
    [ -n "$SOCKS_PORT_APT_GET" ] || SOCKS_PORT_APT_GET="9104"
    [ -n "$SOCKS_PORT_GPG" ] || SOCKS_PORT_GPG="9105"
    [ -n "$SOCKS_PORT_SSH" ] || SOCKS_PORT_SSH="9106"
    [ -n "$SOCKS_PORT_GIT" ] || SOCKS_PORT_GIT="9107"
    [ -n "$SOCKS_PORT_SDWDATE" ] || SOCKS_PORT_SDWDATE="9108"
    [ -n "$SOCKS_PORT_WGET" ] || SOCKS_PORT_WGET="9109"
    [ -n "$SOCKS_PORT_WHONIXCHECK" ] || SOCKS_PORT_WHONIXCHECK="9110"
    [ -n "$SOCKS_PORT_BITCOIN" ] || SOCKS_PORT_BITCOIN="9111"
    [ -n "$SOCKS_PORT_PRIVOXY" ] || SOCKS_PORT_PRIVOXY="9112"
    [ -n "$SOCKS_PORT_POLIPO" ] || SOCKS_PORT_POLIPO="9113"
    [ -n "$SOCKS_PORT_WHONIX_NEWS" ] || SOCKS_PORT_WHONIX_NEWS="9114"
    [ -n "$SOCKS_PORT_TBB_DOWNLOAD" ] || SOCKS_PORT_TBB_DOWNLOAD="9115"
    [ -n "$SOCKS_PORT_TBB_GPG" ] || SOCKS_PORT_TBB_GPG="9116"
    [ -n "$SOCKS_PORT_CURL" ] || SOCKS_PORT_CURL="9117"
    [ -n "$SOCKS_PORT_RSS" ] || SOCKS_PORT_RSS="9118"
    [ -n "$SOCKS_PORT_TORCHAT" ] || SOCKS_PORT_TORCHAT="9119"
    [ -n "$SOCKS_PORT_MIXMASTERUPDATE" ] || SOCKS_PORT_MIXMASTERUPDATE="9120"
    [ -n "$SOCKS_PORT_MIXMASTER" ] || SOCKS_PORT_MIXMASTER="9121"
    [ -n "$SOCKS_PORT_KDE" ] || SOCKS_PORT_KDE="9122"
    [ -n "$SOCKS_PORT_GNOME" ] || SOCKS_PORT_GNOME="9123"
    [ -n "$SOCKS_PORT_APTITUDE" ] || SOCKS_PORT_APTITUDE="9124"
    [ -n "$SOCKS_PORT_YUM" ] || SOCKS_PORT_YUM="9125"
    [ -n "$SOCKS_PORT_TBB_DEFAULT" ] || SOCKS_PORT_TBB_DEFAULT="9150"

    return 0
}

## proxy_workstation_start_bg
proxy_workstation_start_bg () { proxy_workstation_start $* ; }
## proxy_workstation_start
proxy_workstation_start () {
    local dire=ws

    proxy_workstation_config || return 1$?
    proxy_whonix_guest_start

    proxy_whonix_polipo_start $dire || \
	{ ret=$? ;echo ERROR: $prog polipo not started ret=$ret; return 4$ret ; }

    return 0
}

## proxy_workstation_stop
proxy_workstation_stop () {

    service qemu-guest-agent status >/dev/null \
	    && service qemu-guest-agent stop || return 2$?

    return 0
}

## proxy_workstation_install
proxy_workstation_install () {

    proxy_workstation_install_gagent
    proxy_workstation_fix_getty_timeout
    proxy_workstation_shutup_verbosity
    proxy_workstation_install_fstab

    return 0
}

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

  elif [ "$1" = config -o "$1" = install ] ; then
    proxy_workstation_install || return 3$?

  elif [ "$1" = verify -o "$1" = test ] ; then
    proxy_workstation_test || return 4$?

  elif [ "$1" = start_bg -o "$1" = start -o "$1" = stop ] ; then
    proxy_workstation_$1 || return 5$?

  else
    eval "$@"
    exit $?

  fi
