#!/bin/sh

##
# Tor Service
##

. /etc/rc.common

StartService ()
{

    if [ -f /usr/local/bin/tor ]; then
        if pid=$(GetPID Tor); then
            return 0
        else
		ConsoleMessage "Starting Tor Service"
# Tentative
# Making sure it is not running (I know it is not a best approarch)
		killall tor
		/usr/local/bin/tor -f /usr/local/etc/tor/torrc --runasdaemon 1 --pidfile /var/run/Tor.pid &
	fi
    fi
}

StopService ()
{
    if pid=$(GetPID Tor); then
	ConsoleMessage "Stopping Tor Service"
	kill -TERM "${pid}"
# Just for sanity (sometimes necessary.)
	killall tor
    else
	ConsoleMessage "Tor Service not responding."
# Just for sanity (sometimes necessary.)	
	killall tor
    fi
}

RestartService () { StopService; StartService; }

RunService "$1"
