: !/bin/sh


t21dircsvfile="indata/Sep_2008/T21E.csv"

#mobfile="indata/Sep_2008/TELMAX21/wb2_m11_082008.in"
mobfile="tbrss.out"

#10001|0400199895|TELSTRABRSS|CONFIG|COSTCENTRE|Aug 2008|||F0034|F & S STEVE HARRISON|||0|0.00|
#10001|0400199895|TELSTRABRSS|CONFIG|NAME|Aug 2008||||F & S STEVE HARRISON||1029676598|0|0.00|



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

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



function ld_t21dircsvfile(t21dircsvfile) {
	printf("ld_t21dircsvfile(%s)\n", t21dircsvfile)
	system("date")

	# test 3032,ZVARGULIS,MARIS,8685,MATHS RW

	#dt.recordno, dc.surname, dc.firstname, dt.extension, dc.title,
	# dc.location, dt.extgroupunique, dc.email,
	# dc.fax, dc.telephone,
	# dc.division_college, dc.department_school

	t21dirreccnt = -1
	while ( (getline aline < t21dircsvfile) > 0 ) {
		++t21dirreccnt
		split(aline, a_arr, ",")
		#printf("aline=%s\n", aline)
		##printf("a_arr 1=%s 2=%s 3=%s 4=%s 5=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4], a_arr[5])
		recordno = trim(a_arr[1])
		t21surname = trim(a_arr[2])
		t21firstname = trim(a_arr[3])
		extn = clip(a_arr[4])
		title = clip(a_arr[5])
		t21campus = clip(a_arr[6])
		t21location = trim(a_arr[7])
		extgroupunique = clip(a_arr[8])
		email = clip(a_arr[9])
		t21mobile = clip(a_arr[10])
		t21mobile_carrier = clip(a_arr[11])
		fax = clip(a_arr[12])
		telephone = clip(a_arr[13])
		t21division_college = trim(a_arr[14])
		t21department_school = trim(a_arr[15])
		empid = trim(a_arr[16])
		master = trim(a_arr[17])

		# for mk_dircsvfile()
		#print " recordno = [" recordno "]"
		recordno_to_aline_arr[recordno] = aline
		#print "   recordno_to_aline_arr[" recordno "] = [" recordno_to_aline_arr[recordno] "]"
		t21dirreccnt_to_recordno_arr[t21dirreccnt] = recordno
		#print "   t21dirreccnt_to_recordno_arr[" t21dirreccnt "] = [" t21dirreccnt_to_recordno_arr[t21dirreccnt] "]"

		recordno_to_master_arr[recordno] = master
		#print "   recordno_to_master_arr[" recordno "] = [" recordno_to_master_arr[recordno] "]"


		fullname = t21firstname
		if ( fullname != "" && t21surname != "" )
			fullname = fullname " "
		fullname = fullname t21surname
		fullname_to_recordno_arr[fullname] = recordno
		print "fullname_to_recordno_arr[" fullname "] = [" recordno "]"

		# firstname initial only
		iname = substr(t21firstname,1,1)
		if ( iname != "" && t21surname != "" )
			iname = iname " "
		iname = iname t21surname
		fullname_to_recordno_arr[iname] = recordno
		print "fullname_to_recordno_arr[" iname "] = [" recordno "]"


		if ( t21surname == "" )
			t21surname = "_blank_"
		if ( t21firstname == "" )
			t21firstname = "_blank_"

		if ( t21campus == "" )
			t21campus = "_blank_"

		if ( t21location == "" )
			t21location = "_blank_"

		if ( t21division_college == "" )
			t21division_college = "_blank_"

		if ( t21department_school_college == "" )
			t21department_school_college = "_blank_"

		gsub(" ","",t21mobile)
		gsub("-","",t21mobile)

		printf(" - extgroupunique = [%s]  extn = [%s]\n", extgroupunique, extn)
		#printf("   t21surname = [%s]  t21firstname = [%s]\n", t21surname, t21firstname)
		#printf("   t21campus = [%s]  t21location = [%s]\n", t21campus, t21location)
		#printf("   t21division_college  = [%s]\n", t21division_college)
		#printf("   t21department_school = [%s]\n", t21department_school)

		t21name = t21surname
		if ( t21firstname != "_blank_")
			t21name = sprintf("%s %s", t21firstname, t21name)
		recordno_to_t21name_arr[recordno] = t21name
		#print "      recordno_to_t21name_arr[" recordno "] = [" recordno_to_t21name_arr[recordno] "]"

		recordno_to_empid_arr[recordno] = empid
		#print "      recordno_to_empid_arr[" recordno "] = [" recordno_to_empid_arr[recordno] "]"

		recordno_to_extn_arr[recordno] = extn
		recordno_to_eggrid_arr[recordno] = extgroupunique
		recordno_to_t21campus_arr[recordno] = t21campus
		recordno_to_t21location_arr[recordno] = t21location

		#print "      recordno_to_extn_arr[" recordno "] = [" recordno_to_extn_arr[recordno] "]"
		#print "      recordno_to_eggrid_arr[" recordno "] = [" recordno_to_eggrid_arr[recordno] "]"

		#itag = extgroupunique "|" extn
		itag = extn
		#printf(" itag = [%s]\n", itag)

		if ( master == "Y" ) {
			if ( dirextn_to_t21name_arr[itag] == "" )
				dirextn_to_t21name_arr[itag] = t21name
			if ( dirextn_to_t21name_arr[itag] != t21name )
				dirextn_to_t21name_arr[itag] = "Multiple"
			#print "    dirextn_to_t21name_arr[" itag "] = [" dirextn_to_t21name_arr[itag] "]"

			if ( dirextn_to_t21surname_arr[itag] == "" )
				dirextn_to_t21surname_arr[itag] = t21surname
			if ( dirextn_to_t21surname_arr[itag] != t21surname )
				dirextn_to_t21surname_arr[itag] = "Multiple"

			if ( dirextn_to_t21firstname_arr[itag] == "" )
				dirextn_to_t21firstname_arr[itag] = t21firstname
			if ( dirextn_to_t21firstname_arr[itag] != t21firstname )
				dirextn_to_t21firstname_arr[itag] = "Multiple"

			if ( dirextn_to_t21campus_arr[itag] == "" )
				dirextn_to_t21campus_arr[itag] = t21campus
			if ( dirextn_to_t21campus_arr[itag] != t21campus )
				dirextn_to_t21campus_arr[itag] = "Multiple"

			if ( dirextn_to_t21location_arr[itag] == "" )
				dirextn_to_t21location_arr[itag] = t21location
			if ( dirextn_to_t21location_arr[itag] != t21location )
				dirextn_to_t21location_arr[itag] = "Multiple"

			if ( dirextn_to_t21division_college_arr[itag] == "" )
				dirextn_to_t21division_college_arr[itag] = t21division_college
			if ( dirextn_to_t21division_college_arr[itag] != t21division_college )
				dirextn_to_t21division_college_arr[itag] = "Multiple"

			if ( dirextn_to_t21department_school_arr[itag] == "" )
				dirextn_to_t21department_school_arr[itag] = t21department_school
			if ( dirextn_to_t21department_school_arr[itag] != t21department_school )
				dirextn_to_t21department_school_arr[itag] = "Multiple"
			dirextn_to_eggrid_arr[itag] = extgroupunique
		}

		if ( egrid_to_t21campus_arr[extgroupunique] == "" )
			egrid_to_t21campus_arr[extgroupunique] = t21campus
		if ( egrid_to_t21campus_arr[extgroupunique] != t21campus )
			egrid_to_t21campus_arr[extgroupunique] = "Multiple"

		extn_to_egridlist_arr[extn] = extn_to_egridlist_arr[extn] "|" extgroupunique
	}
	close(t21dircsvfile)
}



