#!/bin/sh
#cat config.4gl.head

echo "#This file was generated automatically using 'mk_config' script"
echo "#DO NOT EDIT THIS FILE - EDIT 'mk_config' script instead"
echo "#All changes to this file will be lost next time 'mk_config' runs."
echo 'GLOBALS "globals.4gl"'

if type nawk > /dev/null 2> /dev/null
then
	AWK=nawk
else
	if type gawk > /dev/null 2> /dev/null
	then
		AWK=gawk
	else
		AWK=awk
	fi
fi

$AWK '
BEGIN {
	name=""
	desc["UI"]="User Interface options"
	desc["COMPILE"]="Compile time options"
	desc["RUNTIME"]="Run time options"
	desc["SQL"]="SQL Options"
	desc["FORMS"]="Form Options"
	desc["ODBC"]="ODBC Options"
	desc["TUI"]="Text User Interface options"
	desc["REPORT"]="Report generation options"
	desc["PRINT"]="Printing options"
	desc["MENU"]="Menu options"
	desc["PCODE"]="P-code options"
	desc["ESQL"]="ESQL/C code options"
	desc["DEBUG"]="Debugging options"
	desc["HELP"]="Help handling options"
	no=0
	print "FUNCTION do_display_txt(n)"
	print "DEFINE n INTEGER"
	print "DEFINE a INTEGER"
	print "FOR a=1 to 24"
        print "let lv_txt[a]= \" \""
	print "end for"
	print "CASE n"
}

/^#/ {next}
/^=/ 	{	
	options[no]=substr($0,2)
	next
	}
/^\$/ {
	options[no]="listing" $0
	next
      }


/^\t/ {
	print substr($0,2) > "helpfile.msg"
					this_option=substr($0,2)
					gsub("\"","\\\"",this_option)
	print "   let lv_txt[" cnt "]=     \"" this_option "\"" 
	if (length(substr($0,2))>80) {
		print "#*** TOO LONG"
	}
	cnt++
	next
}

{a=$0
if (substr(a,1,1)!=" ") {
	if (name!="") {
		print "   CALL display_txt()"
		split(units,arr_units,"/")
		for (key in arr_units) {
			var=arr_units[key]
			arr_vars[var]++
			txt=var arr_vars[var];
			arr_names[txt]=name
			arr_briefs[name]=brief
			help_no[name]=no
		}
		no++
	}
	
	name=$1
	if (name=="") next
	units=$2
	brief=substr($0,length(name)+length(units)+3)
	print "." no > "helpfile.msg"
	print "( " $0 " )" > "helpfile.msg"
	print brief > "helpfile.msg"
	printf "Categories :"  > "helpfile.msg"
	for (key in arr_units) {
		printf (" %s",arr_units[key]) > "helpfile.msg"
	}
	print "" > "helpfile.msg"
	print "when "  no 
	cnt=1
}
}

END {

	if (name!="") {
		print "   CALL display_txt()"
		print ""
		help_no[name]=no
		split(units,arr_units,"/")
		for (var in arr_units) {
			arr_vars[var,name]=brief
		}
		no++
	}
	print "END CASE"
	print "END FUNCTION"
	print " "
	print "MAIN"
	print "   OPTIONS HELP FILE \"helpfile.hlp\""
	print "   OPTIONS HELP KEY \"F2\""
	print "   DEFER INTERRUPT"
	print "   CALL initialize()"
	print "   CALL load_newvals()"
	print "   MENU \"Main\""

	for (var in arr_vars) {
		xx=arr_vars[var];
		print "      COMMAND \"" var "\" \"" desc[var] "\""
		print "              HELP 1"
		print "         call details_" var "()"
		print " "
	}
	print "   COMMAND KEY (\"Q\",\"q\",\"F1\") \"Quit\" \"Exit the configurator program\" HELP 2 exit menu"
	print "   END MENU"
	print "   CALL ask_save()"
	print "END MAIN"


	for (var in arr_vars) {
		xx=arr_vars[var];
		print " "
		print "FUNCTION details_" var "()"
		for (a=1;a<=xx;a++) {
			txt=var a
			name=arr_names[txt]
			brief=arr_briefs[name]
			print "   let mv_option[" a "].name=\"" name "\""
			print "   let mv_option[" a "].brief=\"" brief "\""
			print "   let mv_option_offscr[" a "].id=" help_no[name]

			if (substr(options[help_no[name]],1,7)!="listing") {
				num_op=split(options[help_no[name]],arr_options,"|")
				print "   let mv_option_offscr[" a "].nvalues=" num_op
				for (b in arr_options) {
					print "   let mv_option_offscr[" a "].values[" b "]=\"" arr_options[b] "\""
				}	
			} else {
				print "   call load_options_from_listing(" a ",\"" substr(options[help_no[name]],8) "\")"

			}
		}
		print "   call show_options(" a-1 ")"
		print "END FUNCTION"
	}

	print " "
	print "FUNCTION get_id(s)"
	print "define s char(80)"
	print "case s"
	
	cnt=1
	for (var in arr_names) {
		if (!printed[arr_names[var]]) {
			print "when \"" arr_names[var] "\" return " cnt++
		}
		printed[arr_names[var]]=1
	}
	print "end case"
	print "return 0"
	print "END FUNCTION"

	for (var in arr_names) {
		printed[arr_names[var]]=0;
	}

	print " "
	print "FUNCTION get_name(n)"
	print "define n integer"
	print "case n"
	cnt=1
	for (var in arr_names) {
		if (!printed[arr_names[var]]) {
			print "when " cnt++ " return \"" arr_names[var] "\""
		}
		printed[arr_names[var]]=1
	}
	print "end case"
	print "return \"\""
	print "end function"

}' < descriptions



