: !bin/sh
########################################################################
# T21BATCHNAME rseedistid
# T21BATCHDESC Send/Resend Email Web Reports

# ALLOWBLANKDATES false
# CUSTOMDESC Enter Override email address
# CUSTOMDEF
# T21BATCHVAR1DESC Do Even If Already Sent before or Dont Send (Y/N)
# T21BATCHVAR1VALU N
# T21BATCHVAR2DESC Enter distid ID to email, or list file (Blank for ALL)
# T21BATCHVAR2VALU xxxx
# T21BATCHVAR3DESC Enter number of emails before pausing (0 no pause)
# T21BATCHVAR3VALU DEFAULT
# T21BATCHVAR4DESC Enter pause time in minutes
# T21BATCHVAR4VALU DEFAULT
# T21BATCHVAR5DESC Do PERSONAL
# T21BATCHVAR5VALU N
# T21BATCHENDARGS
########################################################################

workdir="/u/catcom/wb/ex4"
# Testing
#workdir="/u/catcom/wb/ex4_tst"
#workdir="."

. /u/catcom/catcom_vars

. /u/catcom/scripts/stdsqlheader

# get todays date
today=`date "+%d/%m/%Y"`

# for when invoked from wbt_dorelease.cgi
HOME="/u/catcom/wb"
export HOME
LOGNAME="bilmax21"
export LOGNAME
umask 002

#-----------------------------------------------------------------
SDATE=$1; EDATE=$2; STIME=$3; ETIME=$4; SITEID=$5

DEFMAXKSIZE="6000"
if [ -z "$WBEMAILMAXKSIZE" ]; then
	maxKsize="$DEFMAXKSIZE"
else
	maxKsize="$WBEMAILMAXKSIZE"
fi

# custom param to specifiy only one distid to email
# override email addres
if [ -n "$6" ]; then
	override_email="$6"
else
	override_email=""
fi

# send even if Already sent
if [ "$override_email" != "" -o "$T21BATCHVAR1VALU" = "y" -o "$T21BATCHVAR1VALU" = "Y" ]; then
	ALWAYS_SEND="Y"
else
	ALWAYS_SEND="N"
fi

# only one distid, or distid list file name to email
onlythisdistid=""
distidlistfile=""
if [ "$T21BATCHVAR2VALU" != "" ]; then
	# check if its a list file
	if [ -f "$T21BATCHVAR2VALU" ]; then
		distidlistfile="$T21BATCHVAR2VALU"
	else
		onlythisdistid="$T21BATCHVAR2VALU"
	fi
fi
if [ "$onlythisdistid" = "ALL" ]; then
	onlythisdistid=""
	distidlistfile=""
fi

# specifiy email count before pause
if [ "$T21BATCHVAR3VALU" != "" ]; then
	emailsbeforepause="$T21BATCHVAR3VALU"
else
	emailsbeforepause="0"
fi
if [ "$emailsbeforepause" = "ALL" ]; then
	emailsbeforepause="0"
fi
if [ "$emailsbeforepause" = "DEFAULT" ]; then
	emailsbeforepause="$WBEMAILSBEFOREPAUSE"
fi

# specifiy pause in minutes
if [ "$T21BATCHVAR4VALU" != "" ]; then
	pauseinminutes="$T21BATCHVAR4VALU"
else
	pauseinminutes="0"
fi
if [ "$pauseinminutes" = "ALL" ]; then
	pauseinminutes="0"
fi
if [ "$pauseinminutes" = "DEFAULT" ]; then
	pauseinminutes="$WBEMAILPAUSEINMINUTES"
fi

# do PERSONAL distid's
if [ "$T21BATCHVAR5VALU" = "y" -o "$T21BATCHVAR5VALU" = "Y" ]; then
	DO_PERSONAL="Y"
else
	DO_PERSONAL="N"
fi