#---------------------------------------------
BEGIN {
	ld_t21dircsvfile(t21dircsvfile)
}


{
	if ( $4 != "CONFIG" )
		next
	if ( $5 == "COSTCENTRE" ) {
		mobile = $2
		cc = $9
		ccdesc = $10
		print $5 ": mobile=[" mobile "]" "  cc=[" cc "]" "  ccdesc=[" ccdesc "]"
	}
	if ( $5 == "NAME" ) {
		++mobcount
		mobile = $2
		fullname = $10
		print $5 ": mobile=[" mobile "]" "  fullname=[" fullname "]"

		if ( fullname == "" ) {
			++mobblankcount
			print "                    " "  *** NOTFOUND blank ***"
			next
		}

		name = fullname
		recordno = fullname_to_recordno_arr[name]
		if ( recordno != "" ) {
			print "                    " "  *** NOTFOUND fullname ***"
			++mobfoundcount
			next
		}

		n = split(fullname, name_arr, " ")

		name = ""
		offset = 1
		for ( i = 1; i <= (n - offset); ++i ) {
			if ( i > 1 )
				name = name " "
			name = name name_arr[i]
		}
		gsub(",", "", name)
		recordno = fullname_to_recordno_arr[name]
		if ( recordno != "" ) {
			print "                    " " *** FOUND ***" " 1 " n "-" offset "  name=[" name "]" "  recordno=[" recordno "]"
			++mobfoundcount
			next
		}


		print "                    " "  *** NOTFOUND ***"

	}
}

END {
	print ""
	print "     mobcount = " mobcount
	print "mobblankcount = " mobblankcount
	print "mobfoundcount = " mobfoundcount
}

' < "$mobfile" >$0.log