#!/bin/csh -f

set prog = $0
set prog = $prog:t

#----------------------------------------------
#  get op sys
#----------------------------------------------
set op_sys=`uname -s`
set tmp_flsp = tmp.$$
echo $op_sys > $tmp_flsp
set op_sys = `tr '[a-z]' '[A-Z]' < $tmp_flsp`
rm -f $tmp_flsp >& /dev/null
if( $op_sys == LINUX || $op_sys == UNIX || $op_sys == DARWIN ) then
  set op_sys = LINUX
endif

#----------------------------------------------
#  check for executable
#----------------------------------------------
set exe = "../bin/moz_pp"
if( ! -e $exe ) then
  echo "No $exe executable; $prog terminating"
  exit -1
endif

#----------------------------------------------
#  check command usage
#----------------------------------------------
if( $#argv < 1 ) then
  cat << EOM
------------------------------------------------------------------------
$prog usage : mozpp "filename"

filename is any mozart preprocessor input file in MZ4ROOT/preproc/inputs  
------------------------------------------------------------------------
EOM
  exit -1
endif

set args = ($argv)
set args1 = (`echo $args | tr '[a-z]' '[A-Z]'`)
#----------------------------------------------
#  check for help
#----------------------------------------------
if( $args1[1] == HELP ) then
  set help_file = ../docs/README.mozpp
  echo " "
  which less >& /dev/null
  if( ! $status ) then
    cat $help_file | less
  else
    which more >& /dev/null
    if( ! $status ) then
      cat $help_file | more
    else
      cat $help_file
    endif
  endif
  echo " "
  exit 0
endif
#----------------------------------------------
#  check input file exists
#----------------------------------------------
if( ! -e $argv[1] ) then
  echo "can't find $argv[1] preprocessor input file in `pwd`; $prog terminating" && exit -1
  exit -1
endif

#----------------------------------------------
#  check for cpp utility
#----------------------------------------------
set found = 0
if( $op_sys == LINUX ) then
  which cpp >& /dev/null
  if( ! $status ) then
    set found = 1
  endif
else if( $op_sys == AIX ) then
  which cpp >& /dev/null
  if( $status ) then
    which /usr/ccs/lib/cpp >& /dev/null
    if( ! $status ) then
      set found = 2
    endif
  else
    set found = 1
  endif
endif

if( ! $found ) then
  echo "can't find cpp utility; $prog terminating"
  exit -1
else if( $found == 2 ) then
  ln -s /usr/ccs/lib/cpp .
endif

#----------------------------------------------
#  preprocess input file
#----------------------------------------------
$exe $argv[1]
#----------------------------------------------
#  cleanup if necessary
#----------------------------------------------
if( $status ) then
  rm -f *.h >& /dev/null
endif

if( $found == 2 ) then
  rm -f cpp >& /dev/null
endif

exit 0
