#!/usr/local/bin/tcl -f
#------------------------ Global Variables ---------------------------
#
# Feb 9th generalized setOrder and the loop in changeElem
#
#
loadlibindex /usr/local/tcl/local/ucosyb.tlib
loadlibindex /u/de/tcl/f/fosql.tlib
#
#---------------------------------------------------------------------------
# MAIN CODE
#---------------------------------------------------------------------------
# Code to stuff the data dictionary with incomplete entries for all
# fields in a given table.

# Handy Dandy Global Vars
#
global dbpipe1 dbpipe2 dbpipe3 dbpipe4 dbpipe5
global sybmsg
global server
global uname
global table
global debugs
set debugs {}

# Customization options:
#
global bitmapdir
global exedir
global dictionary
global tblref
global fassists
global forms
global fwidgets
global site
global ftypes
#
# Directory in which to find icon bitmaps
set bitmapdir /u/de/tcl/f/bitmaps
#
# Directory in which to find extra executables like 'detached' and
# 'anyprint'
set exedir /usr/local/bin
#
# names of essential tables -- change these according to
#	your site
#	your app
#	your mood :-)
#
set site "UCOLick"
set fassists "info.dbo.fassists"
set forms "info.dbo.forms"
set fwidgets "info.dbo.fwidgets"
set ftypes "devel.cc.types"
# set dictionary "NONE"
set dictionary "info.dbo.data_dict"
set tblref "info.dbo.ucotables"
#
set stdcols "nid pid stamp uid "
#
set uname ""

if {[llength $argv] == 0} {
	echo "Usage:  stuffdict Base.Owner.Table \[,Base.Owner.Table,...\]"
	exit 1
}

        echo "Please tell me your sybase username:"
        gets stdin user
        echo "Please enter your sybase password:"
        gets stdin pass

        foreach fqtable $argv {

	echo "Considering table $fqtable..."

	set t [split $fqtable .]

	if {[llength $t] < 3} {
		echo "$fqtable is not an base.owner.object table specification."
		echo "Please provide the tablename in the FQ form."
		exit 1
	}

        set base "[lindex $t 0]"
	set owner "[lindex $t 1]"
	set table "[lindex $t 2]"

	set dbpipe1 [sybOpen $base $user $pass {}]
	set dbpipe2 [sybOpen $base $user $pass {}]

	set sqlcmd "select count(*) from $tblref where dbname = '$base' and owner = '$owner' and tablename = '$table'"
	doSQL 1
	set refcount [sybNext 1]
	echo "Count of reference entries for $base.$owner.$table is $refcount"

	set sqlcmd "select count(*) from $dictionary where tbln = '$owner.$table'"
	doSQL 1
	set diccount [sybNext 1]
	echo "Count of dictionary entries for $base.$owner.$table is $refcount"

	if {$diccount == 0} {

	set sqlcmd "select a.name from $base..syscolumns a, $base..sysobjects b where a.id = b.id and b.name = '$table' and b.uid = user_id('$owner') "

	set sqt "$base.systables"

	doSQL 1

	while {1 == 1} {

		set c [sybNext 1]
		if {$c == ""} {break}

		if {[lsearch $stdcols $c] == -1} {
		echo "adding column $c for table $table to dictionary"
		set sqlcmd "insert into $dictionary (ddid,tbln,fldn) values (-1,'$owner.$table','$c')"
		doSQL 2
		}
	}

	} else {

		echo "Sorry, there are already $diccount data dictionary entries"
		echo "for the table $base.$owner.$table.  Better check this out"
		echo "by hand so as not to over-write existing data."
		echo "NO DICTIONARY INFO STORED FOR $base.$owner.$table..."

	}

	if {$refcount == 0} {

        echo "adding table $base.$owner.$table to $tblref"
        set sqlcmd "insert into $tblref (tblid,dbname,owner,tablename) values
 (-1,'$base','$owner','$table')"
        doSQL 1

	} else {

	echo "Sorry, there is already a record in $tblref for table"
	echo "$base.$owner.$table.  Better check this out by hand to"
	echo "avoid over-writing existing data."
	echo "NO UCOTABLES DATA STORED FOR $base.$owner.$table ..."

	}

	sybClose 1
	sybClose 2

	}

	exit 0
