#!/bin/sh

SRV='squid'
CATERVA_INDIR="/etc/caterva/$SRV"
CONFDIR=/etc/squid
CONFIG=$CONFDIR/squid.conf
ALT_CONFDIR="$CONFDIR/alterator"
BACKUP_DIR="$ALT_CONFDIR/backup"
PRSCRIPT="$ALT_CONFDIR/port-redirection.sh"
LOGFILE="$ALT_CONFDIR/log"

alterator_api_version=1
. alterator-sh-functions
po_domain="alterator-squid"

# Use common service management functions
. alterator-service-functions

# Configuration update
# args: status
update_conf()
{
    local log="$LOGFILE"
    local ret=0

    mkdir -p "$ALT_CONFDIR"
    date > "$log"
    echo >> "$log"

    echo "Parsing the current squid configuration..." >> "$log"
    cat $CONFIG | alterator-squid-update 2>>"$log"
    ret=$?
    if [ $ret -ne 0 ]; then
        write_error "`_ 'Unable to parse the configuration file. Please, keep the original file structure while making the changes.'`"
        return 1
    fi

    cp $CONFIG $CONFIG.orig

    echo >> "$log"
    if [ "$ret" -eq 0 ]; then
        echo "\$ verborum-caterva -vr -i \"$CATERVA_INDIR\" -o \"$CONFDIR\" -b" >> "$log"
        verborum-caterva -vr -i "$CATERVA_INDIR" -o "$CONFDIR" -b >> "$log" 2>&1
        ret=$?
        echo >> "$log"
        if [ "$ret" -eq 0 ]; then
            echo "Run port redirection script $PRSCRIPT" >> "$log"
            /bin/sh -x "$PRSCRIPT" "$1" >> "$log" 2>&1
            ret=$?
            [ "$ret" -eq 0 ] || "Port redirection script failed" >> "$log"
        else
            echo "Configuration update failed" >> "$log"
        fi
    fi
    return $ret
}

# Reloads or restarts the Squid service
# args: restart-flag
squid_reload()
{
    if test_bool "$1"; then
        service_control "$SRV" restart
    else
        service_control "$SRV" reload
    fi
}

# Change squid service startup and running status.
# args: status restart-flag
proxy_control()
{
    if [ "$1" = "on" ] && service_control "$SRV" 'is-active'; then
        if squid_reload "$2"; then
            [ -n "${ALTERATOR_DEBUG:-}" ] && \
                echo "Proxy server service reloaded successfully" 1>&2
        else
            write_error "`_ 'Unable to reload the proxy server service'`"
            [ -n "${ALTERATOR_DEBUG:-}" ] && \
                echo "Unable to reload the proxy server service" 1>&2
	    return 0
        fi
    fi

    if service_control "$SRV" "$([ "$1" = 'on' ] && echo 'energize' || echo 'gronk')"; then
        [ -n "${ALTERATOR_DEBUG:-}" ] && \
            echo "Proxy server service turned $1 successfully" 1>&2
    else
        write_error "`printf \"\`_ 'Unable to turn proxy server service %s'\`\" $1`"
        [ -n "${ALTERATOR_DEBUG:-}" ] && \
            echo "Unable to turn proxy server service $1" 1>&2
	return 0
    fi
}

on_message()
{
    set_locale
    case "$in_action" in
    type)
        write_type_item commit boolean
        write_type_item status boolean
        write_type_item restart boolean
        ;;
    read)
        if service_control "$SRV" 'is-enabled' && \
	   service_control "$SRV" 'is-active'; then
            write_bool_param 'status' 'on'
        else
            write_bool_param 'status' 'off'
        fi
        ;;
    write)
        if test_bool "$in_commit"; then
            if [ -n "$in_status" ]; then
                if test_bool "$in_status"; then
                    update_conf "on" && proxy_control "on" "$in_restart"
                else
                    update_conf "off" && proxy_control "off" "$in_restart"
                fi
            else
                if service_control "$SRV" 'is-enabled' && \
		   service_control "$SRV" 'is-active'; then
                    update_conf "on" && proxy_control "on" "$in_restart"
                else
                    update_conf "off" && proxy_control "off" "$in_restart"
                fi
            fi
        else
            if [ -n "$in_status" ]; then
                if test_bool "$in_status"; then
                    proxy_control "on" "$in_restart"
                else
                    proxy_control "off" "$in_restart"
                fi
            fi
        fi
        ;;
    esac
}

message_loop
