#!/bin/csh -f

if($1 == "" || $1 == "-h"  || ($#argv < 4 && $1 != "clean" ) ) then
	echo " "
	echo "Usage:   "
	echo  "  >> go_table <tablename> <n. of paddles> <n. of layers> <shield material> [database]"
	echo " "
	echo " currently for CND:"
	echo
	echo " ./go_table CND  30 4 Air"
	echo " ./go_table CND2 75 3 Air"
	echo " ./go_table CND3 30 4 Lead"
	echo
	exit 0
endif

set tables  = $1
set paddles = $2
set layers  = $3
set shield  = $4
set DB      = $5

if($1 == "clean") then
  rm -rf *.txt
else
  foreach t ($tables)
    rm -rf $t".txt"
  end
  rm -rf *.txt

  # Default database - first argument defines new one
  set database = $DB
  if($database =="") then
    set database = "user_geometry"
  endif

  # creates files. Should match the tables name above
  ./cnd_build.pl $tables $paddles $layers $shield

  # Remaking the tables
  foreach t ($tables)
    echo $t $database
    echo "y" | $GEMC/database_io/drop_geo_table  $t   $database
    $GEMC/database_io/create_new_geo_table  $t  $database
  end

  rm -rf tmp.txt
  echo use $database";" > tmp.txt;
  foreach t ($tables)
    echo  LOAD DATA LOCAL INFILE \'$t.txt\'   into TABLE $t";" >> tmp.txt
    echo update $t" set time=NOW() where time=0;" >> tmp.txt;
  end

  mysql -h $GEMC_HOST -u $GEMC_USER < tmp.txt

endif
