#!/bin/sh

LOGIN=$1
IP=$2
CASH=$3 
ID=$4


SPEED=`/usr/local/bin/memcat --servers 127.0.0.1  LINIT_SPEED_${LOGIN}`
if [ -z "${SPEED}" ]
then
SPEED=`/etc/stargazer/GetDataL speed ${LOGIN}`
fi

UPSPEED=`/usr/local/bin/memcat --servers 127.0.0.1  LINIT_SPEEDUP_${LOGIN}`
if [ -z "${UPSPEED}" ]
then
UPSPEED=`/etc/stargazer/GetDataL speedup ${LOGIN}`
fi

MAC=`/usr/local/bin/memcat --servers 127.0.0.1  LINIT_MAC_${LOGIN}`
if [ -z "${MAC}" ]
then
MAC=`/etc/stargazer/GetDataL mac ${LOGIN}`
fi

SCOUNT="Kbit/s"

fwcmd="/sbin/ipfw -q"
arpcmd="/usr/sbin/arp"

cur_date=`date \+\%Y.\%m.\%d`
cur_time=`date \+\%H:\%M:\%S`



# fix user mac to ip
${arpcmd} -S $IP $MAC

#SPEED CONTROL
${fwcmd} pipe `expr $ID + 101` config bw $UPSPEED$SCOUNT queue 128Kbytes
${fwcmd} pipe `expr $ID + 32101` config bw $SPEED$SCOUNT queue 128Kbytes

# SHAPER
${fwcmd} table 3 add $IP `expr $ID + 101`
${fwcmd} table 4 add $IP `expr $ID + 32101`

${fwcmd} table 47 delete $IP


# DAY/NIGHT switcher
/bin/echo $SPEED:`expr $ID + 32101` > /etc/stargazer/dn/$LOGIN
/bin/chmod 777 /etc/stargazer/dn/$LOGIN


# ADD TO LOG
echo "$cur_date $cur_time CONNECT: ID-$ID;LOGIN-$LOGIN;IP-$IP;CASH-$CASH;SPEED-$SPEED;UPSPEED-$UPSPEED,MAC-$MAC" >> /var/stargazer/allconnect.log