#!/bin/bash

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

cd /opt/td-config/scripts

if [ "$#" == "0" ]; then
    gksudo -D /usr/share/applications/acc-config.desktop 'python tcc-config.py --control appliance'
else
        if [ "$1" == 'true' ]; then
                gksudo -D /usr/share/applications/acc-config.desktop 'python tcc-config.py --control appliance --upgrade=true'
        elif [ "$1" == 'false' ]; then
                gksudo -D /usr/share/applications/acc-config.desktop 'python tcc-config.py --control appliance --upgrade=false'
	elif [ "$1" == '-d' -o "$1" == '--desync' ]; then
		if [ $USER != 'root' ]; then
			echo "Root user privileges required."
			exit 0
		fi 

		echo ""
                echo "Network Injector (Appliance Control Center)"
                echo ""
                echo "Desync the Network Injector from current server..."
		rm -f /opt/td-config/share/server 2> /dev/null
		rm -f /opt/td-config/share/port 2> /dev/null
		rm -f /opt/td-config/share/key 2> /dev/null
		rm -f /opt/td-config/share/cookie 2> /dev/null
		echo "Please synchronize the Network Injector with new server."
		echo ""
        elif [ "$1" == '-l' -o "$1" == '--log' ]; then
                echo ""
                echo "Network Injector (Appliance Control Center)"
		echo ""
                echo "Log:"
		echo ""
                tail -f /var/log/syslog | grep RCS
	 elif [ "$1" == '-s' -o "$1" == '--show-logs' ]; then
                echo ""
                echo "Network Injector (Appliance Control Center)"
                echo ""
                echo "The log directory is: /var/log/td-config/"
                echo ""
                echo "Current logs:"
                echo ""
                ls -l /var/log/td-config/
	elif [ "$1" == '-r' -o "$1" == '--report' ]; then
		if [ $USER != 'root' ]; then
                        echo "Root user privileges required."
			exit 0
                fi

		echo ""
                echo "Network Injector (Appliance Control Center)"
                echo ""
		echo "Build a complete report, please wait..." 
		cd $HOME
		rm -f appliance_report.tar.gz 2> /dev/null
		rm -Rf appliance_report/ 2> /dev/null
		mkdir appliance_report/
		cp -Rf /opt/td-config/ appliance_report/
		rm -f appliance_report/td-config/share/wordlist
		cp -Rf /var/log/td-config/* appliance_report/ 2> /dev/null
		tar -cvzf appliance_report.tar.gz appliance_report/ > /dev/null
		rm -Rf appliance_report/
		echo "Please send $HOME/appliance_report.tar.gz to support."
		echo ""
        elif [ "$1" == '-v' -o "$1" == '--version' ]; then
                echo ""
                echo "Network Injector (Appliance Control Center)"
		echo ""
                echo -n "Current system version: "
                cat /opt/td-config/VERSION | awk '{print $1}'
                echo -n "Current core version:   "
                cat /opt/td-config/VERSION | awk '{print $2}'
                echo ""
        else
                echo ""
                echo "Network Injector (Appliance Control Center)"
		echo ""
                echo "Usage: appliance [options]"
		echo ""
		echo "  -d, --desync         Desync from current server"
                echo "  -l, --log            Show running log"
		echo "  -s, --show-logs      Show all logs"
		echo "  -r, --report         Build a complete report for support"
                echo "  -v, --version        Show current version"
                echo "  -h, --help           Show this help"
                echo ""
        fi
fi