############
## Testing
#SDATE="01/11/2009"
#EDATE="30/11/2009"
#SITEID="ALL"
#ALWAYS_SEND="Y"
###onlythisdistid="1266"
#onlythisdistid="4380"
#distidlistfile="/u/catcom/wb/ex4_tst/reldistidlist.csv"
#override_email="demo"
#override_email="rod@transmit.com.au"
#override_email="support@transmit.com.au"
#emailsbeforepause="$WBEMAILSBEFOREPAUSE"
#pauseinminutes="$WBEMAILPAUSEINMINUTES"
#DO_PERSONAL="Y"

#-----------------------------------------------------------------

email_file()
{
	echo "email_file() attfiles=[$attfiles]  extratxtfiles=[$extratxtfiles]  subject=[$subject]  emaillist=[$emaillist]"
	for emailaddr in $emaillist
	do
		realemail=$emailaddr
		echo "realemail=[$realemail]"
		if [ "$override_email" != "" ]; then
			emailaddr=$override_email
		fi
		echo "Sending to emailaddr=[$emailaddr]"

		# Force mail to pass DATE as GMT
		TZ=''; export TZ
		(
			#echo "Please Find Attached Information
"
			if [ -n "$toptxtfile" ]; then
				cat $toptxtfile
				echo "
"	
			fi
			if [ -n "$extra_emtxt" ]; then
				echo "$extra_emtxt"
				echo "###############################################
"
				echo "
"	
			fi
			for extraf in $extratxtfiles
			do
				if [ -n "$extraf" ]; then
					cat $extraf
					echo "
"	
				fi
			done
			if [ -n "$bottomtxtfile" ]; then
				cat $bottomtxtfile
				echo "
"	
			fi
			for attf in $attfiles
			do
				uuencode $attf `basename $attf`

				## Testing
				#echo ""
				#echo "-------------------------------------"
				#echo "Email for userid=$userid"
				#echo "        recordno=$wbdconf_recordno"
				#echo "          distid=$wbdconf_distid"
				#echo "  fileattachment=$attf"
				#echo "        zipKsize=$zipKsize"
				#echo "-------------------------------------"
				#echo ""
			done
		) | tr -d "\015" | mail -s "$subject" $emailaddr
		#) >../em_${userid}_${distid}_${monthdir}.eml
	done
}


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

do_email_zip()
{
	echo "Do email zip for $distiddir to $email"
	echo ""

	echo "size under topdat/$distiddir"
	du -k topdat/$distiddir | awk -v "distiddir=topdat/$distiddir" '{if ($2 == distiddir) print $0;}'
	echo ""

	echo "contents of topdat/$distiddir"
	ls "topdat/$distiddir"
	echo "-------"
	echo ""

	echo "topdat/$fnamelistfile ..."
	cat topdat/$fnamelistfile
	echo "-------"
	echo ""

	# create image in zipdat dir
	rm -f -r zipdat/topdat

	# copy distid fnamelist files
	cat topdat/$fnamelistfile | cpio -pvdum zipdat 2>&1 1>/dev/null

	# copy common nav files
	find topdat/common/nav -depth -print | grep -v ".js" | cpio -pvdum zipdat 2>&1 1>/dev/null

	# copy doc files
	find topdat/doc -depth -print | cpio -pvdum zipdat 2>&1 1>/dev/null

	# copy and rename user index html file
	cp img/xuser.html "zipdat/topdat/$userhtmlfile"

	# remove symlink copy disclaimer file
	rm -f "zipdat/topdat/doc/disclaimer.html"
	cp img/disclaimer.html "zipdat/topdat/doc/disclaimer.html"

	cd zipdat

	zipfile="topdat_${distid}_${monthdir}.zip"
	rm -f $zipfile
	if [ -n "$WBZIPPWD" ]; then
		#zip -r -9 $zipfile topdat 2>&1 1>/dev/null
		#t21zipcloak -P "$WBZIPPWD" $zipfile
		zip -P "$WBZIPPWD" -e -r -9 $zipfile topdat 2>&1 1>/dev/null
	else
		zip -r -9 $zipfile topdat 2>&1 1>/dev/null
	fi
	echo "size of zip"
	zipKsize=`du -k $zipfile | awk '{print $1}'`
	echo "zipKsize = $zipKsize"

	if [ $zipKsize -ge $maxKsize ]; then
		cd ..
		echo "TOO BIG!"
		do_email_zip_too_big
		return
	fi

	echo "Sending ZIP."

	attfiles="$zipfile"
	toptxtfile=""
	extra_emtxt=""
	if [ "$inp_emtxt_opt" = "prepend" -o "$inp_emtxt_opt" = "replace" ]; then
		extra_emtxt="$inp_emtxt"
	fi
	if [ "$inp_emtxt_opt" != "replace" ]; then
		extratxtfiles="../topdat/$idfile ../emtxt_email_reports.txt"
	fi
	bottomtxtfile="../emtxt_disclaimer.txt"
	if [ "$userid" != "$distid" ]; then
		idtext="$userid $distid"
	else
		idtext="$distid"
	fi
	subject="${subjectprefix}Bilmax21 Reports for $idtext $monthtag"
	emaillist="$email"
	email_file

	cd ..
}


