#!/bin/csh -f

if($1 == "" || $1 == "-h"  || ($#argv < 3 && $1 != "clean" ) ) then
	echo " "
	echo "Usage:   "
	echo  "  >> go_table <tablename> <n. of paddles> <n. of layers>  [database]"
	echo " "
	exit 0
endif
	

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

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 = "clas12_geometry"
  endif

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


  # Remaking the tables
  foreach t ($tables)
    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 clasdb.jlab.org -u $GEMC_USER < tmp.txt


endif












