: !/bin/sh

carrier="TELSTRA"
month="$1"
infile="$2"

#adjmonth=$month
#adjmonth=`../../../adjmonth $month`

#datadirpat=`echo "$adjmonth" | awk '{ print "??" substr($1,1,2) substr($1,5,2) }'`
#echo "$0: datadirpat=[$datadirpat]"

#infile=`ls ${datadirpat}/rcp*.dat`
if [ -n "$3" ]; then
	outfile="$3"
else
	outfile="${carrier}_${month}.out"
fi

#echo "$0: infile=[$infile]   outfile=[$outfile]"

rm -f $outfile

#RENT DETAIL
#RBMICAVICTRACK -ATT GEORGE COOPER   2084549282        0210220409235702           2S02V     CALL MANAGER                     22 OCT TO 21 NOV                   5.00         5.00N       0.50       5.50

#CALL SUMMARY
#RBMICAVICTRACK -ATT GEORGE COOPER   2084549282        0210220409236319           2S  T                                      TOTAL CALL CHARGES                $0.00                                    

sed -e 's/
$//' "$infile" | \
awk -v "billmonth=$month" -v "carrier=$carrier" '

function trim(is) {
	#sub(/^\"/,"", is)
	#sub(/\"$/,"", is)
	while (sub(/^ /, "", is) != 0)
		is = is ""
	while (sub(/ $/, "", is) != 0)
		is = is ""
	return is
}

#####################################################
# main
BEGIN {
	monthstr[1]  = "Jan";
	monthstr[2]  = "Feb";
	monthstr[3]  = "Mar";
	monthstr[4]  = "Apr";
	monthstr[5]  = "May";
	monthstr[6]  = "Jun";
	monthstr[7]  = "Jul";
	monthstr[8]  = "Aug";
	monthstr[9]  = "Sep";
	monthstr[10] = "Oct";
	monthstr[11] = "Nov";
	monthstr[12] = "Dec";

	billmm = 0 + substr(billmonth,1,2)
	billyyyy = 0 + substr(billmonth,3,4)
	billperiod = monthstr[billmm] " " billyyyy
}

{
	gsub(/\r/, "")		# remove CR
	gsub(/\032/, "")	# remove FF


	servicenumber = trim(substr($0,61,18))
	billcode = substr($0,82,1)
	billsection = substr($0,83,1)
	transactiontype = substr($0,84,2)
	linetype = substr($0,86,1)

	################################
	# rent

	if ( billcode == "2" &&
	     billsection == "S" &&
	     transactiontype == "02" ) {

		btxt = substr($0,87,113)

		callclass = ""

		sdate_str = "CHARGE_DESC"
		btxt_stime_str = "000000"
		btxt_origin = "RENT"
		btxt_destination = trim(substr(btxt,6,30))	# description
		btxt_dialled = trim(substr(btxt,39,16))		# from - to
		btxt_rate = ""
		dursecs = 0
		btxt_cost = trim(substr(btxt,103,11))
		costinclgst = 0.0 + btxt_cost

		# rent
		if ( costinclgst != 0 ) {
			outrec()
			next
		}

		next
	}


	################################
	# call
	if ( billcode == "2" &&
	     billsection == "S" &&
	     transactiontype == "  " &&
	     linetype == "T" &&
	     substr($0,131,4) == "CALL" ) {

		callclass = ""

		datestr = substr($0,55,6)
		yy = substr(datestr,1,2)
		mm = substr(datestr,3,2)
		dd = substr(datestr,5,2)
		from_date = sprintf("%02d/%02d/20%02d", btxt_dd, btxt_mm, btxt_yy)

		sdate_str = "CHARGE_DESC"
		btxt_stime_str = "000000"
		btxt_origin = "CALL"
		btxt_destination = trim("TOTAL CALL CHARGES") # description
		btxt_dialled = ""
		dursecs = 0
		btxt_cost = substr($0,156,8)
		sub(/\$/, "", btxt_cost)
		costinclgst = 0.0 + btxt_cost

		# call
		if ( costinclgst != 0 ) {
			outrec()
			next
		}

		next
	}



	################################
	# call detail
	if (billsection == "D" && linetype == "D") {
		docalldetail()
		next
	}
}


