#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org
START=50

DAEMON=/usr/sbin/tinyproxy
CFGFILE=/var/etc/tinyproxy.conf

start() {
	config_load tinyproxy
	config_foreach start_proxy tinyproxy
}

stop() {
	start-stop-daemon -q -x "$DAEMON" -K
	rm -f $CFGFILE
}

start_proxy() {
	config_get_bool enable "$1" enable
	[ "$enable" -eq "1" ] || return 0

	echo '### AUTOGENERATED CONFIGURATION' > $CFGFILE
	echo '### DO NOT EDIT' >> $CFGFILE
	echo '### SEE /etc/config/tinyproxy INSTEAD' >> $CFGFILE
	echo '' >> $CFGFILE

	proxy_atom "$1" User >> $CFGFILE
	proxy_atom "$1" Group >> $CFGFILE
	proxy_atom "$1" Port 8888 >> $CFGFILE
	proxy_atom "$1" Listen >> $CFGFILE
	proxy_atom "$1" Bind >> $CFGFILE
	proxy_atom "$1" Timeout >> $CFGFILE

	proxy_string "$1" ErrorFile_400 "ErrorFile 400" >> $CFGFILE
	proxy_string "$1" ErrorFile_403 "ErrorFile 403" >> $CFGFILE
	proxy_string "$1" ErrorFile_404 "ErrorFile 404" >> $CFGFILE
	proxy_string "$1" ErrorFile_408 "ErrorFile 408" >> $CFGFILE
	proxy_string "$1" ErrorFile_503 "ErrorFile 503" >> $CFGFILE

	proxy_string "$1" DefaultErrorFile >> $CFGFILE
	proxy_string "$1" StatFile >> $CFGFILE
	proxy_string "$1" Logfile >> $CFGFILE

	proxy_flag "$1" SysLog >> $CFGFILE

	proxy_atom "$1" LogLevel >> $CFGFILE

	proxy_list "$1" XTinyproxy >> $CFGFILE

	proxy_atom "$1" MaxClients >> $CFGFILE
	proxy_atom "$1" MinSpareServers >> $CFGFILE
	proxy_atom "$1" MaxSpareServers >> $CFGFILE
	proxy_atom "$1" StartServers >> $CFGFILE
	proxy_atom "$1" MaxRequestsPerChild >> $CFGFILE
	proxy_atom "$1" Allow >> $CFGFILE

	proxy_string "$1" ViaProxyName >> $CFGFILE
	proxy_string "$1" Filter >> $CFGFILE

	proxy_flag "$1" FilterURLs >> $CFGFILE
	proxy_flag "$1" FilterExtended >> $CFGFILE
	proxy_flag "$1" FilterCaseSensitive >> $CFGFILE
	proxy_flag "$1" FilterDefaultDeny Yes No >> $CFGFILE

	proxy_list "$1" Anonymous '"' >> $CFGFILE
	proxy_list "$1" ConnectPort >> $CFGFILE

	config_foreach write_upstream upstream

	start-stop-daemon -q -x "$DAEMON" -S -- -c "$CFGFILE"
}

write_upstream() {
	local type
	local via
	local target

	config_get "type" "$1" "type"
	config_get via "$1" via
	config_get target "$1" target
	[ -n "$target" ] && target=' "'"$target"'"'

	[ "$type" == "proxy" -a -n "$via" ] && \
		echo "upstream $via$target" >> $CFGFILE

	[ "$type" == "reject" -a -n "$target" ] && \
		echo "no upstream$target" >> $CFGFILE
}

proxy_atom() {
	local SECTION=$1
	local OPTION=$2
	local DEFAULT=$3

	config_get _value "$SECTION" "$OPTION"
	[ -z "$_value" ] && _value="$DEFAULT"
	[ -n "$_value" ] && echo "$OPTION $_value"
}

proxy_string() {
	local SECTION=$1
	local OPTION=$2
	local ALIAS=$3

	config_get _value "$SECTION" "$OPTION"
	[ -n "$_value" ] && echo "${ALIAS:-${OPTION}} "'"'"$_value"'"'
}

proxy_flag() {
	local SECTION=$1
	local OPTION=$2
	local TRUE="${3:-On}"
	local FALSE="${4:-Off}"

	config_get_bool _value "$SECTION" "$OPTION"
	[ "$_value" -eq "1" ] && _value="$TRUE" || _value="$FALSE"
	echo "$OPTION $_value"
}

proxy_list() {
	local SECTION=$1
	local OPTION=$2
	local ENCLOSE=$3

	config_get _value "$SECTION" "$OPTION"
	[ -n "$_value" ] && {
		for entry in $_value; do
			echo "$OPTION ${ENCLOSE}${entry}${ENCLOSE}"
		done
	}
}
