#!/bin/sh
#
# $FreeBSD:
#

# PROVIDE: c-icap
# REQUIRE: DAEMON cleanvar
# BEFORE: LOGIN

#
# Add the following lines to /etc/rc.conf to enable c-icap:
#
# c-icap_enable="YES"
#
# See '/usr/local/bin/c-icap --help' for flags
#

. /etc/rc.subr

name="c_icap"
rcvar=`set_rcvar`

command="/usr/local/bin/c-icap"
required_dirs="/var/cache/c-icap"
pidfile="/var/run/c-icap/c-icap.pid"
required_files="/usr/local/etc/c-icap.conf"

start_precmd="c_icap_precmd"
stop_postcmd="rm -f $pidfile"

retval=1234567890

fileid=/var/lib/netpolice/installation_id

if [ ! -d /var/lib/netpolice ]; then mkdir -p /var/lib/netpolice; fi
if [ ! -f $fileid ]; then   touch $fileid; fi 


add_log_message()
{
    local logger='/usr/bin/logger'
    local tag='Netpolice'
    local facility='daemon'
    local level='error'
    local message="$1"
    if [ -z "$message" ]; then
	return 1
    else
	$logger -t "$tag" -p ${facility}.${level} $message
	if [ "x$?" != "x0" ]; then
	    return 2
	fi
    fi
}

c_icap_precmd()
{
    /usr/local/bin/python /usr/local/bin/push_installation.py -f $fileid -a http://as1.netpolice.ru/check.php?key=%s
    
    /usr/local/bin/python /usr/local/bin/check_installation.py -a http://ls.netpolice.ru/LSPublic.svc?wsdl -t10 -f $fileid
    retval=$?
    if [ $retval != 201 ]; then
	MSG="Invalid license for your netpolice installation, error code $retval"
	echo $MSG
	add_log_message "$MSG"
	return $retval
    else
	return 0 
    fi
}

# read settings, set default values
load_rc_config "$name"
: ${c_icap_enable="NO"}
: ${c_icap_flags=""}

run_rc_command "$1"