function docalldetail()
{
	if ( length($0) < 199 ) {
		printf("too short\n")
		return
	}

# Bill Image

# TYPE: 1
#      CALL ANSWERS - MOBILENET MEMO                
#      CALL FORWARDING CHARGES                     
#      INTERNATIONAL DIRECT                       
#      CALLS RECEIVED O/S (GST FREE)             
#      CALLS MADE O/S      (GST FREE)           
#      CALLS MADE O/S -SMS (GST FREE)          
#      MOBILENET MESSAGEBANK                  
#      WAKE-UP AND REMINDER CALL             
#      DIAL-IT CALLS                        
#      INFORMATION CALLS                   
#      MOBILE ORIGINATED SMS              
#      MOBILE WAP/INTERNET CALLS         
#      NATIONAL DIRECT                  
#      NATIONAL DIRECT - DATA          
#      SERVICE CALL SURCHARGE         
#      SERVICE CALLS                 
#      DIVERTED TO   101
#         1         2         3         4         5         6         7
#SEQ     DATE    TIME   ORIGIN            NUMBER       RATE  MIN/SEC   INCL. GST
#12345678901234567890123456789012345678901234567890123456789012345678901234
#00001 22/08/02  11:18  MELBOURNE     0404814106MNET     P    2:43          1.06
#
# TYPE: 2
#      ENHANCED SMS                 
#         1         2         3         4         5         6         7
#12345678901234567890123456789012345678901234567890123456789012345678901234
#SEQ     DATE    TIME   SERVICE TYPE      DESCRIPTION  QTY   UNITS     INCL. GST
#00681 31/08/02  21:52  POCKETNEWS    AFL LIVE SCO       1 ONDEMAN          0.04
#
# TYPE: 3
#      MOBILE WAP/INTERNET SESSIONS
#         1         2         3         4         5         6         7
#12345678901234567890123456789012345678901234567890123456789012345678901234
#SEQ     DATE   TIME  ORIGIN      ACCESS POINT RATE HH:MM:SS VOL(KB)   INCL. GST
#00263 27/08/02 11:09 589 COLLINS  TELSTRA.INTE  O 00:02:16     144         3.39
#
# TYPE: 4 (2D13D)
#      DIVERTED TO   101
#         1         2         3         4         5         6         7
#12345678901234567890123456789012345678901234567890123456789012345678901234
#SEQ     DATE    TIME   DESTINATION       NUMBER             MIN/SEC   INCL. GST                                  
#00001 09/11/07  15:08  DIVERTED TO   101                     0:30          0.06       0.05        0.01       0.06


	btxt = substr($0,87,79)
	btxt_start = substr(btxt,1,3)
	if ( btxt_start == "   " ) {		# call class line
		callclass = trim(btxt);
		return
	}

	if ( btxt_start == "SEQ" ) {		# header
		if ( substr(btxt,24,6) == "ORIGIN" )
			rectype = 1
		if ( substr(btxt,24,12) == "SERVICE TYPE" )
			rectype = 2
		if ( substr(btxt,34,12) == "ACCESS POINT" )
			rectype = 3
		if ( substr(btxt,24,11) == "DESTINATION" )
			rectype = 4
		return
	}

	#printf("Service Number = [%s]     Call Class = [%s]\n", servicenumber, callclass);
	#printf("rectype = %d\n", rectype);
	#printf("%s\n", btxt);

	btxt_origin = "NOT SET"
	btxt_dialled = "NOT SET"
	dialstr = btxt_dialled
	btxt_rate = "NOT SET"
	btxt_duration = "NOT SET"

	if ( rectype == 1 ) {
		btxt_dd = substr(btxt,7,2)
		btxt_mm = substr(btxt,10,2)
		btxt_yy = substr(btxt,13,2)
		stimestr = substr(btxt,17,5)
		btxt_stime_str = substr(stimestr,1,2) substr(stimestr,4,2) "00"

		btxt_origin = trim(substr(btxt,24,13))
		btxt_dialled = trim(substr(btxt,38,17))
		dialstr = btxt_dialled
		# use unprocessed NUMBER for dest description
		btxt_destination = btxt_dialled
		# process dialled number to remove inconsistencies
		sub(/MNET/, "", btxt_dialled)		# remove MNET
		sub(/MN/, "", btxt_dialled)		# remove MN
		sub(/C/, "", btxt_dialled)		# remove C
		sub(/^\+61/, "0", btxt_dialled)		# replace with 0
		if ( length(btxt_dialled) >= 13 )
			sub(/^0061/, "0", btxt_dialled)	# replace with 0
		if ( length(btxt_dialled) >= 11 )
			sub(/^61/, "0", btxt_dialled)	# replace with 0

		btxt_rate = substr(btxt,57,1)
		btxt_duration = substr(btxt,59,7) ":00"
		durhh = 0
		durmm = 0 + substr(btxt_duration,1,4)
		durss = 0 + substr(btxt_duration,6,2)
		dursecs = (durhh * 60 * 60) + (durmm * 60) + durss

		btxt_cost = substr(btxt,67,13)
	}
	if ( rectype == 2 ) {
		btxt_dd = substr(btxt,7,2)
		btxt_mm = substr(btxt,10,2)
		btxt_yy = substr(btxt,13,2)
		btxt_stime_str = substr(btxt,17,5)

		btxt_servicetype = trim(substr(btxt,24,13))
		btxt_qty = trim(substr(btxt,51,7))
		btxt_units = trim(substr(btxt,59,7))

		btxt_origin = btxt_servicetype
		btxt_dialled = ""
		dialstr = btxt_dialled
		btxt_destination = trim(substr(btxt,38,12))
		btxt_rate = btxt_qty " " btxt_units
		btxt_duration = "00:00:00"
		durhh = 0
		durmm = 0
		durss = 0
		dursecs = 0

		btxt_cost = substr(btxt,67,13)
	}
	if ( rectype == 3 ) {
		btxt_dd = substr(btxt,7,2)
		btxt_mm = substr(btxt,10,2)
		btxt_yy = substr(btxt,13,2)
		btxt_stime_str = substr(btxt,16,5)

		btxt_accesspoint = trim(substr(btxt,35,12))
		btxt_volume = trim(substr(btxt,60,7))

		btxt_origin = trim(substr(btxt,22,12))
		btxt_dialled = ""
		dialstr = btxt_dialled
		btxt_destination = btxt_accesspoint
		btxt_rate = trim(substr(btxt,49,1))
		btxt_rate = btxt_rate " " btxt_volume
		btxt_duration = substr(btxt,51,8)
		durhh = 0 + substr(btxt_duration,1,2)
		durmm = 0 + substr(btxt_duration,4,2)
		durss = 0 + substr(btxt_duration,7,2)
		dursecs = (durhh * 60 * 60) + (durmm * 60) + durss

		btxt_cost = substr(btxt,67,13)
	}

	if ( rectype == 4 ) {
		btxt_dd = 0 + substr(btxt,7,2)
		btxt_mm = 0 + substr(btxt,10,2)
		btxt_yy = 0 + substr(btxt,13,2)
		stimestr = substr(btxt,17,5)
		btxt_stime_str = substr(stimestr,1,2) substr(stimestr,4,2) "00"

		btxt_origin = trim(substr(btxt,24,13))
		btxt_dialled = trim(substr(btxt,38,17))
		dialstr = btxt_dialled
		# use unprocessed NUMBER for dest description
		btxt_destination = btxt_dialled
		# process dialled number to remove inconsistencies
		sub(/MNET/, "", btxt_dialled)		# remove MNET
		sub(/MN/, "", btxt_dialled)		# remove MN
		sub(/C/, "", btxt_dialled)		# remove C
		sub(/^\+61/, "0", btxt_dialled)		# replace with 0
		if ( length(btxt_dialled) >= 13 )
			sub(/^0061/, "0", btxt_dialled)	# replace with 0
		if ( length(btxt_dialled) >= 11 )
			sub(/^61/, "0", btxt_dialled)	# replace with 0

		btxt_rate = substr(btxt,57,1)
		btxt_duration = substr(btxt,59,7) ":00"
		durhh = 0
		durmm = 0 + substr(btxt_duration,1,4)
		durss = 0 + substr(btxt_duration,6,2)
		dursecs = (durhh * 60 * 60) + (durmm * 60) + durss

		btxt_cost = substr(btxt,67,13)
	}


	# see whats in fields
	#print transactiontype | "sort -u >tt.uniq"
	#print dialstr | "sort -u >btxt_dialled.uniq"
	#print substr(dialstr,1,6) | "sort -u >btxt_dialled_6.uniq"
	#print btxt_origin | "sort -u >btxt_origin.uniq"

	#"date '+%Y'" | getline curyyyy 	
	#curCC = substr(curyyyy,1,2)
	#print "current yyyy = " curyyyy "   CC = " curCC
	sdate_str = sprintf("%02d/%02d/20%02d", btxt_dd, btxt_mm, btxt_yy)

	#exclgst = substr($0,166,11)
	#inclgst = substr($0,189,11)
	costinclgst = 0.0 + btxt_cost

	#printf("%s: ", servicenumber)
	#printf("%s", btxt)
	#printf("%s", substr(btxt,1,68))
	#printf(" BTXTCST:%s", btxt_cost)
	#printf(" EXCLGST:%s", exclgst)
	#printf(" INCLGST:%s", inclgst)
	#if ( btxt_cost != inclgst )
	#	printf(" COSTDIFF")
	#printf(" %0.2f", costinclgst)
	#printf("\n")

	# get dest onnet/offnet 
	#cmd0 = sprintf("../getnet %s", btxt_dialled)
	#cmd0 | getline netdesttype
	#close cmd0
	#printf("cmd0=[%s]\n", cmd0)
	#nettype = sprintf("MOBILEONNET_%s", netdesttype)

	outrec()

	#printf("\n")
}


function outrec()
{
	printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%d|%0.2f|\n", servicenumber, carrier, callclass, billperiod, sdate_str, btxt_stime_str, btxt_origin, btxt_destination, btxt_dialled, btxt_rate, dursecs, costinclgst)
}


END {
}

' >$outfile

test -f tt.uniq && mv -f tt.uniq tt_$1.uniq
test -f btxt_dialled.uniq && mv -f btxt_dialled.uniq btxt_dialled_$1.uniq
test -f btxt_dialled_6.uniq && mv -f btxt_dialled_6.uniq btxt_dialled_6_$1.uniq
test -f btxt_origin.uniq && mv -f btxt_origin.uniq btxt_origin_$1.uniq

exit 0