: !/bin/sh
# T21BATCHNAME wb4
# T21BATCHDESC Generate Web Billing (Phase2)

# ALLOWBLANKDATES false
# CUSTOMDESC
# CUSTOMDEF
# T21BATCHVAR1DESC retroBDL
# T21BATCHVAR1VALU
# T21BATCHVAR2DESC Enter distid ID (Blank for ALL)
# T21BATCHVAR2VALU
# T21BATCHVAR3DESC
# T21BATCHVAR3VALU
# T21BATCHVAR4DESC
# T21BATCHVAR4VALU
# T21BATCHVAR5DESC
# T21BATCHVAR5VALU
# T21BATCHENDARGS
########################################################################


# usage: wb4 SDATE EDATE STIME ETIME SITEID thisCENTRE
SDATE=$1; EDATE=$2; STIME=$3; ETIME=$4

if [ -n "$5" ]; then
        SITEID=$5
else
	SITEID="ALL"
fi
if [ -n "$6" ]; then
	thisCENTRE="$6"
else
	thisCENTRE="all"
fi

#-----
# from doex3web
if [ -n "$7" ]; then
        RESTcde="$7"
else
        RESTcde="1"
fi
if [ -n "$8" ]; then
        platinumBATCH="$8"
else
        platinumBATCH="all"
fi
#-----

if [ -n "$T21BATCHVAR1VALU" ]; then
	retroBDL="$T21BATCHVAR1VALU"
else
	retroBDL="0"
fi
if [ -n "$T21BATCHVAR2VALU" ]; then
	thisDISTID="$T21BATCHVAR2VALU"
else
	thisDISTID="all"
fi

export retroBDL

if [ "$retroBDL" != "1" ]; then
	exfnprefix="ex4_"
	wbfnprefix="wb4_"
else
	exfnprefix="ex4r_"
	wbfnprefix="wb4r_"
fi


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

###################################

MMYYYY=`SDATEtoMMYYYY $SDATE`
echo "MMYYYY = [$MMYYYY]"

billperiod=`MMYYYYtobillperiod $MMYYYY _`
echo "$MMYYYY - billperiod = [$billperiod]"

#batchtag="${billperiod}_${platinumBATCH}_${thisDIVID}"
#batchtag="${billperiod)_${thisCENTRE}"
batchtag="${thisCENTRE}"

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

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

#logfile="${wbfnprefix}${thisCENTRE}_log.rtf"
logfile="${wbfnprefix}${batchtag}_log.rtf"

exec 2>&1 >"$logfile"

echo "$0: SDATE=[$SDATE] EDATE=[$EDATE] thisCENTRE=[$thisCENTRE]"
echo "         retroBDL=[$retroBDL]"
echo "       thisDISTID=[$thisDISTID]"

export SDATE EDATE thisCENE

#########################################################
## for Back Billing
#savedir="indata/${billperiod}/TELMAX21"
#
#edd=`echo $EDATE | sed -e 's/\/..\/....$//g'`
#emm=`echo $EDATE | sed -e 's/^..\///g;s/\/....$//g'`
#eyyyy=`echo $EDATE | sed -e 's/^..\/..\///g'`
#eyyyymmdd="${eyyyy}${emm}${edd}"
#
#if [ "$retroBDL" != "1" ]; then
#	# init bb1 files
#	bb1file="TIMS_${eyyyy}${emm}${edd}.txt"
#	invfile="TIMS_${eyyyy}${emm}${edd}_invoice.csv"
#	othfile="TIMS_${eyyyy}${emm}${edd}.other"
#	# remove any remnants and create empty files before starting
#	# (wb4.awk will append to these files)
#	> "$bb1file"
#	> "$invfile"
#	> "$othfile"
#
#	> "old$bb1file"
#	> "old$invfile"
#	> "old$othfile"
#fi


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

acccodeconffile="indata/${billperiod}/t21acccodeconf.unl"

t21rtfile="indata/${billperiod}/t21rt.unl"

