#!/bin/csh

# RUN THE C PREPROCESSOR ON THE PREPARES SISAL SOURCE FILE AND LEAVE THE RESULT
# IN $2.i
# ARGUMENTS: $1=spp2 $2=cc_cmd $3=file_root $4=rest

set spp2cmd=$1
set cccmd=$2
set root=$3
shift;shift;shift

if ( -e $root.c == 1 ) then
  if ( -w $root.c != 1 ) then
    echo cannot overwrite $root.c
    exit 1
  endif
endif

if ( -e $root.spp == 1 ) then
  if ( -w $root.spp != 1 ) then
    echo cannot overwrite $root.spp
    exit 1
  endif
endif
  
if ( -e $root.sis != 1 ) then
  echo $root.sis does not exist
  exit 1
endif

/bin/cp $root.sis $root.c
if ( $status <> 0 ) then
  /bin/rm $root.c
  exit 1
endif

$cccmd -E $* $root.c > $root.spp
if ( $status != 0 ) then
  /bin/rm $root.c
  /bin/rm $root.spp
  exit 1
endif

$spp2cmd -B$root.c -R$root.sis < $root.spp > $root.i
if ( $status != 0 ) then
  /bin/rm $root.spp
  /bin/rm $root.c
  exit(1)
endif

/bin/rm $root.spp
/bin/rm $root.c

exit 0
