#!/bin/csh -f

if($1 == "-h" || $1 == "" || $#argv < 1 || $#argv > 2) then
 echo " "
 echo "Usage:   "
 echo  "  >> create_new_geo_table tablename (database)"
 echo  "     creates a new geometry table <tablename> into database <database>"
 echo " "
 echo "Default database if not specified: user_geometry."
 echo "Alternative database: user_geometry."
 echo " "
 exit 0
endif

set table    = $1
set database = $2
if($database =="") then
 set database = "user_geometry"
endif

echo Using database $database

rm -f tmp.txt
echo  "use $database ;" > tmp.txt
echo  "describe $table;"  >> tmp.txt

echo
echo Checking if table $table already exists...

set out = `mysql -h $GEMC_HOST -u $GEMC_USER < tmp.txt | grep Field | awk '{print $2}'`
echo


if($out == "Type") then
 echo Table $table already exists. If you need to delete it, use drop_geo_table.

else
  echo Table doesn\'t exist. 
  echo Creating new table: $table
  echo
  rm -f tmp2.txt tmp3.txt
  sed "s/template/$table/"       $GEMC/database_io/geo_table_template > tmp2.txt
  sed "s/db_geometry/$database/" tmp2.txt > tmp3.txt
  mysql -h $GEMC_HOST -u $GEMC_USER < tmp3.txt
  set out = `mysql -h $GEMC_HOST -u $GEMC_USER < tmp.txt | grep Field | awk '{print $2}'`

  if($out == "Type") then
    echo Table $table created.
  else
    echo Table cannot be created.
  endif
endif
rm -f tmp3.txt tmp2.txt tmp.txt




