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

START=50
USE_PROCD=1

CFGFILE=/var/etc/tinyproxy.conf
PROG=/usr/bin/tinyproxy

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

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" ] && [ -n "$via" ] && \
		echo "upstream $via$target"

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

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" ] && [ "$OPTION" = "LogFile" ] && {
		touch "$_value"
		chmod 666 "$_value"
	}
}

proxy_m_string() {
	local SECTION=$1
	local OPTION=$2

	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" 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
	}
}

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

	mkdir -p /var/etc
	chmod 0755 /var/etc
	{
	echo '### AUTOGENERATED CONFIGURATION'
	echo '### DO NOT EDIT'
	echo '### SEE /etc/config/tinyproxy INSTEAD'
	echo ''

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

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

	proxy_string "$1" DefaultErrorFile
	proxy_string "$1" StatHost StatHost 127.0.0.1
	proxy_string "$1" StatFile
	proxy_string "$1" LogFile

	proxy_m_string "$1" BasicAuth

	proxy_flag "$1" Syslog

	proxy_atom "$1" LogLevel

	proxy_flag "$1" XTinyproxy

	proxy_atom "$1" MaxClients
	proxy_atom "$1" MinSpareServers
	proxy_atom "$1" MaxSpareServers
	proxy_atom "$1" StartServers
	proxy_atom "$1" MaxRequestsPerChild
	proxy_list "$1" Allow

	proxy_string "$1" ViaProxyName
	proxy_string "$1" Filter

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

	proxy_list "$1" Anonymous '"'
	proxy_list "$1" ConnectPort

	config_foreach write_upstream upstream
	} > "$CFGFILE"

	procd_open_instance
	procd_set_param command "$PROG"
	procd_append_param command -c "$CFGFILE"
	procd_append_param command -d
	procd_close_instance
}

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