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

START=80
USE_PROCD=1

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

_uci2conf() {
	local _LOGDIR="/var/log"	# set default
	local _LOGFILE="privoxy.log"	# set default

	# redefined callback for options when calling config_load
	option_cb()
	{
		# $1	name of variable
		# $2	value
		local __OPT="$1"
		local __VAL="$2"
		case $__OPT in
			logdir)		# logdir handled later
				_LOGDIR="$__VAL"  ;;
			logfile)	# logfile handled later
				_LOGFILE="$__VAL" ;;
			*)
				# 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")
				# filter debug_*
				echo $__OPT | grep -i "debug_" >/dev/null 2>&1 && {
					[ $__VAL -eq 0 ] && return	# not set ignore
					__VAL=$(echo $__OPT | sed -e "s#debug_##g")
					__OPT="debug"
				}
				# uci only accept "_" but we need "-"
				local __OPT=$(echo $__OPT | sed -e "s#_#-#g")
				# write to config
				echo -e "$__OPT\t$__VAL" >> $CFGTEMP
				;;
		esac
	}

	mkdir -m0755 -p /var/etc
	echo "" > $CFGTEMP	# create tmp config file
	chmod 644 $CFGTEMP	# garantee that privoxy can read
	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

	config_load privoxy	# calling above option_cb()

	# write logdir/logfile to config
	echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
	echo -e "logfile\t$_LOGFILE" >> $CFGTEMP

	# create logfile and set permissions
	touch $_LOGDIR/$_LOGFILE
	chmod 664 $_LOGDIR/$_LOGFILE
	chown privoxy:privoxy $_LOGDIR/$_LOGFILE

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

# privoxy should auto-reload it's configuration
# but it only reload on next connect to one of the listen_address
# if we create a new listen_address privoxy never reload
reload_service() {
	# so we restart here because rc.common reload_service only start without stopping
	restart "$@"

	# the following should normally work but see above
#	_uci2conf	# convert uci config
}

service_triggers() {
	procd_add_reload_trigger "privoxy"
}

start_service() {
	# redefined callback for sections when calling config_load
	config_cb() {
		# $1	type of config section
		# $2	name of section
		[ "$1" = "interface" ] && \
			procd_add_interface_trigger interface.* $2 /etc/init.d/privoxy restart
	}

	_uci2conf	# convert uci config

	procd_open_instance

	procd_set_param command /usr/sbin/privoxy
	procd_append_param command --no-daemon			# for procd run in foreground
	procd_append_param command --pidfile $PIDFILE		# set pid file
	procd_append_param command --user privoxy.privoxy	# set user
	procd_append_param command $CFGFILE			# config file

	procd_set_param file $CFGFILE	# set configration file

        procd_open_trigger	# we need a restart on interface events not a reload
	config_load network	# load network configuration and set trigger(s) in config_cb() above
        procd_close_trigger

	procd_close_instance
}

service_running() {
	logger_trick() {
		sleep 1		# give privoxy time to completely come up
		logger -p daemon.notice -t "privoxy[$(cat $PIDFILE)]" "Service started successfully"
	}
	logger_trick &
}

stop_service() {
	logger -p daemon.notice -t "privoxy[$(cat $PIDFILE)]" "Service shutdown"
}
