: !/bin/sh
echo "Started $0: $@"
# usage: wb3 SDATE EDATE STIME ETIME SITEID thisDIVID RESTcde
SDATE=$1; EDATE=$2; STIME=$3; ETIME=$4

if [ -n "$5" ]; then
        SITEID=$5
else
	SITEID="ALL"
fi
if [ -n "$6" ]; then
	thisDIVID="$6"
else
	thisDIVID="all"
fi
if [ -n "$7" ]; then
	RESTcde="$7"
else
	RESTcde="1"
fi
if [ -n "$8" ]; then
	platinumBATCH="$8"
else
	platinumBATCH="all"
fi


###################################
# from env
if [ -z "$switchboardcallcost" ]; then
	switchboardcallcost="0"
fi
if [ -z "$voicemailcallcost" ]; then
	voicemailcallcost="0"
fi

###################################
MMYYYY=`SDATEtoMMYYYY $SDATE`
billperiod=`MMYYYYtobillperiod $MMYYYY _`
batchtag="${billperiod}_${platinumBATCH}_${thisDIVID}"

#####################################
# save output

if [ -z "$logdir" ]; then
	logdir="topdat/${billperiod}/log"; export logdir
	export logdir
	mkdir -p $logdir
	echo "WARNING: wb3: logdir not set using [$logdir]"
fi

logfile="${logdir}/wb3_${batchtag}_log.rtf"

exec 2>&1 >"${logfile}"

echo "$0: SDATE=[$SDATE] EDATE=[$EDATE]"
echo "               thisDIVID=[$thisDIVID]"
echo "                 RESTcde=[$RESTcde]"
echo "           platinumBATCH=[$platinumBATCH]"
echo "     switchboardcallcost=[$switchboardcallcost]"
echo "       voicemailcallcost=[$voicemailcallcost]"
echo "                  MMYYYY=[$MMYYYY]"
echo "              billperiod=[$billperiod]"

###################################
# assumes wb3a has already been run
# which creates the follow files....

t21groups2file="t21groups2.unl"

t21dircsvfile="indata/${billperiod}/T21Eptc.csv"

###################################
# get exwebconf - should always get new copy incase of changes

exwebconffile="${logdir}/exwebconf_${batchtag}.csv"
runisql <<SQLCMD0 2>&1
unload to /tmp/exwebconf$$ delimiter ","
select * from exwebconf
order by parent;
SQLCMD0
cp /tmp/exwebconf$$ $exwebconffile
rm -f /tmp/exwebconf$$

## save to indata 
#cp $exwebconffile "indata/${billperiod}"

###################################
# get tbs data
# always get new copy for each batch run
tbsCustomerfile="indata/${billperiod}/tCustomer.unl"
tbsServiceTypefile="indata/${billperiod}/tServiceType.unl"
tbsElementfile="indata/${billperiod}/tElement.unl"
tbsSupplierfile="indata/${billperiod}/tSupplier.unl"

# Testing rjs
gettbs tCustomer $tbsCustomerfile
gettbs tServiceType $tbsServiceTypefile
gettbs tElement $tbsElementfile
gettbs tSupplier $tbsSupplierfile

###################################
# get invoice and CDE data from TBSdata
# and detail data
# - should always get new copy
platinvfile="${logdir}/platinv_${batchtag}.unl"
cdefile="${logdir}/cde_${batchtag}.cde"
# get invoice number exclusion sqlbit
if [ -f "invcde.excl_${billperiod}" ]; then
	. invcde.excl_${billperiod}
fi
getinvcde5 "$SDATE" "$EDATE" "$platinvfile" "$cdefile"

##detailodir="${logdir}/detail_${batchtag}"
##detailodir="detail_${MMYYYY}"
detailodir="detail_dir"
#time getinvcdedetail5 "$SDATE" "$EDATE" "$platinvfile" "$cdefile" "$detailodir"


##########################
# t21 call file created by wb3a - cdr9
t21callsfile="indata/${billperiod}/VICTRACK/c9_${MMYYYY}.in"

# t21 SWITCHBOARD call file created by wb3a - swb9
t21swcallsfile="indata/${billperiod}/VICTRACK/s9_${MMYYYY}.in"

# t21 VOICEMAIL call file created by wb3a - vml9
t21vmcallsfile="indata/${billperiod}/VICTRACK/v9_${MMYYYY}.in"

# t21 TEC (rent) file created by wb3a - tec9
t21tecfile="indata/${billperiod}/VICTRACK/r9_${MMYYYY}.in"

##############################
# create gsm calls file
#gsmcallsfile="indata/${billperiod}/VICTRACK/m10_${MMYYYY}.in"
gsmcallsfile="${logdir}/m10_${batchtag}.in"
# rjs 28/10/2003 Always do gsm calls processing
rm -f "$gsmcallsfile"
#if [ ! -f "$gsmcallsfile" ]; then
	echo "Creating Mobile gsm calls file: $gsmcallsfile" 
	dogsm $MMYYYY "${gsmcallsfile}"
#else
#	echo "Using existing Mobile gsm calls file: $gsmcallsfile" 
#fi

