#!/bin/sh /etc/rc.common

START=95
STOP=10

PIDFILE=/var/run/privoxy.pid
CFGFILE=/var/etc/privoxy.conf
CFGTEMP=/var/etc/privoxy.conf.tmp

_uci2conf() {
	# redefined callback for options when calling config_load
	config_cb() {
		if [ ."$2" != ."privoxy" ]; then
			option_cb() { return 0; }
		else
			option_cb()
			{
				# $1	name of variable
				# $2	value
				local __OPT="$1"
				local __VAL="$2"
				case $__OPT in
					confdir|templdir|temporary_directory|logdir|logfile)
						# needs to be handled separately because we need to set permissions
						# AND needs to be defined first because of a BUG inside privoxy
						# require directories to be defined first inside config 
						;;
					debug_*)
						[ $__VAL -eq 0 ] && return	# not set ignore
						echo -e "debug\t$(echo $__OPT | sed -e 's#debug_##g')" >> $CFGTEMP ;;
					*)
						# detect list options (LENGTH) and ignore
						echo $__OPT | grep -i "_LENGTH" >/dev/null 2>&1 && return
						# detect list options (ITEM) and ignore
						echo $__OPT | grep -i "_ITEM" >/dev/null 2>&1 && __OPT=$(echo $__OPT | sed -e "s#_ITEM.*##g")
						# uci only accept "_" but we need "-"
						local __OPT=$(echo $__OPT | sed -e "s#_#-#g")
						# write to config
						echo -e "$__OPT\t$__VAL" >> $CFGTEMP
						;;
				esac
			}

			list_cb()
			{
				option_cb "$@"
			}
		fi
	}

	# temporary config file
	# privoxy need read access
	mkdir -m0755 -p /var/etc
	echo "" > $CFGTEMP
	chmod 644 $CFGTEMP
	chgrp privoxy $CFGTEMP

	echo '### AUTO-GENERATED CONFIGURATION'	   >> $CFGTEMP
	echo '### USED BY PRIVOXY'		   >> $CFGTEMP
	echo '### DO NOT EDIT'			   >> $CFGTEMP
	echo '### SEE /etc/config/privoxy INSTEAD' >> $CFGTEMP
	echo ''					   >> $CFGTEMP

	# logdir and logfile
	# privoxy needs read/write access
	_LOGDIR=$(uci -q get privoxy.privoxy.logdir)   || _LOGDIR="/var/log"
	_LOGFILE=$(uci -q get privoxy.privoxy.logfile) || _LOGFILE="privoxy.log"
	mkdir -m0755 -p $_LOGDIR
	touch $_LOGDIR/$_LOGFILE
	chmod 664 $_LOGDIR/$_LOGFILE
	chown privoxy:privoxy $_LOGDIR/$_LOGFILE
	echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
	echo -e "logfile\t$_LOGFILE" >> $CFGTEMP

	# confdir
	# privoxy needs read access (possibly write access)
	_CONFDIR=$(uci -q get privoxy.privoxy.confdir) || _CONFDIR="/etc/privoxy"
	chmod 755 $_CONFDIR
	chmod 664 $_CONFDIR/*
	chgrp privoxy $_CONFDIR $_CONFDIR/*
	echo -e "confdir\t$_CONFDIR" >> $CFGTEMP

	# templdir
	# privoxy need read access
	_TEMPLDIR=$(uci -q get privoxy.privoxy.templdir)	# no default needed
	if [ -z "$_TEMPLDIR" ]; then
		chmod 755 $_CONFDIR/templates
		chmod 644 $_CONFDIR/templates/*
		chgrp privoxy $_CONFDIR/templates $_CONFDIR/templates/*
	else
		chmod 755 $_TEMPLDIR
		chmod 644 $_TEMPLDIR/*
		chgrp privoxy $_TEMPLDIR $_TEMPLDIR/*
		echo -e "templdir\t$_TEMPLDIR" >> $CFGTEMP
	fi

	# temporary-directory
	# privoxy needs read/write access
	_TMP_DIR=$(uci -q get privoxy.privoxy.temporary_directory)	# no default needed
	if [ -n "$_TMP_DIR" ]; then
		mkdir -m0750 -p $_TMP_DIR
		chown privoxy:privoxy $_TMP_DIR
		echo -e "temporary-directory\t$_TMP_DIR" >> $CFGTEMP
	fi

	config_load "privoxy"	# calling above option_cb() and write the rest into $CFGTEMP

	# move temp to final privoxy readable configuration
	mv -f $CFGTEMP $CFGFILE
	return 0
}

boot() {
	# wait a given time (default 10 seconds) before startup
	# to wait for interfaces to come up / not using hotplug events during boot
	_start() {
		[ $1 -gt 0 ] && {
			logger -p daemon.info -t "privoxy[]" "Scheduled startup in $1 seconds"
			sleep $1
		}
		start
	}

	local _DELAY
	_DELAY=$(uci_get "privoxy" "system" "boot_delay" "10")
	_start $_DELAY &
	return 0
}

shutdown() {
	rm -f /tmp/privoxy.hotplug
	stop
}

start() {
	# if already running do nothing
	local _PID=$(cat $PIDFILE 2>/dev/null)
	kill -1 $_PID 2>/dev/null && return 0

	_uci2conf
	/usr/sbin/privoxy --pidfile $PIDFILE --user privoxy.privoxy $CFGFILE
	touch /tmp/privoxy.hotplug

	# verify startup
	_PID=$(cat $PIDFILE 2>/dev/null)
	kill -1 $_PID 2>/dev/null
	local _ERR=$?
	[ $_ERR -eq 0 ] \
		&& logger -p daemon.notice -t "privoxy[$_PID]" "Started successfully"\
		|| logger -p daemon.warn -t "privoxy[]" "Failed to start"
	return $_ERR
}

reload() {
	# reload is also used by luci-app-privoxy
	local _PID=$(cat $PIDFILE 2>/dev/null)
	kill -1 $_PID 2>/dev/null
	if [ $? -eq 0 ]; then
		# only restart if already running
		restart
	else
		# only start if enabled
		enabled && start
	fi
	return 0
}

stop() {
	local _PID=$(cat $PIDFILE 2>/dev/null)
	kill -15 $_PID 2>/dev/null
	sleep 1			# give time to shutdown
	local _tmp=$(pgrep /usr/sbin/privoxy | tr "\n" " ")
	if [ -z "$_tmp" ]; then
		logger -p daemon.notice -t "privoxy[$_PID]" "Shutdown successfully"
	else
		kill -9 $_tmp	# Normally never come here
		logger -p daemon.warn -t "privoxy[$_tmp]" "Shutdown forced by KILL"
	fi
	return 0
}
