#!/opt0/tcl/bin/sytcl
#
#
# stripped down sybdump gets just a format file def
#
# format files are of the form:
# 4.0
# 7
# 1       SYBCHAR 0       13      "\t"    1 acfusu
# 2       SYBCHAR 0       8       "\t"    2 funds
# 3       SYBCHAR 0       0       "\t"    3 funds_stamp
# 4       SYBCHAR 0       8       "\t"    4 lien
# 5       SYBCHAR 0       0       "\t"    5 lien_stamp
# 6       SYBCHAR 0       8       "\t"    6 expend
# 7       SYBCHAR 0       0       "\n"    7 expend_stamp
#
#			^^^^ but this length is the either/or length for
#			reading field data in:  either read N chars or
#			stop at the terminator.  so when you are sure
#			that your terminator is there, you can just make
#			this large -- so we do
#
#
loadlibindex /opt0/share/tcl/lib/ucodb/ucodb.tlib
#
#
#------------------------------MAIN--------------------------------------
#
	lassign $argv base server

	if {[llength $argv] != 2} {
		echo "Usage:  sybdump DBaseName ServerName"
		exit
	}
	set user guest
	set pass rsvp

	if {![file isdirectory $base]} {
		if {[file exists $base]} {
			puts stderr "OOPS, file $base is not a dir."
			puts stderr "Fix this and try again."
		} else {
			system "mkdir $base"
		}
	}

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

	global sybmsg
	global dbpipe1
	global init
	global debugs
	
	set debugs {}

	set tables ""

	set dbpipe1 [sybOpen $base $user $pass $server]
	set dbpipe2 [sybOpen $base $user $pass $server]

#	put the preamble in your output files first 

#	First deal with the tables

	echo "dealing with the user tables..."

#	get 2 fields:  owner name and table name from sysobjects
	set sqlcmd "select name,user_name(uid),id from sysobjects where type = 'U' order by name,user_name(uid)"
	set table sysobjects
	doSQL 1
	while {1 == 1} {

		set cnames {}
		set line [sybNext 1]
#		echo "INPUT	$line"
		if {$line == ""} {break}

		set owner [lindex $line 1]
		set tname [lindex $line 0]
		set objid [lindex $line 2]
		set fname [format "%s.%s" $owner $tname]

		set sqlcmd "sp_help '$fname'"
		set table $tname

		if {[lsearch $tables $fname] >= 0} {
			set comment "/* WARNING, WARNING		*/\n/*   This table name is already in use in this database!*/"
		}
		lappend tables $tname

		doSQL 2

		set comment "/* table was $base.$fname 			*/"

		set fmtf [open $base/$tname.fmt w]

	while {3 == 3} {

		set line [sybNext 2]
#		echo "	INPUT2	$line"
		if {$sybmsg(nextrow) == "NO_MORE_RESULTS"} {break}

#	If the first word is name then toss a line and take the table name
#	from the first word of the 2nd line

#	If the first word is Column_name then eat one line and start
#	looking for columns:  the first line that does not start with
#	a space is the end of this column list so terminate the table
#	definition.  Put the columns and their characteristics in 
#	an array and we'll write it out at the end.
		if {[lindex $line 0] == "default"} {
			set line [sybNext 2]
			set i 1
			while {4 == 4} {
				set line [sybNext 2]
				if {$line == ""} {break}
				set cname [lindex $line 0]
				lappend cnames $cname
				set ctype [lindex $line 1]
				set csize [lindex $line 2]
				set cnull [lindex $line 3]
				if {$cnull == 0} {
				set cnull "NOT NULL"
				} else {
				set cnull "NULL"
				}
				set fields($i,name) $cname
				set fields($i,type) $ctype
				set fields($i,size) $csize
				set fields($i,null) $cnull
				incr i
			}
			set record "4.0\n[expr {$i - 1}]"
			puts $fmtf $record
			loop j 1 $i {
				case $fields($j,type) in {
				{char varchar} {
				set record "  $fields($j,name)	$fields($j,type)($fields($j,size))  $fields($j,null)"
				}
				{default} {
				set record "  $fields($j,name)	$fields($j,type)	$fields($j,null)"
				}
				}
				if {$j < [expr {$i - 1}]} {
					set record "$j	SYBCHAR 0	255	\"~\" 	$j $fields($j,name)	"
				} else {
					set record "$j	SYBCHAR 0	255	\"\\n\" 	$j $fields($j,name)	"
				}
				puts $fmtf $record
			}

		}
			
	}
	close $fmtf
#

	}
#
#	and   keep doing it.
#
	
