#!/bin/csh -f

set pname="$0"
set OPTIONS=()
while ( $#argv > 0 )
	switch ( "$1" ) 
	case -*:
		set OPTIONS=($OPTIONS:q "$1:q")
		breaksw
	default:
		if ( $?INPUT ) then
			echo "${pname}: only one input file may be specified."
			exit 1
		else
			set INPUT="$1:q"
		endif
		breaksw
	endsw
end

if ( ! $?INPUT ) then
	set INPUT="/usr/adm/ppp.log"
endif



		
egrep 'pppd .* started|Exit.' $INPUT  \
| sed \
	-e 's/ Exit./ FIN   FIN/' \
	-e 's/ pppd .* started by \([a-z]*\),.*/ DEBUT \1/' \
	-e 's/\([A-Za-z]*\) \([ 0-9][ 0-9]\) \([0-9\:]*\) .*pppd\[\(.*\)]: \(.*\) \(.*\)/\4	\5	\6	\1	\2	\3/' \
	-e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' \
	-e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' \
	-e 's/Jul/07/' -e 's/Aug/08/' -e 's/Sep/09/' \
	-e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/' \
	-e 's/\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\)/\1	\2	\3/' \
| sort +3n +4n +0n +1 \
| pppcostreport $OPTIONS

#     0.742 F = 180 s
#      
# -------------------------------------------------
#     hd md  hf mf  ct    dt   n      d    f
# -------------------------------------------------
#      0  0   5 59  2.8   504  42     0   359
#      6  0   7 59  2.0   360  20   360   479
#      8  0  12 29  1.0   180  92   480   749
#     12 30  13 29  1.4   252  14   750   809
#     13 30  17 59  1.0   180  92   810  1079
#     18  0  21 29  1.4   252  50  1080  1289
#     21 30  22 29  2.0   360  10  1290  1349
#     22 30  23 59  2.8   504  10  1350  1439
#     24 00  --------------------  1440
# -------------------------------------------------
#                             330
	total_taxe_per_day=330
	prix_taxe=0.742
	bareme=':504:0360:360:0480:180:0750:252:0810:180:1080:252:1290:360:1350:504:1440'
	#        ----++++ ----++++ ----++++ ----++++ ----++++ ----++++ ----++++ ----++++

echo "+----------------+----------------+-----------+-------+-----------+-----------+"
echo "|      DEBUT     |      FIN       | DUREE (s) | TAXES |  COUT (F) |UTILISATEUR|"
echo "+----------------+----------------+-----------+-------+-----------+-----------+"
###   | mm/dd hh:mm:ss | mm/dd hh:mm:ss | 188888 | 1330 | 1330.00 | hyacinte | 
curpid=0
curaction=''
curuser=''
curmonth=0
curday=0
curhour=0
curminute=0
cursecond=0
totduree=0
tottaxes=0
totcout=0.00

egrep 'pppd .* started|Exit.' $INPUT  \
| sed \
	-e 's/ Exit./ FIN   FIN/' \
	-e 's/ pppd .* started by \([a-z]*\),.*/ DEBUT \1/' \
	-e 's/\([A-Za-z]*\) \([ 0-9][ 0-9]\) \([0-9\:]*\) .*pppd\[\(.*\)]: \(.*\) \(.*\)/\4	\5	\6	\1	\2	\3/' \
	-e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' \
	-e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' \
	-e 's/Jul/07/' -e 's/Aug/08/' -e 's/Sep/09/' \
	-e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/' \
	-e 's/\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\)/\1	\2	\3/' \
| sort +3n +4n +0n +1 \
| while read pid action user month day hour minute second remainder ; do

	curuser="`echo $curuser'             '|sed -e 's/\(........\).*/\1/'`"
	day=`echo 00$day|sed -e 's/.*\(..\)$/\1/'`

	
