#!/bin/ash
# *********************************************************************
# makeschema: build the 'table' and 'column' database schema files.
#
# Copyright (c) 2006,2007,2009 Carlo Strozzi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# *********************************************************************

# Get local settings and apply defaults.
: ${TMPDIR:=/tmp} ${NOSQL_CHARSET:=utf-8}
: ${NOSQL_INSTALL:=@NOSQL_INSTALL@}
: ${SWU_INSTALL:=@SWU_INSTALL@}
: ${PATH:=@LOCAL_PATH@}

export NOSQL_INSTALL SWU_INSTALL PATH TMPDIR NOSQL_CHARSET

# Default paths to old schema files.
old_table=./table
old_column=./column

out=column				# default output.

while [ $# -gt 0 ]
do
   case $1 in
	-t|--table)
	     out=table			# new default output.
	     case "$2" in
		  -*)  ;;		# stick with the default.
		  *)
		       if [ "X$2" != X ]
		       then
			  shift
			  old_table="$1"
		       fi
		  ;;
	     esac
	;;
	-c|--column)
	     out=column		# in case both '-t' and '-c' where specified.
	     case "$2" in
		  -*)  ;;		# stick with the default.
		  *)
		       if [ "X$2" != X ]
		       then
			  shift
			  old_column="$1"
		       fi
		  ;;
	     esac
	;;
	-h|--help)
	     grep -v '^#' $NOSQL_INSTALL/help/makeschema.txt
	     exit 0
	;;
	--show-copying)
	     cat $NOSQL_INSTALL/doc/COPYING
	     exit 1
	;;
	--show-warranty)
	     cat $NOSQL_INSTALL/doc/WARRANTY
	     exit 1
	;;
	-*) ;;				# Skip unknown options.
	*) args="$args $1" ;;
   esac
   shift
done

set - $args

if [ $# -lt 1 ]
then
   echo 'Usage: makeschema [options] table [table ...]' >&2
   exit 1
fi

table_tmp=`mktemp $TMPDIR/makeschema.XXXXXX` || exit $?
column_tmp=`mktemp $TMPDIR/makeschema.XXXXXX` || exit $?
work_tmp=`mktemp $TMPDIR/makeschema.XXXXXX` || exit $?

# Set-up trapping on signals.
trap "rm -f $table_tmp $column_tmp $work_tmp" 0 
trap "exit 2" 1 2 3 15

# Prime the output tables.
printf '\001Table\t\001Path\n' > $table_tmp
printf '\001Column\t\001Table\n' > $column_tmp

for i
do
    if [ -e "$i" ]
    then
       istable < "$i" 2>/dev/null
       if [ $? -ne 0 ]
       then
	  echo makeschema: skipping invalid table "'$i'" >&2
	  continue
       fi
       tbl_name=$(basename "$i")
       printf "$tbl_name\t$i\\n" >> $table_tmp
       template < "$i" |
		_awk -- '{print $2 "\t'"$tbl_name"'"}' >> $column_tmp
    else
       echo makeschema: table "'$i'" not found >&2
       exit 1
    fi
done

sorttable Table < $table_tmp > $work_tmp; cp $work_tmp $table_tmp
sorttable < $column_tmp > $work_tmp; cp $work_tmp $column_tmp

# Incorporate information from old schema files if available, and
# if they contain the necessary column names.

if [ -e "$old_table" ]
then
   if awktable -i "$old_table" -- \
		'BEGIN{rc_=1} Table {exit(rc_=0)}END{exit(rc_)}'
   then
      rmcolumn Path < "$old_table" | jointable --all $table_tmp - > $work_tmp
      cp $work_tmp $table_tmp
   fi
else
   echo makeschema: missing old table schema "'$old_table'" >&2
fi

if [ -e "$old_column" ]
then
   if awktable -i "$old_column" -- \
	'BEGIN{rc_=1} Column && Table {exit(rc_=0)}END{exit(rc_)}'
   then
      jointable --all --column Column,Table \
			$column_tmp "$old_column" > $work_tmp
      cp $work_tmp $column_tmp
   fi
else
   echo makeschema: missing old column schema "'$old_column'" >&2
fi

if [ $out = table ]
then
   cat $table_tmp
else
   # Autmatically set basic schema column flags.
   awktable -H -i $column_tmp -- '
   !Flags {print; next}
   $Table=="column" && ($Column=="Column" || $Column=="Table") {
	gsub(/k/,"",$Flags); $Flags = $Flags "k"
   }
   $Table=="table" && $Column=="Table" {
	gsub(/K/,"",$Flags); $Flags = $Flags "K"
   }
   $Table=="table" && ($Column=="Path" || $Column=="Edit") {
	gsub(/h/,"",$Flags); $Flags = $Flags "h"
   }
   {print}'
fi

# End of program.
