#!/bin/sh

DBFILE=/var/log/squid/squidmill.db
CONFFILE=/etc/sysconfig/squidmill
SOCKET=/var/run/squidmill/squidmill.sock
LIMIT=200

[ -s "$CONFFILE" ] && . "$CONFFILE" "$@"

alterator_api_version=1
. alterator-sh-functions
. alterator-service-functions
po_domain="alterator-squidmill"

# Change squidmill service startup and running status.
# args: status
squidmill_control()
{
    if service_control squidmill "$([ "$1" = 'on' ] && echo 'energize' || echo 'gronk')"; then
	[ -n "${ALTERATOR_DEBUG:-}" ] && \
	    echo "Squidmill service turned $1 successfully" 1>&2
    else
	write_error "$(printf "$(_ 'Unable to turn squidmill service %s')" $1)"
	[ -n "${ALTERATOR_DEBUG:-}" ] && \
	    echo "Unable to turn squidmill service $1" 1>&2
    fi
}

REPORT_STATUS=0

# args: sdate edate user uri min max . other-args
query_report()
{
    local access=
    if [ -S "$SOCKET" ]; then
	access="-c \"$SOCKET\""
    elif [ -f "$DBFILE" ]; then
	access="-d \"$DBFILE\""
    else
	return 0
    fi

    local cmd="/usr/sbin/squidmill $access -r -l $LIMIT -i${3:+ \"$3\"} -u${4:+ \"$4\"}${1:+ -s \"$1\"}${2:+ -e \"$2\"}${5:+ -m \"$5\"}${6:+ -M \"$6\"}${7:+ $7}"
    [ -n "${ALTERATOR_DEBUG:-}" ] && echo "$cmd" 1>&2
    eval "$cmd" </dev/null
}

# args: sdate edate user uri min max . other-args
report()
{
    local TMPDIR=`mktemp -d`
    local ret=

    query_report "$1" "$2" "$3" "$4" "$5" "$6" "$7" 1>"$TMPDIR/report" 2>"$TMPDIR/report.log"
    ret=$?

    if [ "$ret" -ne 0 ] && [ "$ret" -ne 100 ]; then
        write_error "$(printf "$(_ 'Squidmill error (%s)')" "$(tail -1 "$TMPDIR/report.log")")"
    else
	cat "$TMPDIR/report" | \
	    while IFS="	" read -r t s e i u; do
            write_table_item ident $i uri $u size $s timestamp "$t"
	done
    fi

    rm -rf "$TMPDIR"

    return $ret
}

# args: sdate edate user uri min max . other-args
write_summary()
{
      local TMPDIR=`mktemp -d`
      local sum="$(query_report "$1" "$2" "$3" "$4" "$5" "$6" "$7 -S" 2>"$TMPDIR/report.log")"
      local ret=$?
      if [ $ret -eq 0 ]; then
	echo "$sum" | (
		IFS="	" read -r t s e;
		write_string_param max_timestamp "$t"
            	write_string_param sum_size $s
            	write_string_param sum_elapsed $e )
      else
	      write_error "$(printf "$(_ 'Squidmill error (%s)')" "$(tail -1 "$TMPDIR/report.log")")"
      fi

      rm -rf "$TMPDIR"

      return $ret
}

# args: sdate edate
list_users()
{
    local access=
    if [ -S "$SOCKET" ]; then
	access="-c \"$SOCKET\""
    elif [ -f "$DBFILE" ]; then
	access="-d \"$DBFILE\""
    else
	return 0
    fi

    local cmd="/usr/sbin/squidmill $access -r -l $LIMIT -i ${1:+ -s \"$1\"}${2:+ -e \"$2\"}"
    [ -n "${ALTERATOR_DEBUG:-}" ] && echo "$cmd" 1>&2
    eval "$cmd" </dev/null | cut -f 4 | sort -u | \
        while read -r u; do
          write_enum_item "$u" "$u"
        done
}

# args date time
date_time()
{
    if [ -n "$1" ]; then
        echo "$1 $2"
    else
        echo "$1"
    fi
}

on_message()
{
    set_locale
    case "$in_action" in
    type)
        case "$in__objects" in
            /)
                write_type_item follow boolean
                ;;
        esac
        ;;
    read)
        case "$in__objects" in
            summary)
                write_summary "`date_time \"$in_sdate\" 00:00:00`" \
                              "`date_time \"$in_edate\" 23:59:59`" \
                              "$in_user_pat" "$in_uri_pat" \
                              "$in_min_traf" "$in_max_traf"
		REPORT_STATUS=$?
                ;;
            report-status)
		if [ $REPORT_STATUS -eq 0 ] || [ $REPORT_STATUS -eq 100 ]; then
			write_string_param report_status "$REPORT_STATUS"
			write_string_param limit "$LIMIT"
		fi
                ;;
            /)
                if service_control squidmill 'is-enabled' && \
		   service_control squidmill 'is-active'; then
                    write_bool_param follow 'on'
                else
                    write_bool_param follow 'off'
                fi
                ;;
        esac
        ;;
    write)
        case "$in__objects" in
            /)
                if test_bool $in_follow; then
                    squidmill_control on
                else
                    squidmill_control off
                fi
                ;;
        esac
        ;;
    list)
         case "$in__objects" in
            users)
                list_users "`date_time \"$in_sdate\" 00:00:00`" \
                           "`date_time \"$in_edate\" 23:59:59`"
                ;;
            /)
                report "`date_time \"$in_sdate\" 00:00:00`" \
                       "`date_time \"$in_edate\" 23:59:59`" \
                       "$in_user_pat" "$in_uri_pat" \
                       "$in_min_traf" "$in_max_traf"
		REPORT_STATUS=$?
                ;;
        esac
        ;;
    esac
}

message_loop