do_email_zip_too_big()
{
	cd zipdat
	attfiles=""
	toptxtfile=""
	extra_emtxt=""
	if [ "$inp_emtxt_opt" = "prepend" -o "$inp_emtxt_opt" = "replace" ]; then
		extra_emtxt="$inp_emtxt"
	fi
	if [ "$inp_emtxt_opt" != "replace" ]; then
		extratxtfiles="../topdat/$idfile ../emtxt_moveto_online.txt"
	fi
	bottomtxtfile="../emtxt_disclaimer.txt"
	if [ "$userid" != "$distid" ]; then
		idtext="$userid $distid"
	else
		idtext="$distid"
	fi
	subject="${subjectprefix}Bilmax21 Reports for $idtext $monthtag"
	emaillist="$email"
	email_file
	cd ..
}


do_email_web()
{
	cd zipdat

	attfiles=""

	#attfbase=`basename $idfile .txt`
	#echo "attfbase=[$attfbase]"
	#attf1="/tmp/${attfbase}.rtf"
	#echo "attf1=[$attf1]"
	#cp "../topdat/$idfile" "$attf1"
	#attfiles="$attf1"

	toptxtfile="../emtxt_online_top.txt"
	extra_emtxt=""
	if [ "$inp_emtxt_opt" = "prepend" -o "$inp_emtxt_opt" = "replace" ]; then
		extra_emtxt="$inp_emtxt"
	fi
	if [ "$inp_emtxt_opt" != "replace" ]; then
		##extratxtfiles="../topdat/$idfile ../emtxt_always_online.txt"
		#extratxtfiles="../emtxt_always_online.txt"
		extratxtfiles="../emtxt_always_online.txt ../topdat/$idfile"
	fi
	bottomtxtfile="../emtxt_disclaimer.txt"
	if [ "$userid" != "$distid" ]; then
		idtext="$userid $distid"
	else
		idtext="$distid"
	fi
	subject="${subjectprefix}Bilmax21 Reports for $idtext $monthtag"
	emaillist="$email"
	email_file

	#rm -f "$attf1"

	cd ..
}

#----------------------------------------

