#!/bin/csh -f

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

#------------------------------------------------------------
#  check 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
#------------------------------------------------------------
#  make_mozpp only handles linux, unix, OS X, or AIX op sys
#------------------------------------------------------------
if( $op_sys == LINUX || $op_sys == UNIX || $op_sys == DARWIN ) then
  set op_sys = LINUX
else if( $op_sys != AIX ) then
  echo "$prog only works for Linux, Unix, OS X, or AIX op systems"
  exit 1
endif

#------------------------------------------------------------
#  check for gnu make utility
#------------------------------------------------------------
foreach file (gmake gnumake)
  which $file >& /dev/null
  if( ! $status ) then
    set gmake_cmd = `which $file` 
  endif
end
if( ! $?gmake_cmd ) then
  echo "can't find gnu make utility; $prog terminating"
  exit -1
endif

#------------------------------------------------------------
#  check for configuration file
#------------------------------------------------------------
set cfg_file = `pwd`
set cfg_file = $cfg_file:h"/configure/MZ4.cfg"
if( -e $cfg_file ) then
  set cfgs_tpl = `cat $cfg_file`
else
  if( $?compiler ) then
    set cfgs_tpl = (NONE NONE NONE NONE NONE NONE NONE NONE)
    set $cfgs_tpl[4] = `pwd`
  else
    echo "can't locate configuration file, MZ4.cfg, and no compiler argument; $prog terminating"
    exit -1
  endif
endif

if( $#argv > 0 ) then
#------------------------------------------------------------
#  process arguments
#------------------------------------------------------------
  set args1 = ($argv)
  set args  = (`echo $args1 | tr '[a-z]' '[A-Z]'`)
  foreach argument ($args)
    if( $args[1] == HELP ) then
      set help_file = docs/README.make.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
    foreach keyword (CMP)
      echo $argument | grep "$keyword=" >& /dev/null
      if( ! $status ) then
        if( $keyword == CMP ) then
          set compiler = `echo $args1[1] | cut -d= -f2`
          set cfgs_tpl[1] = $compiler
        endif
        break
      else
        continue
      endif
    end
    shift args1
  end
endif

#------------------------------------------------------------
#  set working configuration parameters
#------------------------------------------------------------
set cfgs = ($cfgs_tpl)

#------------------------------------------------------------
#  check compiler
#------------------------------------------------------------
if( $cfgs[1] == NONE ) then
  echo "can't build preprocessor; no fortran90 compiler; $prog terminating"
  exit -1
endif

set compiler = $cfgs[1]:t
if( $op_sys == LINUX ) then
  set match_comp = (pgf9 ifort lf9)
else
  set match_comp = xlf
endif

foreach comp ($match_comp)
  echo $compiler | grep $comp >& /dev/null
  if( ! $status ) then
    if( $comp == pgf9 ) then
      set comp_hndl = PGI
    else if( $comp == ifort ) then
      set comp_hndl = Intel
    else if( $comp == lf9 ) then
      set comp_hndl = Lahey
    else if( $comp == xlf ) then
      set comp_hndl = "IBM xlf"
    else
      echo "$compiler is not a supported compiler{$match_comp}; $prog terminating"
      exit -1
    endif
    break
  endif
end

if( ! $?comp_hndl ) then
   echo "$compiler is not a supported compiler{$match_comp}; $prog terminating"
   exit -1
endif

if( $op_sys == LINUX ) then
  set compiler = $comp_hndl
endif

cd src >& /dev/null || echo "failed to cd to src subdirectory; $prog terminating" && exit -1

set objdir = OBJ
if( -d $objdir ) then
  rm -f $objdir/* >& /dev/null
else
  mkdir -p $objdir || echo "failed to create $objdir subdirectory; $prog terminating" && exit -1
endif

$gmake_cmd COMPILER=$compiler OP_SYS=$op_sys SRCDIRS="mozpp_src"

rm -r -f $objdir
