#!/bin/sh
# meant to be run from the same directory as the pgsql .table files

make clean

for i in *.table.m4
do
  make `basename $i .m4`
done

for file in *.table 
do
  table=`basename $file .table`
  echo "Processing $table from $file.."
  echo ./create-new-db-layout $file
  ./create-new-db-layout $file > /tmp/table.$$.rec
  for dir in ../../../astium-confweb/src/php/cms ../../../astium-confweb/src/html/php/cms
  do
    if [ -f $dir/$table.rec ]
    then
      if [ ! -f $dir/$table.rec-orig ]
      then
        # create .rec backup files if not existing yet
        echo cp $dir/$table.rec $dir/$table.rec-orig
        cp $dir/$table.rec $dir/$table.rec-orig
      fi
      echo mv -f /tmp/table.$$.rec $dir/$table.rec
      mv -f /tmp/table.$$.rec $dir/$table.rec
    fi
  done
  # it's still there, so there was no .rec file apparently
  if [ -f /tmp/table.$$.rec ]
  then
    echo mv -f /tmp/table.$$.rec ../../../astium-confweb/src/html/php/cms/$table.rec
    mv -f /tmp/table.$$.rec ../../../astium-confweb/src/html/php/cms/$table.rec
    touch ../../../astium-confweb/src/html/php/cms/$table.rec-orig
  fi
done

echo "./create-views > ../../../astium-confweb/src/db/views"
./create-views > ../../../astium-confweb/src/db/views