do_set_lastsentdate()
{
    today="$1"
    awk -v"distidlistfile=$distidlistfile" \
        -v"onlythisdistid=$onlythisdistid" \
        -v"alwayssend=$ALWAYS_SEND" \
	-v"curddmmyyyy=$today" \
	-v"monthdir=$billperiod" '
    function trim(s)
    {
	sub(/^[ \t]*/,"",s)
	sub(/[ \t]*$/,"",s)
	return s
    }

    function clip(s)
    {
	sub(/[ \t]*$/,"",s)
	return s
    }

    function ld_distidlist(fname) {
	#printf("ld_distidlist(%s)\n", fname)
	while ( (getline aline < fname) > 0 ) {
		#print "aline=[" aline "]"
		split(aline, a_arr, ",")
		f = 0
		reldistid = trim(a_arr[++f])
		reldistid_arr[reldistid] = 1
		#print "reldistid_arr[" reldistid "] = 1"
	}
	close(fname)
    }

    BEGIN {
	if ( distidlistfile != "" )
		ld_distidlist(distidlistfile)
	if ( onlythisdistid != "" )
		reldistid_arr[onlythisdistid] = 1

	wbttop_fname = sprintf("topdat/%s/data/wbttop.csv", monthdir);
	print "check wbttop_fname=[" wbttop_fname "]"
	save_wbttop_fname = sprintf("topdat/%s/data/orig_wbttop.csv", monthdir)
	# if not already saved, save original wbttop csv file
	if ( (getline x < save_wbttop_fname) < 0 ) {
		system("cp " wbttop_fname " " save_wbttop_fname)
	}
	close(save_wbttop_fname)
	tmp_wbttop_fname = sprintf("/tmp/tmp_rse_%s_wbttop.csv", monthdir)
	countsent = 0
	while ( (getline wbttopline < wbttop_fname) > 0 ) {
		split(wbttopline, wbttop_arr, ",")
		#print "wbttopline = " wbttopline
		f = 0
		filedesc = clip(wbttop_arr[++f]);
		exbilperiod = clip(wbttop_arr[++f]);
		sdate = clip(wbttop_arr[++f]);
		edate = clip(wbttop_arr[++f]);
		useriddesc = clip(wbttop_arr[++f]);
		t21userid = clip(wbttop_arr[++f]);
		DISTIDdesc = clip(wbttop_arr[++f]);
		DISTID = clip(wbttop_arr[++f]);
		t21wbd_method = clip(wbttop_arr[++f]);
		t21wbd_type = clip(wbttop_arr[++f]);
		bm_releasedate = clip(wbttop_arr[++f]);
		bm_regendate = clip(wbttop_arr[++f]);
		bm_lastsentdate = clip(wbttop_arr[++f]);

		if ( filedesc != "User List" ) { # not header
			if ( reldistid_arr[DISTID] == 1 ) {
				# update lastsent date
				print "SET lastsentdate in monthdir=" monthdir " for DISTID=[" DISTID "]" " to " curddmmyyyy
				if (bm_lastsentdate == "" || alwayssend == "Y")
					bm_lastsentdate = curddmmyyyy
				wbttopline = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", filedesc, exbilperiod, sdate, edate, useriddesc, t21userid, DISTIDdesc, DISTID, t21wbd_method, t21wbd_type, bm_releasedate, bm_regendate, bm_lastsentdate)
				++countsent
			}
		}

		printf("%s\n", wbttopline) >>tmp_wbttop_fname
	}
	close(wbttop_fname)

	close(tmp_wbttop_fname)
	if ( countsent > 0 ) {
		# replace wbttop with released one
		system("rtrun cp " tmp_wbttop_fname " " wbttop_fname)
		#print "-------- NEW " tmp_wbttop_fname "--------"
		#system("cat " tmp_wbttop_fname)
		#print "-----------------------------------------------"
	}
	system("rm " tmp_wbttop_fname)
	exit 0
    }'
}


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

tmpwfile="t21WBDL_top_$$.csv"
workfile="/tmp/${tmpwfile}"
# make sure we are here
cd $workdir

tmpfile="/tmp/emailex4distid$$"
rm -f $tmpfile

## Testing
exec >$tmpfile 2>&1

echo "$0: SDATE=[$SDATE] EDATE=[$EDATE]"

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

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