mobconffile="indata/${billperiod}/mobconf.unl"

chargecatfile="indata/${billperiod}/chargecat.unl"

dataplanfile="indata/${billperiod}/dataplan.unl"

t21extrangesfile="indata/${billperiod}/t21extranges.unl"

t21groups4file="indata/${billperiod}/t21groups4.unl"

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

if [ "$retroBDL" != "1" ]; then
	t21WBDLfile="indata/${billperiod}/t21WBDL.unl"
else
	t21WBDLfile="indata/${billperiod}/t21WBDLr.unl"
fi

t21wbdexcludefile="indata/${billperiod}/t21wbdexclude.unl"

#tblCustomerfile="indata/${billperiod}/tblCustomer.unl"
#tblTASSEMapfile="indata/${billperiod}/tblTASSEMap.unl"

########################################################

if [ "$retroBDL" != "1" ]; then
	if [ "$thisCENTRE" = "all" ]; then
		echo "rm -fr topdat/${billperiod}"
		rm -fr topdat/${billperiod}
	fi
fi

###################################
# get new copy of mobile conf

rm -f $mobconffile
echo "Creating mobile conf file: $mobconffile" 
##getmobconf "$mobconffile"
#getSIDMAINTconf "$mobconffile" "" "mobile_phones" $SDATE $EDATE

# get mobconf info from tbs data
gettbsmobconf "$mobconffile"

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

exwebconffile="exwebconf.csv"
runisql <<SQLCMD0 2>&1
unload to /tmp/$exwebconffile delimiter ","
select * from exwebconf
order by parent;
SQLCMD0
cp /tmp/$exwebconffile $exwebconffile

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

###################################
# get new copy of groups4 hierarchy from tbs
# create t21groups4.unl

rm -f $t21groups4file
echo "Creating T21 groups4 file: $t21groups4file" 
#gett21groups4 $t21groups4file
gett21groups4 $t21groups4file 1 1 "$SDATE" "$EDATE"

###################################
# 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"
tbsPersonfile="indata/${billperiod}/tPerson.unl"
tbsPersonServiceListfile="indata/${billperiod}/tbspersonservicelist.unl"

## Testing
# get tbs config data
gettbs tCustomer $tbsCustomerfile
gettbs tServiceType $tbsServiceTypefile
gettbs tElement $tbsElementfile
gettbs tSupplier $tbsSupplierfile
#gettbs tPerson $tbsPersonfile
copytPerson "$tbsPersonfile"

# get tbs person serverice list
gettbspersonservicelist $tbsPersonServiceListfile


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

##Testing
##getinvcde6 "$SDATE" "$EDATE" "$platinvfile" "$cdefile"

##tdetailodir="${logdir}/detail_${batchtag}"
##tdetailodir="detail_${MMYYYY}"
#tdetailodir="detail_dir"
#tdetailodir="topdat/${billperiod}/data/tdetail"
tdetailodir="indata/${billperiod}/tdetail"
#splitby=""	# all
splitby="0"	# shipto/invoiceno/batchid
#splitby="1"	# batchid/serviceid
#splitby="2	# invoiceno/batchid

mcsvodir="indata/${billperiod}/mimportcsv"

#Testing
#getinvcdedetail6 "$SDATE" "$EDATE" "$platinvfile" "$cdefile" "$tdetailodir" "$splitby" "$mcsvodir"

#save 
cp $platinvfile "indata/${billperiod}"
cp $cdefile "indata/${billperiod}"

mimportcsvdir="indata/${billperiod}/mimportcsv"


##########################
# t21 account code call file created by wb4a
t21acccodecallsfile="indata/${billperiod}/TELMAX21/cda_${MMYYYY}.in"

##########################
# t21 overheads file created by wb4a
t21ovhfile="indata/${billperiod}/TELMAX21/ovh_${MMYYYY}.in"

##########################
# t21 call file created by wb4a
t21callsfile="indata/${billperiod}/TELMAX21/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"