#	echo '--------------------------------------------'
#	echo 'pid        = '$pid
#	echo 'action     = '$action
#	echo 'user       = '$user
#	echo 'month      = '$month
#	echo 'day        = '$day
#	echo 'hour       = '$hour
#	echo 'minute     = '$minute
#	echo 'second     = '$second
#	echo 'remainder  = '$remainder
	
	if [ $pid -eq $curpid ] ; then
		if [ "$curaction" = "DEBUT" -a "$action" = "FIN" ] ; then
		
			
			if [ $curmonth -eq $month ] ; then
				duree=`expr $day - $curday`
			else
				case $curmonth in 
				01|03|05|07|08|10|12|1|3|5|7)
					duree=`expr 31 + $day - $curday`
					;;
				04|06|09|11|4|6|9)
					duree=`expr 30 + $day - $curday`
					;;
				02|2)
					year=`date|sed -e 's/.* \([0-9][0-9][0-9][0-9]\)$/\1/'`
					if [ `expr "(" "$year" "%" "4" "=" "0" ")" "&" "(" "$year" "%" "100" "!=" "0" ")" "&" "(" "$year" "%" "400" "=" "0" ")" ` ] ; then
						duree=`expr 29 + $day - $curday`
					else
						duree=`expr 28 + $day - $curday`
					fi
					;;
				esac
			fi
			if [ $duree -ge 0 ] ; then
				duree=`expr "(" "(" "$duree" "*" "24" "+" "$hour" ")" "*" "60" "+" "$minute" ")" "*" "60" "+" "$second" "-" "(" "(" "$curhour" "*" "60" "+" "$curminute" ")" "*" "60" "+" "$cursecond" ")"`
			fi
			if [ $duree -ge 0 ] ; then
				if [ $duree -ge 86400 ] ; then
					taxes=`expr "(" "$duree" "/" "86400" ")" "*" "$total_taxe_per_day"`
					duree=`expr $duree % 86400`
				else
					taxes=0
				fi
				
				depart=`expr "$curhour" "*" "60" "+" "$curminute"`
				arrive=`expr "$hour"    "*" "60" "+" "$minute"`
	
	#	bareme='504:0360:360:0480:180:0750:252:0810:180:1080:252:1290:360:1350:504:1440'
	#	#       ---:++++ ---:++++ ---:++++ ---:++++ ---:++++ ---:++++ ---:++++ ---:++++
	
				td=$duree
				n=0
				while [ $td -gt 0 ] ; do
					i=`expr "$n" "*" "9" "+" "6"`
					d=`expr substr "$bareme" $i 4`
					if [ $depart -lt $d ] ; then
						i=`expr "$n" "*" "9" "+" "2"`
						t=`expr substr "$bareme" $i 3`
						if [ $depart -lt $arrive -a  $arrive -lt $d ] ; then
							# depart arrive d
							incrm=`expr "(" "(" "$arrive" "-" "$depart" ")" "*" "60" "+" "$t" "-" "1" ")" "/" "$t"`
							taxes=`expr "$taxes" "+" "$incrm"`
							td=0
						else
							# depart d arrive or arrive depart d
							incrm=`expr "(" "(" "$d" "-" "$depart" ")" "*" "60" "+" "$t" "-" "1" ")" "/" "$t"`
							taxes=`expr "$taxes" "+" "$incrm"`
							td=`expr "$td" "-" "$incrm" "*" "(" "$t" "-" "1" ")"`
						fi
						depart=$d
					fi
					n=`expr $n + 1`
					if [ "$n" -gt 7 ] ; then
						n=0
						depart=0
					fi
				done
				cout=`echo "scale = 2 ; $taxes * $prix_taxe + 0.00001"|bc`
				totcout=`echo "$totcout + $cout - 0.00001"|bc`
				totduree=`expr "$totduree" "+" "$duree"`
				tottaxes=`expr "$tottaxes" "+" "$taxes"`
				duree=`echo '              '$duree|sed -e 's/.*\(........\)$/\1/'`
				taxes=`echo '              '$taxes|sed -e 's/.*\(.....\)$/\1/'`
				cout=`echo '              '$cout|sed -e 's/.*\(.......\...\).*$/\1/'`
				echo "| $curmonth/$curday $curhour:$curminute:$cursecond | $month/$day $hour:$minute:$second | $duree  | $taxes |$cout |  $curuser |"
			fi
		else
			echo "| $curmonth/$curday $curhour:$curminute:$cursecond | $month/$day $hour:$minute:$second |  INVALIDE |INCONNU|  INCONNU  |  $curuser |"
		fi
	else
		if [ "$curaction" = "DEBUT" ] ; then
			echo "| $curmonth/$curday $curhour:$curminute:$cursecond |    INCONNU     |  INCONNU  |INCONNU|  INCONNU  |  $curuser |"
		elif [ "$action" = "FIN" ] ; then
			echo "|    INCONNU     | $month/$day $hour:$minute:$second |  INCONNU  |INCONNU|  INCONNU  |  INCONNU  |"
		fi
	fi
	
	curpid=$pid
	curaction=$action
	curuser=$user
	curmonth=$month
	curday=$day
	curhour=$hour
	curminute=$minute
	cursecond=$second
	
	echo "$totduree $tottaxes $totcout" > /tmp/pppcost.$$
done

read  totduree tottaxes totcout < /tmp/pppcost.$$
rm -f /tmp/pppcost.$$

totduree=`echo '                '$totduree|sed -e 's/.*\(........\)$/\1/'`
tottaxes=`echo '                '$tottaxes|sed -e 's/.*\(.....\)$/\1/'`
totcout=`echo '                '$totcout|sed -e 's/.*\(......\...\).*$/\1/'`
	
echo "+----------------+----------------+-----------+-------+-----------+-----------+"
echo "|                        TOTAUX   | $totduree  | $tottaxes | $totcout |           |"
echo "+----------------+----------------+-----------+-------+-----------+-----------+"

exit 0


cat ppp.avril \
| egrep 'pppd .* started|Exit.'  \
| sed \
	-e 's/ Exit./ FIN   FIN/' \
	-e 's/ pppd .* started by \([a-z]*\),.*/ DEBUT \1/' \
	-e 's/\([A-Za-z]*\) \([ 0-9][ 0-9]\) \([0-9\:]*\) .*pppd\[\(.*\)]: \(.*\) \(.*\)/\4	\5	\6	\1	\2	\3/' \
	-e 's/Jan/01/' -e 's/Feb/02/' -e 's/Mar/03/' \
	-e 's/Apr/04/' -e 's/May/05/' -e 's/Jun/06/' \
	-e 's/Jul/07/' -e 's/Aug/08/' -e 's/Sep/09/' \
	-e 's/Oct/10/' -e 's/Nov/11/' -e 's/Dec/12/' \
	-e 's/\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\)/\1	\2	\3/' \
| sort +3n +4n +0n +1 \
|openfile

