#!/bin/csh
# This is "cgiread", which is used to convert cgiwrite output to
# comma separated values (csv) format using R.
# The single (required) argument is the cgiwrite output file name.

# Check for first argument (basename of file)
if ( $# != 1 ) then
  echo Usage: cgiread filename
  exit
endif

# check file exists
if (! -e $1) then
  echo $1 does not exist
  exit
endif
if (-s $1 == 0) then
  echo $1 is empty
  exit
endif
echo $1 > cgireadDBfile.tmp

# erase log file if it exists
if (-e cgireadlog.tmp) then
  rm -f cgireadlog.tmp
endif
set tmpfile = `mktemp -u /tmp/cgiwrite.XXXXXX`
R --no-restore --no-save --no-readline < cgiread.R >& $tmpfile

if (-e cgireadlog.tmp) then
  cat cgireadlog.tmp
  rm -f cgireadlog.tmp
else
  echo Sorry, something went wrong!
  cat $tmpfile
endif

if (-e $tmpfile) then
  rm -f $tmpfile
endif

if (-e cgireadDBfile.tmp) then
  rm -f cgireadDBfile.tmp
endif