##########################
# remove tmp t21callsfile
# Testing
# (for testing dont rm after first run ok)
#rm -f ${t21callsfile}.tmp

##############################
# create m11cdr file
m11cdrfile="indata/${billperiod}/TELMAX21/wb4_m11_cdr.in"
if [ "$retroBDL" != "1" ]; then
	# always redo
	rm -f "$m11cdrfile"
	if [ ! -f "$m11cdrfile" ]; then
		echo "Creating m11cdr file: $m11cdrfile" 
		# from getdetail6...
		sort < "$tdetailodir/wb4_m11_cdr.in" > "$m11cdrfile"
	else
		echo "Using existing m11cdr file: $m11cdrfile" 
	fi
fi

##############################
# create mobile calls file
mobcallsfile="indata/${billperiod}/TELMAX21/wb4_m11_${MMYYYY}.in"
if [ "$retroBDL" != "1" ]; then
	# always redo
	# Testing
	rm -f "$mobcallsfile"
	if [ ! -f "$mobcallsfile" ]; then
		echo "Creating mobile calls file: $mobcallsfile" 
		#domob $SDATE $EDATE wb4_m11.in
		#mv wb4_m11.in "$mobcallsfile"
		#dogsm $MMYYYY "${mobcallsfile}"
		# from getdetail6...
		sort < "$tdetailodir/wb4_m11_${MMYYYY}.in" > "$mobcallsfile"
	else
		echo "Using existing mobile calls file: $mobcallsfile" 
	fi
fi

###########################
# create web billing
#
#gawk --dump-variable
awk \
    -f wb4.awk -v "MMYYYY=$MMYYYY" -v"sdate=$SDATE" -v"edate=$EDATE" \
	-v "logdir=$logdir" \
	-v "dot21verify=0" \
	-v "retroBDL=$retroBDL" \
	-v "thisDISTID=$thisDISTID" \
	-v "thisCENTRE=$thisCENTRE" \
        -v "RESTcde=$RESTcde" \
        -v "platinumBATCH=$platinumBATCH" \
        -v "switchboardcallcost=$switchboardcallcost" \
        -v "voicemailcallcost=$voicemailcallcost" \
        -v "platinvfile=$platinvfile" \
        -v "cdefile=$cdefile" \
	-v "tdetailodir=$tdetailodir" \
	-v "mimportcsvdir=$mimportcsvdir" \
	-v "t21acccodecallsfile=$t21acccodecallsfile" \
	-v "t21ovhfile=$t21ovhfile" \
	-v "t21callsfile=$t21callsfile" \
        -v "t21swcallsfile=$t21swcallsfile" \
        -v "t21vmcallsfile=$t21vmcallsfile" \
        -v "t21tecfile=$t21tecfile" \
	-v "m11cdrfile=$m11cdrfile" \
	-v "mobcallsfile=$mobcallsfile" \
	-v "acccodeconffile=$acccodeconffile" \
	-v "exwebconffile=$exwebconffile" \
	-v "mobconffile=$mobconffile" \
	-v "chargecatfile=$chargecatfile" \
	-v "t21rtfile=$t21rtfile" \
	-v "t21extrangesfile=$t21extrangesfile" \
	-v "t21groups4file=$t21groups4file" \
	-v "t21dircsvfile=$t21dircsvfile" \
	-v "t21WBDLfile=$t21WBDLfile" \
	-v "t21wbdexcludefile=$t21wbdexcludefile" \
        -v "tbsCustomerfile=$tbsCustomerfile" \
        -v "tbsServiceTypefile=$tbsServiceTypefile" \
        -v "tbsElementfile=$tbsElementfile" \
        -v "tbsSupplierfile=$tbsSupplierfile" \
        -v "tbsPersonfile=$tbsPersonfile" \
        -v "tbsPersonServiceListfile=$tbsPersonServiceListfile"

# run allmobilescsv
# feature moved to wb4.awk instead of modifying allmobilecsv script
#allmobilescsv $billperiod $platinumBATCH $thisDIVID All

