#! /bin/sh
# ----------------------------------------------------------------
#   FILE
#	genbki.sh
#
#   DESCRIPTION
#       shell script which generates .bki files from specially
#	formatted .h files.  These .bki files are used to initialize
#	the postgres template database.
#
#   NOTES
#	non-essential whitespace is removed from the generated file.
#	if this is ever a problem, then the sed script at the very
#	end can be changed into another awk script or something smarter..
#
#   IDENTIFICATION
# 	$Header: /private/postgres/src/lib/Gen/RCS/genbki.sh,v 1.4 1992/06/23 18:24:51 mer Exp $
# ----------------------------------------------------------------

# -----------------
# check for options i.e. -D defines for cpp
# -----------------
BKIOPTS=''
set - `getopt D: $*`
if [ $? != 0 ]
then
    echo `basename $0`: Bad option
    exit 1
fi

for opt in $*
do
    case $opt in
    -D) BKIOPTS="$BKIOPTS -D$2"; shift; shift;;
    --) shift; break;;
    esac
done

# ----------------
# 	collect nodefiles
# ----------------
SYSFILES=''
x=1
numargs=$#
while test $x -le $numargs ; do
    SYSFILES="$SYSFILES $1"
    x=`expr $x + 1`
    shift
done

# ----------------
# 	strip comments and trash from .h before we generate
#	the .bki file...
# ----------------
#
cat $SYSFILES | \
sed -e 's/\/\*.*\*\///g' \
    -e 's/;[ 	]*$//g' | \
awk '
# ----------------
#	now use awk to process remaining .h file..
#
#	nc is the number of catalogs
#	inside is a variable set to 1 when we are scanning the
#	   contents of a catalog definition.
#	inserting_data is a flag indicating when we are processing DATA lines.
#		(i.e. have a relation open and need to close it)
# ----------------
BEGIN {
	inside = 0;
	raw = 0;
	bootstrap = 0;
	nc = 0;
	reln_open = 0;
}

# ----------------
#	anything in a BKI_BEGIN .. BKI_END block should be passed
#	along without interpretation.
# ----------------
/^BKI_BEGIN/ 	{ raw = 1; next; }
/^BKI_END/ 	{ raw = 0; next; }
raw == 1 	{ print; next; }

# ----------------
#	DATA() statements should get passed right through after
#	stripping off the DATA( and the ) on the end.
# ----------------
/^DATA\(/ {
	data = substr($0, 6, length($0) - 6);
	print data;
	next;
}

/^DEFINE_INDEX\(/ {
# ----
#  end any prior catalog data insertions before starting a define index
# ----
	if (reln_open == 1) {
		print "show";
		print "close " catalog;
		reln_open = 0;
	}

	data = substr($0, 14, length($0) - 14);
	print "define index " data
}
	
# ----------------
#	CATALOG() definitions take some more work.
# ----------------
/^CATALOG\(/ { 
# ----
#  end any prior catalog data insertions before starting a new one..
# ----
	if (reln_open == 1) {
		print "show";
		print "close " catalog;
		reln_open = 0;
	}

# ----
#  get the name of the new catalog
# ----
	pos = index($1,")");
	catalog = substr($1,9,pos-9); 

	if ($0 ~ /BOOTSTRAP/) {
		bootstrap = 1;
	}

        i = 1;
	inside = 1;
        nc++;
	next;
}

# ----------------
#	process the contents of the catalog definition
#
#	attname[ x ] contains the attribute name for attribute x
#	atttype[ x ] contains the attribute type fot attribute x
# ----------------
inside == 1 {
# ----
#  ignore a leading brace line..
# ----
        if ($1 ~ /{/)
		next;

# ----
#  if this is the last line, then output the bki catalog stuff.
# ----
	if ($1 ~ /}/) {
		if (bootstrap) {
			print "create bootstrap " catalog;
		} else {
			print "create " catalog;
		}
		print "\t(";

		for (j=1; j<i-1; j++) {
			print "\t " attname[ j ] " = " atttype[ j ] " ,";
		}
		print "\t " attname[ j ] " = " atttype[ j ] ;
		print "\t)";

		if (! bootstrap) {
			print "open " catalog;
		}

		i = 1;
		reln_open = 1;
		inside = 0;
		bootstrap = 0;
		next;
	}

# ----
#  if we are inside the catalog definition, then keep sucking up
#  attibute names and types
# ----
	atttype[ i ] = $1;
	attname[ i ] = $2;
	i++;
	next;
}

END {
	if (reln_open == 1) {
		print "show";
		print "close " catalog;
		reln_open = 0;
	}
}
' | \
/lib/cpp $BKIOPTS | \
sed -e '/^[ 	]*$/d' \
    -e 's/[ 	][ 	]*/ /g'

# ----------------
#	all done
# ----------------
exit 0