echo "--------------------------------------------------------------------"
date
echo "override_email=[$override_email]"
echo "ALWAYS_SEND=[$ALWAYS_SEND]"
if [ "$onlythisdistid" != "" ]; then
	echo "Only for distid -- $onlythisdistid"
else
	if [ "$distidlistfile" != "" ]; then
		echo "For distid's listed in file -- $distidlistfile"
	else
		echo "All Configured distid as at last wb4a run"
	fi
fi

echo "emailsbeforepause = $emailsbeforepause"
echo "   pauseinminutes = $pauseinminutes"

echo

## make sure we are here
#cd ${workdir}/topdat

# remove zip files and dir
#rm -rf zipdat
#mkdir zipdat

##########################
# create working file
rm -f "$workfile"
echo "Creating T21 WBDL work file: $workfile" 
##
##gett21WBDL "$SDATE" "$EDATE" STIME ETIME "$SITEID" "$tmpwfile"
##cp "$tmpwfile" "$workfile"
##rm -f "$tmpwfile"
##
#t21WBDLfile="t21WBDL.unl"
#t21WBDLdir="indata/${billperiod}"
#echo "Copying ${t21WBDLdir}/${t21WBDLfile} to $workfile"
#cp "${t21WBDLdir}/${t21WBDLfile}" "$workfile"

t21WBDLfile="wbttop.csv"
t21WBDLdir="topdat/${billperiod}/data"
echo "Copying ${t21WBDLdir}/${t21WBDLfile} to $workfile"
cp "${t21WBDLdir}/${t21WBDLfile}" "$workfile"

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

#last_distid=""
emailssent="0"

# wbttop
#User List,September 2006,SDATE,EDATE,UserID_DESC,UserID,DistID_DESC,DistID,BDMethod,BDType
#,,,,TOM MILLAR,9030048,Personal Reports for TOM MILLAR,9030048,WEB,PERSONAL


