#!/opt/tcl803/bin/sytcl
#
# $Header: /home/cvsroot/tcldb/sybdump/sybdump,v 1.9 1998/12/14 20:54:02 de Exp $
#
#	Homegrown Tcl version of "resql" 
#	not quite as functional but much cheaper :-)
#
#	Creates subdir named Dbase in cwd,
# 		containing sql and csh scripts
#	files *.dat are TSV dumps of data
#	files *.sql are sql DDL for rebuilding database
#	files *.csh are csh wrappers around sql and/or bcp commands
#
#	syntax:		sybdump Dbase Server ?dbusername? ?dbpasswd?
#
####-----------------------------------------------------------------####
#	Uncomment this code if you have loadable sybtcl extension.
#	In this case you will be running a straight TclX interp
#	from the #! line above, as opposed to a hardwired "sytcl".
# 
# set err [catch {set stv [package require Sybtcl]} res]
# if {$err} {
# 	puts stderr "Cannot load package SybTcl, fatal error"
# 	puts stderr "$res"
# 	exit 1
# } else {
# 	puts stderr "SybTcl version $stv"
# }
#
####-----------------------------------------------------------------####
#
set ucodb /opt/share/tcl/lib/ucodb
loadlibindex $ucodb/ucodb.tlib
loadlibindex $ucodb/sybdump.tlib
#
#
#------------------------------MAIN--------------------------------------
#
	lassign $argv server base user pass

	if {[llength $argv] < 2} {
		echo "Usage:  sybdump DBaseName ServerName ?user? ?password?"
		exit
	}

	if {$user == ""} {
	echo "Please tell me your Sybase username:"
	gets stdin user
	}
	if {$pass == ""} {
	echo "Please enter your Sybase password:"
	gets stdin pass
	}

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

	set tables ""

#	Output file names
	if {[file exists $base]} {
		if {![file isdirectory $base]} {
			puts stderr "Sorry, $base is not a directory!"
			exit 0
		}
	} else {
		system "mkdir $base"
		puts stderr "created new directory $base"
	}

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

#	data in and out scripts in here

	set extfile $base/Extract.$base.csh
	set impfile $base/Reload.$base.csh
	set bcpof [open $extfile w]
	set bcpif [open $impfile w]

	set autofile $base/Automatic.csh
	set autof [open $autofile w]

	set descrip(cfg) "master db config data"
	set descrip(spd) "master db sp definitions"
	set descrip(tbl) "table definition ddl"
	set descrip(usr) "logins, users, groups"
	set descrip(act) "action privs (grant/revoke)"
	set descrip(acl) "object privs (grant/revoke)"
	set descrip(trg) "triggers"
	set descrip(pro) "procs"
	set descrip(vuw) "views"
	set descrip(rul) "rules and constraints"
	set descrip(def) "defaults"
	set descrip(key) "primary, foreign keys"
	set descrip(typ) "user defined data types"
	set descrip(idx) "table indices"
	set descrip(dev) "devices and databases, options"

###	TODO
###	what are "binds" ?
###

	set masterf [list dev cfg ]

	set files [list cfg dev usr act typ def rul tbl idx vuw pro trg acl key]

	set pass1 [list cfg dev usr act typ def rul tbl]
	set pass2 [list idx vuw pro trg acl key ]



	foreach f $files {
		set ${f}file $base/$base.$f.sql
		set ${f}f [open [set ${f}file] w]
	}



#-------------------------------------------------------------------------------------
#	put the preamble in your output files first 
#	create auto reload master script

	set preamble "#!/bin/csh\n#\n# BCP script for extracting data from Sybase database $base"
	puts $bcpof $preamble
	set preamble "#!/bin/csh\n#\n# BCP script for reloading data into Sybase database $base"
	puts $bcpif $preamble

	set preamble "#!/bin/csh\n#\n# Master script for reconstructing database $base"
	puts $autof $preamble
	foreach f $pass1 {
		puts $autof "isql -Usa -P$pass -S$server < $base.$f.sql"
	}
	puts $autof "source $impfile"
	foreach f $pass2 {
		puts $autof "isql -Usa -P$pass -S$server < $base.$f.sql"
	}
	close $autof

#

	set preamble "use $base\ngo\n\n"

	foreach f $files {
		set fp [set ${f}f]
		puts $fp "/* $descrip($f)				*/"
		puts $fp "/* [clock format [clock seconds]]		*/"
		puts $fp "/* SQL script for reconstituting $base	*/"
		puts $fp "/* sybdump run by [id user] on [id host]	*/"
		puts $fp $preamble
	}

	foreach f $files {

		make_$f

	}

	foreach f $files {
		close [set ${f}f]
	}

	system "chmod 700 $impfile"
	system "chmod 700 $extfile"

###
###	DONE
###
