#! /bin/csh
# Dump the netinfo database via 'niutil' into a Sybase SQL script.
# C.D.Lane (lane@sumex-aim.stanford.edu) 3/1/90

onintr exit

set domain = '.'
set root = '/'
set database = 'netinfo'

set program = $0
set temp = "/tmp/$program:t.${user}.$$"; unset program

set noglob

# Sybase script header to delete old database, create new one and define a type.

echo 'set nocount on'
echo 'go'
echo 'if exists (select * from master.dbo.sysdatabases where name = "'${database}'")'
echo 'drop database' $database
echo 'go'
echo 'create database' $database
echo 'go'
echo 'use' $database
echo 'go'
echo 'execute sp_addtype string, "varchar(128)", "null"'
echo 'go'

# Scan netinfo data dumping a Sybase table for each 'path' and its 'properties'.

foreach table ( `niutil -list $domain $root | sed 's/^[0-9]*//'` )
   echo > $temp
   foreach id ( `niutil -list $domain ${root}$table | sed 's/^\([0-9]*\).*$/\1/'` )
       niutil -read $domain $id | sed 's/:.*$//' >> $temp
   end # foreach id
   echo 'create table' $table '('`sort -u $temp | sed 's/^\(.*\)$/\1 string,/'`')' | sed 's/,\(.\)$/\1/'
   echo 'go'
   echo 'grant select on' $table 'to public'
   echo 'go'
end # foreach table

# Scan the netinfo data again dumping all the 'property' values.

foreach table ( `niutil -list $domain $root | sed 's/^[0-9]*//'` )
   foreach id ( `niutil -list $domain ${root}$table | sed 's/^\([0-9]*\).*$/\1/'` )
       echo 'insert' $table '('`niutil -read $domain $id | sed 's/:.*$/,/'`')' | sed 's/,\(.\)$/\1/'
       echo > $temp
       foreach value ( "`niutil -read $domain $id | sed 's/^.*://'`" )
           echo "'`glob ${value}`'," >> $temp
       end # foreach value
       echo 'values ('`cat $temp`')' | sed 's/,\(.\)$/\1/'
       echo 'go'
   end # foreach id
end # foreach table

exit:

rm -f $temp

unset noglob domain root database temp
