#!/bin/sh

SRV='squid'
CATERVA_INDIR="/etc/caterva/$SRV"
CONFDIR=/etc/squid
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"

    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_restart "$SRV"
    else
        service_reload "$SRV"
    fi
}

# Change squid service startup and running status.
# args: status restart-flag
proxy_control()
{
    if startup_control "$SRV" "$1"; then
        if [ "$1" = "on" ] && service_test "$SRV" "on"; 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
            fi
        else
            if service_control "$SRV" "$1"; 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
            fi
        fi
    else
        write_error "`_ 'Unable to set proxy server service startup status'`"
        [ -n "${ALTERATOR_DEBUG:-}" ] && \
                echo "Unable to set proxy server service startup status" 1>&2
    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_test "$SRV" "on"; 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_test "$SRV" "on"; then
                    update_conf "on"
                else
                    update_conf "off"
                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
