#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2011 OpenWrt.org

START=50

CFGFILE=/var/etc/tinyproxy.conf

section_enabled() {
	config_get_bool enabled "$1" 'enabled' 0
	[ $enabled -gt 0 ]
}

start() {
	config_load 'tinyproxy'
	config_foreach start_proxy 'tinyproxy'
}

stop() {
	service_stop /usr/sbin/tinyproxy
}

start_proxy() {
	section_enabled "$1" || return 1

	mkdir -m0755 -p /var/etc
	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" StatHost StatHost 127.0.0.1 >> $CFGFILE
	proxy_string "$1" StatFile >> $CFGFILE
	proxy_string "$1" LogFile >> $CFGFILE

	proxy_flag "$1" Syslog >> $CFGFILE

	proxy_atom "$1" LogLevel >> $CFGFILE

	proxy_flag "$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_list "$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

	service_start /usr/sbin/tinyproxy -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
	local DEFAULT=$4

	config_get _value "$SECTION" "$OPTION"
	[ -z "$_value" ] && _value="$DEFAULT"
	[ -n "$_value" ] && echo "${ALIAS:-${OPTION}} "'"'"$_value"'"'
	[ -n "$_value" -a "$OPTION" = "LogFile" ] && {
		touch $_value
		chmod 666 $_value
	}
}

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

	config_get_bool _value "$SECTION" "$OPTION" 0
	[ "$_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
	}
}
