#!/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

NAME=polipo

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

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
}

polipo_start() {

	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
}

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

exit 0
