proc make_idx {} {

	global base idxf

#	rules of the sysindexes table
#	indid is 0 if a table w/o indexes
#	1 if clustered
#	>1 if nonclustered
#	255 if a text chain

	set sqlcmd "select i.indid, i.name, o.id, user_name(o.uid), o.name, \
	i.status, keycnt, s.name, s.segment from dbo.sysindexes i, dbo.sysobjects o, \
        dbo.syssegments s where i.id = o.id and i.segment = s.segment and \
        o.sysstat & 7 = 3 and i.indid > 0 and i.indid != 255 and  \
        o.name not in ( 'spt_values', 'spt_committab' )  \
        order by i.name, o.uid "

	set sqt "sysindexes sysobjects syssegments"

	doSQL 1

	while {1} {

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

		lassign $line indid iname oid own oname istat keyct segn segid

		keylset Indices($iname) tbl $oname toid $oid indid $indid own $own 
		keylset Indices($iname) keyct $keyct segn $segn segid $segid status $istat 
	}

	if {![info exists Indices]} {
		puts stderr "OUCH no indices at all in $base"
		return
	}

	foreach i [lsort [array names Indices]] {

		set kc [keylget Indices($i) keyct]
		set id [keylget Indices($i) indid]
		set tn [keylget Indices($i) tbl]

		set keys ""
		loop k $kc 0 -1 {
		set sqlcmd "select index_col(\"$tn\", $id, $k)"
#		puts stderr "$sqlcmd"
		doSQL 1
		set col [sybNext 1]
#		puts stderr "Got col $col"
		if {[string trim [stringFix $col]] == ""} {continue}
		lappend keys $col
		}

		keylset Indices($i) keys $keys

		if {$id == 0} {continue}

		if {$id == 1} {
			set it "clustered"
		} 
		if {$id > 1} {
			set it "nonclustered"
		}
		puts $idxf "create $it index $i on ${tn}([join $keys ", "]) with fillfactor=75\ngo"
		
	}


	return idx

}
	
