#!/bin/csh -d
# This is "cgisetup", which sets up a new cgiwrite file.
# Required arguments are the name and max size of a database.

set permfile = "dbPermissions.txt"

if ($# != 2) then
  echo Usage: cgisetup dbname maxsize
  exit
endif

echo Setup for cgiwrite
echo " "

# Check that we are in a cgi directory
set cd = `pwd`
set alt = $user
if (!($cd:t == cgi) && !($cd:t == cgi-$alt)) then
  echo Is `pwd` your cgi directory \(y or n\)\?
  if ($< != "y") then
    echo Please run cgisetup from your cgi directory.
    exit
  endif
  echo " "
endif

if (! -e $permfile) then
  if (! { touch $permfile }) then
    echo Error: cannot create $permfile
    exit
  endif
  chmod g+r $permfile
  chmod og-w $permfile
  echo $permfile has been created.
endif

set name = $1
set size = $2

# Check if already on file
set mytemp = `mktemp -u /tmp/jnkknj.XXXXXX`
cat $permfile | grep $name > $mytemp
if (! -z $mytemp) then
  echo Possible duplicate database name\; change it or edit dbPermissions manually
  rm $mytemp
  exit
endif
rm $mytemp

# Enter new database info into permissions file
echo $name $size >> $permfile

# Create and set mode for new database
if (! { touch $1 }) then
  echo Error cannot create $name
endif
chmod g+w $name
chmod og-r $name

echo Setup appears successful
echo " "
echo Use cgiread $name at any time to create a new $name:r.csv file
echo " "
echo See http://www.stat.cmu.edu/~hseltman/Computer.html\#cgiwrite
echo for more details including the HTML part.

