#!/bin/sh

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/usr/bin/polipo
test -x $DAEMON || exit 0

CONFIG_FILE=/etc/polipo/config
FORBIDDEN_FILE=/etc/polipo/forbidden

NAME=polipo

PIDFILE=/var/run/$NAME/$NAME.pid
LOGFILE=/var/log/$NAME/$NAME.log
USER=proxy
GROUP=proxy
DAEMON_OPTS="-c $CONFIG_FILE pidFile=$PIDFILE daemonise=true logFile=$LOGFILE"

offlineMode() {
        # At the moment this only depends on the presence of a default route
        # Eventually this will be configurable via $OPTION_FILE
        if /sbin/route -n | grep  ^0.0.0.0 > /dev/null; then
                # We have a default route - go online
                echo -n false
        else
                # We don't have a default route - go offline
                echo -n true
        fi
}

forbiddenFile() {
        if [ -f $FORBIDDEN_FILE ] || [ -d $FORBIDDEN_FILE ]; then
                echo -n $FORBIDDEN_FILE
        else
                echo -n /dev/null
        fi
}

waitForPIDRemove() {
        T=30
        for i in `seq 1 $T`; do
                if [ ! -f $PIDFILE ]; then return; fi
                sleep 1
        done
        echo "Waited $T seconds and $PIDFILE did not disappear.  Giving up." 2>&1
}

# Outputs the binary name of the process with PID $1, e.g. /usr/sbin/polipo
nameOfPID() {
	ps -o command= -p $1 | cut -f1 '-d '
}

# Returns true if the pidfile exists, and there is a polipo process running
# with the PID therein.  False otherwise.
alreadyRunning() {
	if [ -r $PIDFILE ]; then
		PID=`cat $PIDFILE`
		if [ "`nameOfPID $PID`" = $DAEMON ]; then
			return 0 # TRUE
		fi
	fi
	return 1 # FALSE
}

# Deletes polipo's pidfile if it exists
# This is designed to remove the pidfile from a crashed polipo before
# starting a new one
removePIDFile() {
	if [ -e $PIDFILE ]; then
		echo -n "Removing stale pidfile"
		rm $PIDFILE
		echo "."
	fi
}

DAEMON_OPTS="$DAEMON_OPTS forbiddenFile=`forbiddenFile`"

polipo_start() {
        DAEMON_OPTS="$DAEMON_OPTS proxyOffline=`offlineMode`"

	if alreadyRunning; then
		echo "$DAEMON already running -- doing nothing"
		exit
	fi	

	removePIDFile

        start-stop-daemon --start --quiet --pidfile $PIDFILE \
                --chuid $USER:$GROUP --exec $DAEMON -- $DAEMON_OPTS
}

polipo_stop() {
        start-stop-daemon --stop --quiet --pidfile $PIDFILE \
                --oknodo --exec $DAEMON
}

polipo_go_online() {
        DAEMON_OPTS="$DAEMON_OPTS proxyOffline=false"

        test ! -f $PIDFILE && return

        start-stop-daemon --stop --quiet --pidfile \
                 $PIDFILE --oknodo --exec $DAEMON
        waitForPIDRemove
        start-stop-daemon --start --quiet --pidfile $PIDFILE \
                 --chuid $USER:$GROUP --exec $DAEMON -- $DAEMON_OPTS
}

polipo_go_offline() {
        DAEMON_OPTS="$DAEMON_OPTS proxyOffline=true"

        test ! -f $PIDFILE && return

        start-stop-daemon --stop --quiet --pidfile \
                 $PIDFILE --oknodo --exec $DAEMON
        waitForPIDRemove
        start-stop-daemon --start --quiet --pidfile $PIDFILE \
                 --chuid $USER:$GROUP --exec $DAEMON -- $DAEMON_OPTS
}

case "$1" in
  start)
        polipo_start
        ;;
  stop)
        polipo_stop
        ;;
  restart)
        polipo_stop
        waitForPIDRemove
        polipo_start
        ;;
  go-offline)
        polipo_go_offline
        ;;
  go-online)
        polipo_go_online
        ;;
  *)
        N=polipo-control
        echo "Usage: $N {start|stop|go-offline|go-online}" >&2
        exit 1         
esac

exit 0
