#!/bin/bash
#
#
######################################
### Created by Vitaliy Razzhivin   ###
### Network Solution, 2013         ###  
### razzhivin@lanbilling.ru        ###
### LB-base-2.0-007+               ###
### CentOS 5.x-6.x                 ###
### Ericsson SmartEdge             ###
### Dynamic CLIPS                  ###
######################################
#
# Prerequisites:
# - netmask util (e.g: rpm -ivh http://pkgs.repoforge.org/netmask/netmask-2.3.7-3.2.el5.rf.x86_64.rpm)
# - freeradius-utils (yum install freeradius-utils)

PARAMS="$*" # all parameters for ./handler from billing
LOG='/var/log/billing/srvctl.log' # no comments

COA_PORT='3799' # port is default
COA_SECRET='LANB_coa' # coa secret key

RADCLIENT='/usr/bin/radclient -t1 -r1 -c1 -x' # use mega-special radius client that support CoA :)
CONTEXT='LBILL'

REDIRECT_PROFILE='CABINET' # redirect profile name
FORWARD_POLICY='CABINET' # forward policy name

# zeroing variables
ACTION=''
REASON=''
SESSION=''
LOGIN=''
NAS=''
IPLIST=''
SHAPE=''
OLDSHAPE=''
BNGNAME=''
SUBSCRIBER=''
GUEST=''
PASSWORD=''
OLDBLOCKED=''
BLOCKED=''

log()
{
    echo "[`date +'%d-%m-%Y %H:%M:%S'`] (${SUBSCRIBER} ${IP}) - $1" >> $LOG # log regular events
}

log_error()
{
    echo "[`date +'%d-%m-%Y %H:%M:%S'`] - $1 [$PARAMS]" >> $LOG # log error events
}

# parsing handler parameters
while [ -n "$1" ]
do
    case $1 in
    "--action")
    ACTION=$2
    ;;
    "--reason")
    REASON=$2
    ;;
    "--session")
    SESSION=$2
    ;;
    "--login")
    LOGIN=$2
    ;;
    "--nas")
    NAS=$2
    ;;
    "--ip" | "--net")
    IP=$2
    ;;
    "--shape")
    SHAPE=$2
    ;;
    "--oldshape")
    OLDSHAPE=$2
    ;;
    "--opt-bng-name")
    BNGNAME=$2
    ;;
    "--mac")
    SUBSCRIBER=$2
    ;;
    "--guest")
    GUEST=$2
    ;;
    "--password")
    PASSWORD=$2
    ;;
    "--oldblocked")
        OLDBLOCKED=$2
        ;;
    "--blocked")
        BLOCKED=$2
        ;;
    *)
    log_error "Unknown parameters: $1, $2"
    ;;
    esac

    shift 2
done

_SUBSCRIBER=${SUBSCRIBER:0:2}
    for((i=2;i<${#SUBSCRIBER};i+=2))
    do
        _SUBSCRIBER=${_SUBSCRIBER}:${SUBSCRIBER:$i:2}
    done
    SUBSCRIBER=${_SUBSCRIBER}

# log_error "DEBUG"

case $ACTION in
    "stop")
    
    if [ -z ${SUBSCRIBER} ]
    then
        log_error "SUBSCRIBER is not defined"
        exit 1
    fi

    if [ -z ${SHAPE} ]
    then
        log_error "SHAPE is not defined"
        exit 1
    fi

    if [ -z ${NAS} ]
    then
        log_error "NAS is not defined"
        exit 1
    fi

    if [ ${REASON} = 'changed' ]
    then
        if [ -n ${OLDSHAPE} ]
        then
            log "Changing speed ${OLDSHAPE} => ${SHAPE}"
            echo "User-name=${SUBSCRIBER}
              Dynamic-QoS-Param=\"police-class-rate LCLASS rate-absolute ${SHAPE}\"
              Dynamic-QoS-Param=\"meter-class-rate LCLASS rate-absolute ${SHAPE}\"
              Dynamic-QoS-Param=\"police-class-burst LCLASS $[SHAPE*150]\"
              Dynamic-QoS-Param=\"meter-class-burst LCLASS $[SHAPE*150]\"
              Context-Name=\"${CONTEXT}\"" | ${RADCLIENT} ${NAS}:${COA_PORT} coa ${COA_SECRET} >> ${LOG}
        fi
    else
        log "Logging off subscriber => ${REASON}"
        echo "User-name=${SUBSCRIBER}
                  Context-Name=\"${CONTEXT}\"" | ${RADCLIENT} ${NAS}:${COA_PORT} disconnect ${COA_SECRET} >> ${LOG}
    fi
    ;;
    "isg-stop")
        if [ ${OLDBLOCKED} = '0' ]
        then
            log "Account is blocked switching off account"
        echo "User-name=${SUBSCRIBER}
        HTTP-Redirect-Profile-Name=\"${REDIRECT_PROFILE}\"
        Forward-Policy=\"in:${FORWARD_POLICY}\"
        Context-Name=\"${CONTEXT}\"" | ${RADCLIENT} ${NAS}:${COA_PORT} coa ${COA_SECRET} >> ${LOG}
        else
            log "Switching on account"
            echo "User-name=${SUBSCRIBER}
        Forward-Policy=\"in:\"
        Dynamic-QoS-Param=\"police-class-rate LCLASS rate-absolute ${SHAPE}\"
        Dynamic-QoS-Param=\"meter-class-rate LCLASS rate-absolute ${SHAPE}\"
        Dynamic-QoS-Param=\"police-class-burst LCLASS $[SHAPE*150]\"
        Dynamic-QoS-Param=\"meter-class-burst LCLASS $[SHAPE*150]\"
        Context-Name=\"$CONTEXT\"" | ${RADCLIENT} ${NAS}:${COA_PORT} coa ${COA_SECRET} >> ${LOG}
        fi
        ;;
    "quota")
        echo "1" > /dev/null
        ;;
    "start")
        echo "1" > /dev/null
        ;;
    "edit")
        echo "1" > /dev/null
        ;;
    "off")
        echo "1" > /dev/null
        ;;
    "on")
        echo "1" > /dev/null
        ;;
    "create")
        echo "1" > /dev/null
        ;;
    *)
        log_error "Unknown action $action"
        ;;
esac