if [ "$retroBDL" != "1" ]; then
	# create index files
	mkind "topdat/$billperiod"
fi

# rename HIERIEDx file
if [ "$retroBDL" != "1" ]; then
	HIERIDxrtffile="HIERIDx_${batchtag}_lst.rtf"
	mv HIERIDx_${batchtag}.lst "$HIERIDxrtffile"
else
	HIERIDxrtffile="HIERIDx_${batchtag}_rlst.rtf"
	mv HIERIDx_${batchtag}.rlst "$HIERIDxrtffile"
fi

## save totals files
#if [ "$retroBDL" != "1" ]; then
#	mv ex4_totrep.txt ex4_totrep_${thisCENTRE}.txt
#	mv ex4_totcsv*.csv ex4_totcsv_${thisCENTRE}.csv
#else
#	mv ${exfnprefix}totrep.txt ${exfnprefix}totrep_${thisCENTRE}.txt
#	mv ${exfnprefix}totcsv*.csv ${exfnprefix}totcsv_${thisCENTRE}.csv
#fi


#rjswb3
## 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
#-rjswb3


## run additional customer import csv creation
#getallmcsv6 $SDATE $EDATE


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

####################
# get warnings
errwarnlogfile="${exfnprefix}ERROR_WARNING_${thisCENTRE}.rtf"
rm -f $errwarnlogfile
echo "" >>$errwarnlogfile
echo "Warning / Error LOG" >>$errwarnlogfile
echo "------------------------------------------" >>$errwarnlogfile
echo "" >>$errwarnlogfile
head -3 "$logfile" >>$errwarnlogfile
echo "" >>$errwarnlogfile
if [ "$retroBDL" != "1" ]; then
	egrep "FATAL|WARNING|ERROR" "$logfile" | sort -u >>$errwarnlogfile
else
	egrep "FATAL|BMRETRO" "$logfile" >>$errwarnlogfile
fi
echo "" >>$errwarnlogfile
if [ -s ${wbfnprefix}${batchtag}.out ]; then
	cat ${wbfnprefix}${batchtag}.out >>$errwarnlogfile
echo "" >>$errwarnlogfile
fi

# zip log files
ex4zip="${exfnprefix}${thisCENTRE}_LOGS.zip"
rm -f "ex4zip"
zip -9 "$ex4zip" \
	$errwarnlogfile \
	$logfile \
	$HIERIDxrtffile


########################
# archive topdat
#( time arctopdat 2>&1 ) 2>&1 >arctopdat.log
#mv arctopdat.log arctopdat_log.rtf
#zip -9 arctopdat_log.zip arctopdat_log.rtf
#touch arctopdat_log.zip


#rjswb3
#########################################################################
## 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/_${thisDIVI}$//"`
#export actual_platinumBATCH
#echo "actual_platinumBATCH = [$actual_platinumBATCH]"
#
#########################################################################
## email wb3 totals/csv
#emailex3tot "${logdir}/ex3" "$billperiod" "$thisDIVID" "$platinumBATCH" "" "$shwlink"
#-rjswb3

## email wb4 totals/csv
##emailex4tot ${exfnprefix} $thisCENTRE arctopdat_log.zip
#emailex4tot ${exfnprefix} $thisCENTRE


#########################################################
# achive logs
#logdir="topdat/${billperiod}/log"
logdir="logs/$billperiod"
rm -rf "$logdir"
mkdir -p "$logdir"
ls HIERIDx_${batchtag}_lst.rtf \
 ex4_totcsv_${thisCENTRE}_mobiles.csv \
 ex4_totcsv_${thisCENTRE}.csv \
 ex4_totrep_${thisCENTRE}.txt \
 ex4_ERROR_WARNING_${batchtag}.rtf \
 $ex4zip \
 exwebconf.csv \
 wb4_${batchtag}_log.rtf \
 wb4_${batchtag}.out \
 nohup.out \
 | cpio -pvdum "$logdir"

#--------

exit 0