cat $workfile | \
while read aline
do
	#echo "aline=[$aline]"

	eval `echo "$aline" | awk -F',' '{ printf(" fld1=\"%s\"; useriddesc=\"%s\"; userid=\"%s\"; distiddesc=\"%s\"; distid=\"%s\"; wbd_method=\"%s\"; wbd_type=\"%s\"; ", $1, $5, $6, $7, $8, $9, $10) }' `

	if [ -n "$fld1" ]; then
		if [ "$fld1" = "User List" ]; then	# skip header
			#echo "SKIP Header=[$aline]"
			continue
		fi
	fi

	## if done already (ie.skip multiple grids)
	#if [ "$last_distid" = "$distid" ]; then
	#	last_distid="$distid"
	#	continue
	#fi

	if [ -n "$onlythisdistid" -a "$onlythisdistid" != "$distid" ]; then
		#echo "SKIP distid = $distid"
		#echo ""
		#last_distid="$distid"
		continue
	fi

	indistidlist="0"
	if [ -n "$distidlistfile" ]; then
		if grep "^${distid}$" "$distidlistfile" > /dev/null
		then	# in list file
			#echo "Do in list file distid = $distid"
			indistidlist="1"
		else	# not in list file
			#echo "SKIP (not in list file) distid = $distid"
			continue
		fi
	fi

	# force upper case
	wbd_type=`echo "$wbd_type" | awk '{print toupper($1)}'`

	recordno=""
	lastdist=""
	lastdistdate=""
	altemail=""
	altemailyn="N"
	if [ $wbd_type = "PERSONAL" ];then 
		if [ "$DO_PERSONAL" = "N" ]; then
			#echo "SKIP distid = $distid  wbd_type = $wbd_type"
			continue
		fi
		#echo "DO distid = $distid  wbd_type = $wbd_type"
	else
		# assume STANDARD
		#echo "DO distid = $distid  wbd_type = $wbd_type"
		# get email address from current wbdconf DB table
		#sqlmy_status "Finding altemail for distid=$distid"
		procsql ":" "select unique wbdconf.recordno, wbdconf.lastdist, wbdconf.lastdistdate, wbdconf.altemail, wbdconf.altemailyn
 from wbdconf where distid = \"$distid\""
		#echo "nrows=$nrows"
		#echo "recordno = [$recordno]"
		#echo "altemail = [$altemail]"
		#echo "altemailyn = [$altemailyn]"

		# force upper case
		altemailyn=`echo "$altemailyn" | awk '{print toupper($1)}'`
		if [ "$altemailyn" = "" -o "$altemail" = "" ]; then
			altemailyn="N"
		fi
	fi

	# try to get email addres from current directcustom DB table
	#echo "recordno = [$recordno]"
	if [ "$recordno" != "" ]; then
		# use recordno from wbdconf (STANDARD)
		where_bit="where directcustom.recordno = $recordno"
	else
		# use userid from wbttop csv  to lookup StaffID (PERSONAL)
		where_bit="where directcustom.$WB_USERNAME_DIRCUST_FIELD = \"$userid\""
	fi 
		
	if [ "$wbd_type" = "PERSONAL" ]; then
		where_bit="$where_bit and directcustom.recordno in (select directoryid from sidconf)"
	fi

	#sqlmy_status "Finding directcustom_email for distid=$distid"
	#sqlmy_status "      where_bit=$where_bit"
	directcustom_email=""
	procsql ":" "select unique directcustom.recordno, directcustom.email directcustom_email from directcustom $where_bit"
	#echo "nrows=$nrows"
	#echo "recordno = [$recordno]"
	#echo "directcustom_email = [$directcustom_email]"

	email="$directcustom_email"
	if [ "$altemailyn" = "Y" -a "$altemail" != "" ]; then
		email="$altemail"
	fi

	# force upper case
	wbd_method=`echo "$wbd_method" | awk '{print toupper($1)}'`
	if [ "$wbd_method" = "EMAIL" ]; then
		emailyn="Y"
	else
		emailyn="N"
	fi
	if [ "$wbd_method" = "WEB" ]; then
		webyn="Y"
	else
		webyn="N"
	fi

	if [ -z "$userid" ]; then
	#	if [ -z "$recordno" ]; then
	#		echo "ERROR: NO userid or recordno for distid=$distid"
	#		last_distid="$distid"
	#		continue
	#	fi
	#	echo "WARNING: NO userid - using recorno=$recordno for distid=$distid"
	#	userid="$recordno"
	#else
	#	userid="$userid"
		echo "ERROR: NO userid for distid=$distid"
		echo ""
		continue
	fi

	monthdir="$billperiod"
	if [ ! -d "topdat/$monthdir" ]; then
		echo "ERROR: NO data for monthdir = $monthdir"
		#last_distid="$distid"
		continue
	fi

	distiddir="$billperiod/$distid"
	if [ ! -d "topdat/$distiddir" ]; then
		echo "ERROR: NO data for distiddir = $distiddir"
		#last_distid="$distid"
		continue
	fi

	subjectprefix="" 

	# if not set to email OR web
	if [ "$emailyn" != "Y" -a "$webyn" != "Y" ]; then
		echo "ERROR: Distribution List distid = $distid is NOT SET for EMAIL OR WEB"
		#last_distid="$distid"
		continue
	fi

	if [ -z "$email" ]; then
		echo "ERROR: NO email address for $distid"
		#last_distid="$distid"
		#continue
		email="$WBADMINEMAIL"	
		subjectprefix="No Email Address for - " 
	fi

	echo ""
	echo "DO..."
	echo "    recordno = $recordno"
	echo "      distid = $distid"
	echo "  distiddesc = $distiddesc"
	echo "    lastdist = $lastdist"
	echo "lastdistdate = $lastdistdate"
	echo "    altemail = $altemail"
	echo "  altemailyn = $altemailyn"
	echo "  wbd_method = $wbd_method"
	echo "    wbd_type = $wbd_type"
	echo "      userid = $userid"
	echo "  directcustom_email = $directcustom_email"
	echo "monthdir=$monthdir"
	echo "distiddir=$distiddir"
	echo "emailyn=$emailyn"
	echo "webyn=$webyn"

	# get username
	t21name=`grep "^$recordno," "indata/${billperiod}/T21E.csv" | awk -F',' '{ t21name = $3; if (t21name != "") t21name = t21name " "; t21name = t21name $2; print t21name; }'`
	echo "t21name=[$t21name]"

	idfile="${distiddir}/${distid}_${billperiod}.txt"
	fnamelistfile="${distiddir}/fnamelist.csv"
	userhtmlfile="${userid} ($t21name) Click here to run your reports.html"
	monthtag=`echo "$billperiod" | tr ' ' '-'` 
	idtag="$distid $monthtag"
	echo "idfile=$idfile idtag=$idtag monthtag=$monthtag"
	echo "fnamelistfile=$fnamelistfile"
	echo "userhtmlfile=$userhtmlfile"

	if [ "$lastdist" = "$monthtag" ]; then
		if [ "$ALWAYS_SEND" != "Y" ]; then
			echo "WARNING: $distid Already sent"
			#last_distid="$distid"
			continue
		fi
	fi


	if [ "$emailyn" = "Y" ]; then
		do_email_zip
	fi

	if [ "$webyn" = "Y" ]; then
		do_email_web
	fi

	########################################
	# remove workdir
	rm -f -r zipdat/topdat

	###############
	## Testing
	#echo "Testing - should update lastdist for $distid"
	#last_distid="$distid"
	#echo ""
	#continue

	#if [ "$ALWAYS_SEND" = "Y" ]; then
	#	#last_distid="$distid"
	#	echo "Resend - SKIP update lastdist"
	#	echo ""
	#else
		echo "Before update lastdist wbd_type=[$wbd_type]"
		# update lastdist
		case $wbd_type in
		[STANDARD|INVOICE_PDF|ADMIN|BILMAX21|WBDM_DIST_ADMIN|WBDM|DISTID_ADMIN|WBDM_LIST_ADMIN|WBDM_USER_ADMIN])
			## Testing
			#echo "WOULD: update wbdconf \
			#	set lastdist = \"$monthtag\", \
			#	    lastdistdate = \"$today\" \
			#	where distid = \"$distid\""
			result=`echo "update wbdconf \
				set lastdist = \"$monthtag\", \
				    lastdistdate = \"$today\" \
				where distid = \"$distid\"" | \
					runisql 2>&1 | grep -c "1 row(s) updated"`
			echo "update lastdist for $distid result = [$result]"
			if [ $result -ne 1 ]; then
				echo "ERROR update lastdist: distid $distid"
				#exit 1
				continue
			fi
			;;
		esac
	#fi


	########################################
	emailssent=`expr $emailssent + 1`
	echo "emailssent = $emailssent  emailsbeforepause = $emailsbeforepause"

	if [ $emailsbeforepause -gt 0 ]; then
		moda=`expr $emailssent % $emailsbeforepause`
		if [ $moda -eq 0 ]; then
			if [ $pauseinminutes -gt 0 ]; then
				pauseinseconds=`expr $pauseinminutes \* 60`
				echo "PAUSING... pauseinminutes = $pauseinminutes  pauseinseconds = $pauseinseconds"
				#echo "should: sleep $pauseinseconds"
				sleep $pauseinseconds
			fi
		fi
	fi

	#last_distid="$distid"
	echo
done

###########################################
# update lastsentdate in wbttopcsv
do_set_lastsentdate "$today"

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

echo ""
echo ""
echo ""
echo ""

#######################################################
# save log
## Testing
cat $tmpfile >>${workdir}/emex4_${billperiod}.log

#output file to user
## Testing
cat $tmpfile | pr_rep system emex4_

rm -f $workfile
rm -f $tmpfile

exit 0