#!/bin/sh
#echo "Content-type: text/csv\n"
# set environment variables.
. /u/catcom/catcom_vars
OUTFILE="T21Eptc.csv"
if [ -n "$1" ]; then
OUTFILE="$1"
fi
###################################
#echo "
"
#echo "Process QUERY_STRING=$QUERY_STRING"
#echo ""
#for qtok in `echo "$QUERY_STRING" | tr "&" " "`
#do
# echo "qtok=[$qtok]"
# eval $qtok
#done
###################################
# set working dir
#rjs cd /u/catcom/t21web/webdirenq
###################################
# get lock
#LOCKFILE="/tmp/t21websql.lok"
#while true
#do
# if [ ! -f "$LOCKFILE" ]; then
# break;
# fi
# sleep 2
#done
#touch $LOCKFILE
###################################
tmpfile1="/tmp/webdirtmp$$.unl"
rm -f $tmpfile1
#rjs
runisql </dev/null 2>&1
database catcom;
unload to "$tmpfile1" DELIMITER ","
select dt.recordno, dc.surname, dc.firstname, dt.extension, dt.site, dc.section, dc.division, dc.title, dc.location, dt.extgroupunique, dc.email, dt.firstinit[1,9], dc.mobile, dc.faxno, dc.voicemail, dc.telephone
from directtemplate dt, directcustom dc
where dt.recordno = dc.recordno
and dt.secure_code = "Y"
UNION
select dt.recordno, dc.surname, dc.firstname, dt.extension, dt.site, dc.section, dc.division, dc.title, dc.location, dt.extgroupunique, dc.email, dt.firstinit[1,9], dc.mobile, dc.faxno, dc.voicemail, dc.telephone
from directtemplate dt, directcustom dc
where dt.recordno = dc.recordno
and dt.secure_code = "N" and dc.surname = ".INDIAL"
order by 2, 3, 4
SQLCMD0
#rjs
#cp T21Eptc_tmp.unl $tmpfile1
(
###############################################################
# csv header
echo "RecordNo,}1Surname_____________,}1Firstname______,#eXtension,Site,Section__________________,Div___,Title____________________,Location___,ServiceID,Email_________________________________,TEC___,#Mobile______,#Fax_Number__,Voicemail,#Telephone___"
# csv data
cat $tmpfile1 | sed -e 's/,$//'
#rjs
#rm -f $tmpfile1
###############################
# hit file
#
#hitsfile="hits"
#lasthit=`ls -altr $hitsfile | cut -c42-53`
#touch $hitsfile
#hits=`cat $hitsfile`
#if [ -z "$hits" ]; then
# hits="0"
#fi
#hits=`expr $hits + 1`
#echo "$hits" > $hitsfile
#echo "99999,~HITS,$hits,LAST $lasthit,,,,,,,"
###############################
# add Sync record at end
#echo "999999,ZZZZSync,,,,,,,,,"
###############################################################
) >"$OUTFILE"
###############################
# remove lock
#rm -f $LOCKFILE
###############################
exit 0