#!/opt0/tcl/bin/sytcl
#
#
set infile generic.0
#
loadlibindex /opt0/tcl/lib/ucosybtk/ucosyb.tlib
#
global debugs
set debugs {}
#
global sybmsg
global server
global dbpipe1
#
#
#
if {[llength $argv] != 4} {
	echo "Must provide four args."
	echo "Usage:  generic_form servername tablename formname formtype"
	exit 1
}
#
lassign $argv server table fname ftype
#
set temp [split $table .]
if {$server == "UCO-ADMIN"} {
	set meta info
} else {
	set meta metabase
}
set uname sa
set pass NOYB
set base $meta
set sqt $meta.dbo.forms
#
if {[llength $temp] == 3} {
      lassign $temp dbs u t
} else {
      echo "Sorry, table name must be fully specified."
      exit 1
}
#
set fyx 0
if {$dbs == "ucofyx"} {
	set fyx 1
	set dbs F95
}
#
#
set dbpipe1 [sybOpen $base $uname $pass $server]
#
set pref "$dbs.."
set table "$dbs.$u.$t"
#
set pk [getPkeys $table]
#
echo "pk is $pk"
#
echo "dbs is $dbs owner is $u table is $t"
#
set sqlcmd "select count(*) from $meta.dbo.formtypes where formtype = '$ftype'"
doSQL 1
set ct [sybNext 1]
if {$ct == 0} {
	echo "Sorry, no such form type supported."
	exit 1
}
set cols {}
#
set sqlcmd "select a.name from ${pref}syscolumns a,  ${pref}sysobjects b where a.id = b.id and b.name = '$t' and b.uid = user_id('$u')"
echo "getcols sqlcmd is\n$sqlcmd"
doSQL 1
while {1 == 1} {
	set col [sybNext 1]
	if {$col == ""} {break}
	lappend cols $col
}
#
echo "this table has cols: $cols"
#
set keys {}
foreach k $pk {
	if {$k > 0} {
	set c [lindex $cols [expr {$k - 1}]]
	eval set [set table]($c,pkey) 1
	lappend keys $c
	}
}
#
echo "this table has keys: $keys"
#
if {[llength $keys] == 0} {
	echo "Sorry-charlie, you must have a primary key on table $table"
	exit 1
}
#
set unique [getclock]
set wins {}
set sqlcmd "select distinct tbln from forms where wtype = 'GN'"
doSQL 1
while {1 == 1} {
	set fn [sybNext 1]
	if {$fn == ""} {break}
	lappend wins $fn
}
#
echo "Known window names are:\n$wins"
#
set win [string tolower $fname]
if {[lsearch $wins $win] >= 0} {
	set newin "$win[crange $unique 5 end"
	echo "window name $win already in use, using $newin"
	set win $newin
}
#
echo "window name will be $win"
#
for_file line $infile {

	lassign [split $line ~] fid fn ft level item area wtype tbln fldn wname labx laby labs labf labt laba entx enty ents entf enta cmd next prev help dflt cmd2 echo

#	old exec fids are meaningless
#	and so are prev, next, echo (look at the NULLs in the insert)

	case $wtype in {
	{RB CB PS BT MB} {
		set tbln ""
		set fn $fname
		set ft ""
	}
	{GN} {
		set fn $fname
		set ft $ftype
		set tbln $win
		set dflt $table
		set cmd ""
		set entf ""
		set fldn "$fname"
		set labf "fosql.xbm"
	}
	{default} {
		echo "Whaat?  found a type $wtype in input"
		continue
	}
	}

	if {$labx == ""} {set labx NULL}
	if {$laby == ""} {set laby NULL}
	if {$labs == ""} {set labs NULL}
	if {$entx == ""} {set entx NULL}
	if {$enty == ""} {set enty NULL}
	if {$ents == ""} {set ents NULL}

	set sqlcmd "insert into $meta.dbo.forms values (-1,'$fname','$ftype',$level,$item,'$area','$wtype','$tbln','$fldn','$wname',$labx,$laby,$labs,'$labf','$labt','$laba',$entx,$enty,$ents,'$entf','$enta','$cmd',NULL,NULL,'$help','$dflt','$cmd2',NULL)"
#	echo "sqlcmd:\n$sqlcmd"
	doSQL 1
	echo "stored record $fname level $level $wtype $wname"

}

	set wtype EN
	set ypos .0800
	foreach k $keys {

	set wname [string tolower $k]
	set area f
	echo "add EN keywidget named $wname"

	set sqlcmd "insert into $meta.dbo.forms values (-1,'$fn','$ftype',
$level, $item,'$area','$wtype','$table','$k','$wname',.200,$ypos,10,'mediumhel','Primary Key','w',.600,$ypos,8,'mediumcou','e',NULL,NULL,NULL,NULL,'-1',NULL,NULL)"
	doSQL 1

	set ypos [expr $ypos + .1000]

	}

# end of proc
