#!/usr/bin/bash
# The "#!/usr/bin/env bash" would depend on PATH, so it is not used here. 
#==========================================================================
# Initial author of this file: Martin.Vahi@softf1.com
# This file is in public domain.
#
# Initial author of the template of this file: Martin.Vahi@softf1.com
# The following line is a spdx.org license label line:
# SPDX-License-Identifier: 0BSD
#==========================================================================
CMD_GREP="/home/mmmv/applications/GNU_grep/v_3_11/bin/grep"
CMD_KILL="/usr/bin/kill"
CMD_NICE="/usr/bin/nice"
CMD_PRINTF="/usr/bin/printf"
CMD_PS="/usr/bin/ps"
CMD_SED="/home/mmmv/applications/GNU_sed/v_4_9/bin/sed" # Linux sed (GNU sed) and BSD sed differ
CMD_SLEEP="/usr/bin/sleep"
CMD_SYNC="/usr/bin/sync"
CMD_UNAME="/usr/bin/uname"
CMD_XARGS="/usr/bin/xargs"
CMD_TOR="/home/mmmv/applications/Tor/v_0_4_8_15/bin/tor"
#--------------------------------------------------------------------------
func_throw(){
    #----------------------------------------------------------------------
    local S_CMD_OR_SOME_OTHER_TEXT="$1"
    local S_GUID="$2"
    #----------------------------------------------------------------------
    echo ""
    echo -e "\e[31mMissing\e[39m"
    echo -e "    $S_CMD_OR_SOME_OTHER_TEXT"
    echo "GUID=='$S_GUID'"
    echo ""
    exit 1
    #----------------------------------------------------------------------
} # func_throw
#--------------------------------------------------------------------------
func_kill_tor(){
    local SI_SIGNAL="$1"
    local S_PID="`$CMD_PS -A | $CMD_GREP -E '[ ]tor\$' | $CMD_SED -e 's/^[[:blank:]]//g' | $CMD_SED -e 's/[[:blank:]].*\$//g'`"
    if [ "$S_PID" != "" ]; then
        $CMD_SYNC
        wait
        $CMD_XARGS $CMD_KILL -s $SI_SIGNAL $S_PID  &
        # The "&" at the previous line allows 
        #     "kill -s 9 $S_PID "  
        # to be called if the graceful exit takes too long.
    fi
    $CMD_SYNC
    wait
} # func_kill_tor
#--------------------------------------------------------------------------
HOME="/home/www1" # so that cron also can run this script
S_FP_WWW1_TORRC="$HOME/m_local/etc/torrc_veebiserver01_www1.conf"
if [ -e "$CMD_GREP" ]; then
if [ -e "$CMD_UNAME" ]; then
if [ "`$CMD_UNAME -a | $CMD_GREP -i linux `" != "" ]; then
if [ -e "$S_FP_WWW1_TORRC" ]; then
    if [ -e "$CMD_KILL" ]; then
    if [ -e "$CMD_NICE" ]; then
    if [ -e "$CMD_PRINTF" ]; then
    if [ -e "$CMD_PS" ]; then
    if [ -e "$CMD_SED" ]; then
    if [ -e "$CMD_SLEEP" ]; then
    if [ -e "$CMD_SYNC" ]; then
    if [ -e "$CMD_XARGS" ]; then
    if [ -e "$CMD_TOR" ]; then
    #----------------------------------------------------------------------
        $CMD_SYNC
        wait
        $CMD_SLEEP 4
        # /usr/bin/killall --signal 15 tor # graceful exit
        func_kill_tor "15"
        $CMD_SLEEP 3
        # /usr/bin/killall --signal  9 tor # kill process
        func_kill_tor "9"
        $CMD_PRINTF "" > /opt/partitions/RAM_20MiB/www1_tor.log
        $CMD_SLEEP 5
        $CMD_NICE -n 8 $CMD_TOR -f $S_FP_WWW1_TORRC  &
    #----------------------------------------------------------------------
    else func_throw "$CMD_TOR" "75703615-2ece-4c51-bd19-f103107049e7"
    fi
    else func_throw "$CMD_XARGS" "3e909297-3878-44bc-9049-f103107049e7"
    fi
    else func_throw "$CMD_SYNC" "d442efba-e492-481c-b719-f103107049e7"
    fi
    else func_throw "$CMD_SLEEP" "e1275270-1cc4-451c-b919-f103107049e7"
    fi
    else func_throw "$CMD_SED" "a3c302d3-a06b-42ae-9999-f103107049e7"
    fi
    else func_throw "$CMD_PS" "f48cb1e7-8af2-4540-8219-f103107049e7"
    fi
    else func_throw "$CMD_PRINTF" "45eac015-f205-4ebc-8339-f103107049e7"
    fi
    else func_throw "$CMD_NICE" "2e3ad5b4-7fa8-459f-be39-f103107049e7"
    fi
    else func_throw "$CMD_KILL" "52c84e88-0100-41f5-8f19-f103107049e7"
    fi
else func_throw "$S_FP_WWW1_TORRC" "565d9e95-069f-4f5a-a359-f103107049e7"
fi
else func_throw "Linux operating system. This script has been customised to Linux." "aac14291-3590-43fe-8169-f103107049e7"
fi
else func_throw "$CMD_UNAME" "543d8720-ac15-4ea4-bb29-f103107049e7"
fi
else func_throw "$CMD_GREP" "5bac7db4-3efe-476d-bf28-f103107049e7"
fi

#==========================================================================
# S_VERSION_OF_THIS_FILE="91f301c5-ef7f-4894-a818-f103107049e7"
#==========================================================================
