: !/bin/sh

tmpfile0="/tmp/moblist0.$$"
tmpfile1="/tmp/moblist1.$$"

rm -f cts1.noperson cts1.err cts1.log0 cts1.log1
rm -f cts1.0 cts1.1 cts1.2

runisql <<SQLCMD0
unload to "$tmpfile1" delimiter "|"
select dc.recordno, dc.surname, dc.firstname, dc.mobile
from directcustom dc
where dc.mobile is not null and dc.mobile != " "
order by 1
SQLCMD0

gettbsservice "$tmpfile0"

awk -F'|' -v"moblistfile=$tmpfile1" '
function trim(s)
{
	sub(/^[ \t]*/,"",s)
	sub(/[ \t]*$/,"",s)
	return s
}

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

function ld_moblist(moblistfile) {
	while ( (getline aline < moblistfile) > 0 ) {
		split(aline, a_arr, "|")
		#printf("aline=%s\n", aline)
		f = 0
		recordno = trim(a_arr[++f])
		surname = trim(a_arr[++f])
		firstname = trim(a_arr[++f])
		t21mobile = trim(a_arr[++f])

		t21name = firstname
		if ( t21name != "" && surname != "" )
			t21name = t21name " "
		t21name = t21name surname
		if ( t21name == "" )
			t21name = "_BLANK_"
		
		recordno_to_t21name_arr[recordno] = t21name
		t21name_to_recordno_arr[t21name] = recordno

		recordno_to_t21mobile_arr[recordno] = t21mobile
		t21mobile_to_recordno_arr[t21mobile] = recordno

		#print "ld_moblist: t21mobile_to_recordno_arr[" t21mobile "] = " t21mobile_to_recordno_arr[t21mobile]
	}
	close(moblistfile)
}

BEGIN {
	ld_moblist(moblistfile)
	bynamefoundcnt = 0
	bynamemiscnt = 0
	mobfoundcnt = 0
	mobmiscnt = 0
}

{
	#print $0

	f = 0
	ID = trim($++f)
	ServiceID = trim($++f)
	CustCent = trim($++f)
	CustDept = trim($++f)
	CustGrp = trim($++f)
	ShipTo = trim($++f)
	FullName = trim($++f)
	ServiceActive = trim($++f)
	Code = trim($++f)
	ServType = trim($++f)
	PersonID = trim($++f)
	Person = trim($++f)
	Location = trim($++f)
	System = trim($++f)
	Batchtype = trim($++f)
	InstallationDate = trim($++f)
	TerminationDate = trim($++f)

	tbsdirid = "T0"
	if ( PersonID != "" ) {
		# service assigned to a person
		tbsdirid = "T" PersonID
	}
	else {
		# service not assigned to a person
		print "ServiceID = [" ServiceID "]" " has blank PersonID" >"cts1.noperson"
		next
	}

	tbsname = Person
	if ( tbsname == "" ) {
		tbsname = "_TBLANK_"
		print "ServiceID = [" ServiceID "]" " has BLANK name in tPerson (PersonID=" PersonID ")" >"cts1.err"
	}

	t21dirid = "D0"
	recordno = t21name_to_recordno_arr[tbsname]
	if ( recordno == "" ) {
		++bynamemiscnt
		print "ServiceID=[" ServiceID "] name NOT in T21dir tbsname=[" tbsname "]" " PersonID= [" PersonID "]" >"cts1.log0"
	}
	else {
		++bynamefoundcnt
		t21dirid = "D" recordno
		print "ServiceID=[" ServiceID "] name FOUND in T21dir tbsname=[" tbsname "]" " t21dirid= [" t21dirid "]" >"cts1.log1"

	}


	#print "ServiceID = [" ServiceID "]"
	foundmobile = 0
	foundname = 0
	if ( substr(ServiceID,1,2) == "04" ) {
		++mobfoundcnt
		tbsmobile = substr(ServiceID,1,4) substr(ServiceID,6)

		recordno = t21mobile_to_recordno_arr[tbsmobile]
		if ( recordno == "" ) {
			recordno = t21name_to_recordno_arr[tbsname]
			if ( recordno == "" ) {
				++mobmiscnt
				print "ServiceID = [" ServiceID "]" " NOT in T21dir tbsname=[" tbsname "]" " (" tbsdirid ")" >"cts1.0"
			}
			else {
				foundname = 1
				t21dirid = "D" recordno
				t21name = recordno_to_t21name_arr[recordno]
				print "ServiceID = [" ServiceID "]" " name FOUND in T21dir tbsname=[" tbsname "]" " (" t21dirid ")" >"cts1.1"
			}
		}
		else {
			foundmobile = 1
			t21dirid = "D" recordno
			t21name = recordno_to_t21name_arr[recordno]
			print "ServiceID = [" ServiceID "]" " mobile FOUND in T21dir tbsname=[" tbsname "]" "  t21name=[" t21name "]" " (" t21dirid ")" >"cts1.2"
		}
	}
}

END {
	print "mobfoundcnt=" mobfoundcnt "  mobmiscnt=" mobmiscnt
}
' < "$tmpfile0"



rm -f "$tmpfile0"
rm -f "$tmpfile1"


zip -9 cts1.zip cts1*