: !/bin/sh
eyyyymmdd="$1"
savedir="$2"
thisCENTRE="$3"
#echo "completebb1: $1 $2 $3"

tmpfile="/tmp/bb1_$$.tmp"
tmp5file="/tmp/bb1_5_$$.tmp"

exec 2>&1 >$tmpfile
#---------------------------------------------------
email_file()
{
	for emailaddr in $emaillist
	do
		# Force mail to pass DATE as GMT
		TZ=''; export TZ
		(
			echo "Please Find Attached Information
"
			for extraf in $extratxtfiles
			do
				if [ -n "$extraf" ]; then
					cat $extraf
					echo "
"	
				fi
			done
			for attf in $attfiles
			do
				uuencode $attf `basename $attf`
			done
		) | mail -s "$subject" $emailaddr
	done
}

#---------------------------------------------------
ex2totreptxtfile="ex2_totrep_${thisCENTRE}.txt"

bb1file="TIMS_${eyyyymmdd}.txt"
invfile="TIMS_${eyyyymmdd}_invoice.csv"
othfile="TIMS_${eyyyymmdd}.other"

bb1grandtotalfile="grandtotal_${eyyyymmdd}.txt"
bb1txtreptotalfile="reptotal_${eyyyymmdd}.txt"

(
echo "Totals for all groups (taken from $ex2totreptxtfile)..."
echo ""
egrep "Fixed Line Call Charges|Equipment and Service Charges|Miscellaneous Charges|Grand Total" \
	"$ex2totreptxtfile" | grep "Total"
echo ""
) > "$bb1grandtotalfile"

awk -F'|' -v"bb1file=$bb1file" '{ totcallcost += $8; totequipcost += $9 }
END {
	print "Totals added from bb1 file: " bb1file
	printf("    Calls Total: %.2f\n", totcallcost)
	printf("Equipment Total: %.2f\n", totequipcost)
	printf("    Grand Total: %.2f\n", (totcallcost + totequipcost))
	print ""
}' <"$bb1file" >"$bb1txtreptotalfile"


# save and send only if NOT already done
if [ ! -f "${savedir}/${bb1file}" ]; then
	# save back billing files
	cp "$bb1grandtotalfile" "$savedir"
	cp "$bb1txtreptotalfile" "$savedir"
	cp "$bb1file" "$savedir"
	cp "$invfile" "$savedir"
	cp "$othfile" "$savedir"

#	## Auto FTP results file to Oracle Financials
#	ftp -v -n <<FTP0
#	  open tracey.uws.edu.au
#	  user workprd work20prd
#	  ascii
#	  cd /opt/app/applprd/1103/cgl/1.0.0/input/TIMS
#		  put $HOME/system/TIMS_$eyyyymmdd.txt TIMS_$eyyyymmdd.txt
#	  ls -altr
#	  quit
#FTP0

	rtrun mv /u/catcom/reports/workprd/TIMS_????????.txt /u/catcom/reports/workprd/.save
	rtrun mv /u/catcom/reports/workprd/reptotal_????????.txt /u/catcom/reports/workprd/.save

	rtrun rm -f "/u/catcom/reports/workprd/$bb1txtreptotalfile"
	rtrun cp "$bb1txtreptotalfile" /u/catcom/reports/workprd
	rtrun chmod 644 "/u/catcom/reports/workprd/$bb1txtreptotalfile"
	rtrun chown workprd "/u/catcom/reports/workprd/$bb1txtreptotalfile"
	rtrun chgrp catcom "/u/catcom/reports/workprd/$bb1txtreptotalfile"

	rtrun rm -f "/u/catcom/reports/workprd/$bb1file"
	rtrun cp "$bb1file" /u/catcom/reports/workprd
	rtrun chmod 644 "/u/catcom/reports/workprd/$bb1file"
	rtrun chown workprd "/u/catcom/reports/workprd/$bb1file"
	rtrun chgrp catcom "/u/catcom/reports/workprd/$bb1file"
	
	echo "bb1 complete\r\n\r\n" >$tmp5file

	## to put end of log file in text of email
	#echo "\r\n\r\n" >$tmp5file
	#echo "Last 25 line of log (/u/catcom/debug/bb1.log)" >>$tmp5file
	#echo "\r\n" >>$tmp5file
	#tail -25 $tmpfile >>$tmp5file

	echo "" >>$tmp5file
	ls -altr /u/catcom/reports/workprd >>$tmp5file
	echo "" >>$tmp5file
	cat "$bb1txtreptotalfile" >>$tmp5file
	echo "" >>$tmp5file
	cat "$bb1grandtotalfile" >>$tmp5file
	echo "" >>$tmp5file
	attfiles=" \
	TIMS_${eyyyymmdd}.txt \
	TIMS_${eyyyymmdd}_invoice.csv \
	TIMS_${eyyyymmdd}.other \
	"
	extratxtfiles="$tmp5file"
	subject="Telmax21 bb1 for the month ending ${eyyyymmdd}"

	#emaillist="itaccounts@uws.edu.au support@transmit.com.au P.Hardaker@uws.edu.au"
	#emaillist="support@transmit.com.au"
	#emaillist="rod@transmit.com.au"
	#emaillist="catcom"

	emaillist="$WBADMINEMAIL"

	email_file
fi

# save log
#cat $tmpfile >>/u/catcom/debug/bb1.log
rm -f $tmpfile
rm -f $tmp5file

exit 0