###########################
# create web billing
awk -f wb3.awk -v "MMYYYY=$MMYYYY" -v"sdate=$SDATE" -v"edate=$EDATE" \
	-v "logdir=$logdir" \
	-v "dot21verify=0" \
	-v "thisDIVID=$thisDIVID" \
	-v "RESTcde=$RESTcde" \
	-v "platinumBATCH=$platinumBATCH" \
	-v "switchboardcallcost=$switchboardcallcost" \
	-v "voicemailcallcost=$voicemailcallcost" \
	-v "platinvfile=$platinvfile" \
	-v "cdefile=$cdefile" \
	-v "detailodir=$detailodir" \
	-v "exwebconffile=$exwebconffile" \
	-v "t21callsfile=$t21callsfile" \
	-v "t21swcallsfile=$t21swcallsfile" \
	-v "t21vmcallsfile=$t21vmcallsfile" \
	-v "t21tecfile=$t21tecfile" \
	-v "gsmcallsfile=$gsmcallsfile" \
	-v "t21dircsvfile=$t21dircsvfile" \
	-v "t21groups2file=$t21groups2file" \
	-v "tbsCustomerfile=$tbsCustomerfile" \
	-v "tbsServiceTypefile=$tbsServiceTypefile" \
	-v "tbsElementfile=$tbsElementfile" \
	-v "tbsSupplierfile=$tbsSupplierfile"

# run allmobilescsv
allmobilescsv $billperiod $platinumBATCH $thisDIVID All

#billperiod="Sep_2008"
#platinumBATCH="all"
#DIVID="all"
##toplevelshipto="VRT"
#toplevelshipto=""


# check target
if [ -d "${targdir}" -o -L "${targdir}" ]; then
	# if lok exists
	if [ -e "${targdir}.lok" ]; then
		echo "ERROR: already Running  targdir ${targdir}"
		showlink=""
	else
		echo "INFO: Created targdir ${targdir}"
		showlink="SHOWLINK"
	fi
else
	echo "ERROR: ${targdir} NOT created"
	showlink=""
fi

if [ -e "${targdir}.lok" ]; then
	showlink=""
fi

# rename logs with ok fileext's
mv ${logdir}/HIERIDx_${batchtag}.lst ${logdir}/HIERIDx_${batchtag}_lst.rtf
mv ${logdir}/sundrys_${batchtag}.uniq ${logdir}/sundrys_${batchtag}_uniq.rtf
mv ${logdir}/DEPTID_${batchtag}.lst ${logdir}/DEPTID_${batchtag}_lst.rtf
mv ${logdir}/SID_${batchtag}.dup ${logdir}/SID_${batchtag}_dup.rtf

# remove / archive processed mobile detail data
if [ "$thisDIVID" = "all" -a "$platinumBATCH" = "all" ]; then
	# save if all run
	basegsmcallsfile=`basename $gsmcallsfile`
	#rm -f "indata/${billperiod}/VICTRACK/$basegsmcallsfile"
	#mv "$gsmcallsfile" "indata/${billperiod}/VICTRACK"
else	# remove it
	rm -f "$gsmcallsfile"
fi


########################################
#Testing
#exit 0

####################
# get warnings
errwarnlogfile="${logdir}/ex3_ERROR_WARNING_${batchtag}.rtf"
rm -f $errwarnlogfile
echo "" >>$errwarnlogfile
echo "Warning / Error LOG" >>$errwarnlogfile
echo "------------------------------------------" >>$errwarnlogfile
echo "" >>$errwarnlogfile
head -10 ${logdir}/wb3_${batchtag}_log.rtf >>$errwarnlogfile
echo "" >>$errwarnlogfile
egrep "^INFO:|^FATAL ERROR:|^WARNING:" ${logdir}/wb3_${batchtag}_log.rtf | sort -u >>$errwarnlogfile
echo "" >>$errwarnlogfile
if [ -s wb3_${batchtag}.out ]; then
	cat wb3_${batchtag}.out >>$errwarnlogfile
	echo "" >>$errwarnlogfile
fi

#--------------------------------------------------
# zip log files
rm -f ${logdir}/ex3_${batchtag}_LOGS.zip
zip -r -9 "ex3_${batchtag}_LOGS.zip" "${logdir}"
#zip -r -9 "ex3_${batchtag}_LOGS.zip" "${logdir}/*.txt" "${logdir}/*.csv" "${logdir}/ex3_*.rtf" "${logdir}/wb3_*.out"
mv "ex3_${batchtag}_LOGS.zip" "${logdir}"

########################################################################
# wb3.awk makes sym link if platinumBATCH is an invoice or div/sundry
# get actual platinumbatch file name / DIVID
actual_platinumBATCH_DIVID=`basename \`(cd "$targdir"; pwd)\``
export actual_platinumBATCH_DIVID
echo "actual_platinumBATCH_DIVID = [$actual_platinumBATCH_DIVID]"

actual_platinumBATCH=`basename \`(cd "$targdir"; pwd)\` | sed -e "s/_${thisDIVID}$//"`
export actual_platinumBATCH
echo "actual_platinumBATCH = [$actual_platinumBATCH]"

########################################################################
# email wb3 totals/csv
#emailex3tot "${logdir}/ex3" "$billperiod" "$thisDIVID" "$platinumBATCH" "" "$showlink"

#################################################
exit 0