#!/sbin/ksh


#####
# Clean out ALL g_Master_Font_* Dif entries
#####
clean_master_fonts()
{

	cd /twc/bin

	echo "  DELETE g_Master_Font_0001; DELETE g_Master_Font_0002; DELETE g_Master_Font_0003;
		DELETE g_Master_Font_0004; DELETE g_Master_Font_0005; DELETE g_Master_Font_0006;
		DELETE g_Master_Font_0007; DELETE g_Master_Font_0008; DELETE g_Master_Font_0009;
		DELETE g_Master_Font_0010; DELETE g_Master_Font_0011; DELETE g_Master_Font_0012;
		DELETE g_Master_Font_0013; DELETE g_Master_Font_0014; DELETE g_Master_Font_0015;
		DELETE g_Master_Font_0016; DELETE g_Master_Font_0017; DELETE g_Master_Font_0018;
		DELETE g_Master_Font_0019; DELETE g_Master_Font_0020; DELETE g_Master_Font_0021; 
		DELETE g_Master_Font_0022; DELETE g_Master_Font_0023; DELETE g_Master_Font_0024; 
		DELETE g_Master_Font_0025; DELETE g_Master_Font_0026; DELETE g_Master_Font_0027; 
		DELETE g_Master_Font_0028; DELETE g_Master_Font_0029; DELETE g_Master_Font_0030; 
		DELETE g_Master_Font_0031; DELETE g_Master_Font_0032; DELETE g_Master_Font_0033; 
		DELETE g_Master_Font_0034; DELETE g_Master_Font_0035; DELETE g_Master_Font_0036; 
		DELETE g_Master_Font_0037; DELETE g_Master_Font_0038; DELETE g_Master_Font_0039; 
		DELETE g_Master_Font_0040; DELETE g_Master_Font_0041; DELETE g_Master_Font_0042; 
		DELETE g_Master_Font_0043; DELETE g_Master_Font_0044; DELETE g_Master_Font_0045; 
		DELETE g_Master_Font_0046; DELETE g_Master_Font_0047; DELETE g_Master_Font_0048; 
		DELETE g_Master_Font_0049; DELETE g_Master_Font_0050; DELETE g_Master_Font_0051; 
		DELETE g_Master_Font_0052; DELETE g_Master_Font_0053; DELETE g_Master_Font_0054; 
		DELETE g_Master_Font_0055; DELETE g_Master_Font_0056; DELETE g_Master_Font_0057; 
		DELETE g_Master_Font_0058; DELETE g_Master_Font_0059; DELETE g_Master_Font_0060; 
		DELETE g_Master_Font_0061; DELETE g_Master_Font_0062; DELETE g_Master_Font_0063; 
		DELETE g_Master_Font_0064; DELETE g_Master_Font_0065; DELETE g_Master_Font_0066; 
		DELETE g_Master_Font_0067; DELETE g_Master_Font_0068; DELETE g_Master_Font_0069; 
		DELETE g_Master_Font_0070; DELETE g_Master_Font_0071; DELETE g_Master_Font_0072; 
		DELETE g_Master_Font_0073; DELETE g_Master_Font_0074; DELETE g_Master_Font_0075; 
		DELETE g_Master_Font_0076; DELETE g_Master_Font_0077; DELETE g_Master_Font_0078; 
		DELETE g_Master_Font_0079; DELETE g_Master_Font_0080; DELETE g_Master_Font_0081; 
		DELETE g_Master_Font_0082; DELETE g_Master_Font_0083; DELETE g_Master_Font_0084; 
		DELETE g_Master_Font_0085; DELETE g_Master_Font_0086; DELETE g_Master_Font_0087; 
		DELETE g_Master_Font_0088; DELETE g_Master_Font_0089; " | /twc/bin/fire_str >/dev/null 2>&1
	sleep 3
}


#####
# Clean out ALL g_TagFont_* Dif entries
#####
clean_tag_fonts()
{

	cd /twc/bin

	echo "  DELETE g_TagFont_101; DELETE g_TagFont_102; DELETE g_TagFont_103; 
		DELETE g_TagFont_104; DELETE g_TagFont_105; DELETE g_TagFont_106; 
		DELETE g_TagFont_107; DELETE g_TagFont_108; DELETE g_TagFont_109; 
		DELETE g_TagFont_110; DELETE g_TagFont_201; DELETE g_TagFont_202; 
		DELETE g_TagFont_203; DELETE g_TagFont_204; DELETE g_TagFont_205; 
		DELETE g_TagFont_206; DELETE g_TagFont_207; DELETE g_TagFont_208; 
		DELETE g_TagFont_209; DELETE g_TagFont_210; " | /twc/bin/fire_str >/dev/null 2>&1
	sleep 3
}


#####
# Look in the dif to see if there are any duplicate font dif entries
#####
look_for_master_dupes()
{
	#create a file which contains font definitions dif names
	/twc/bin/db_exp -s dif /twc/dif/wxl_dif |grep "^\"g_Master_Font_" | cut -f1 -d',' > /tmp/font_list

	sort /tmp/font_list > /tmp/font_list.sort
	sort -u /tmp/font_list > /tmp/font_list.sortu

	#See if there are any duplicate lines by comparing the "sorted" list with the "sorted unique" list
	diff -q /tmp/font_list.sort /tmp/font_list.sortu >/dev/null 2>&1

	#if the return from the diff was 0, then the lists are the same and we don't have any dupes
	DUPE_MASTER_FONTS=$?

	rm -f /tmp/font_list /tmp/font_list.sort /tmp/font_list.sortu
}




look_for_tag_dupes()
{
	#create a file which contains font definitions dif names
	/twc/bin/db_exp -s dif /twc/dif/wxl_dif |grep "^\"g_TagFont_" | cut -f1 -d',' > /tmp/font_list

	sort /tmp/font_list > /tmp/font_list.sort
	sort -u /tmp/font_list > /tmp/font_list.sortu

	#See if there are any duplicate lines by comparing the "sorted" list with the "sorted unique" list
	diff -q /tmp/font_list.sort /tmp/font_list.sortu >/dev/null 2>&1

	#if the return from the diff was 0, then the lists are the same and we don't have any dupes
	DUPE_TAG_FONTS=$?

	rm -f /tmp/font_list /tmp/font_list.sort /tmp/font_list.sortu
}




#####
# We can't delete a dif entry if the entry it points to (the #name") doesn't exist.
# So, we must delete the "g_Master_Font_*" entries before the "g_TagFont_*" entries.

#####
# Check MASTER fonts
#####

MAX_LOOP_COUNT=5
look_for_master_dupes

while [ \( $DUPE_MASTER_FONTS -ne 0 \) -a \( $MAX_LOOP_COUNT -gt 0 \) ]
do
	echo "calling clean_master_fonts"
	clean_master_fonts

	look_for_master_dupes

	MAX_LOOP_COUNT=`expr $MAX_LOOP_COUNT - 1`
	echo "MAX_LOOP_COUNT = $MAX_LOOP_COUNT"
done



#####
# Check TAG fonts
#####

MAX_LOOP_COUNT=5
look_for_tag_dupes

while [ \( $DUPE_TAG_FONTS -ne 0 \) -a \( $MAX_LOOP_COUNT -gt 0 \) ]
do
	echo "calling clean_tag_fonts"
	clean_tag_fonts

	look_for_tag_dupes

	MAX_LOOP_COUNT=`expr $MAX_LOOP_COUNT - 1`
	echo "MAX_LOOP_COUNT = $MAX_LOOP_COUNT"
done


