#!/bin/csh -f

if ($#argv != 4) then
 echo "Installation script help message.  This script installs codes needed for running WRF-STILT on Pleiades"
 echo "Before running this:"
 echo " - need to download the tar ball(s) for s/w and static to $HOME/scitor"
 echo " - need to update the .cshrc file so it contains the right modules and setenv statements"
 echo "   (model this after the .cshrc.20100709 file in the local_files directory in the s/w tar ball)"
 echo "Need 4 args: "
 echo " 1 - tag identifying s/w tar ball, e.g. 'no' to skip this, 'pleiades_v32_01' for a tar ball named NWP-pleiades_v32_01.tar.gz"
 echo " 2 - WRF casename, e.g. 'no' to skip, 'SLC-case03' for installing static files from $HOME/scitor/SLC-case03-static.tar.gz"
 echo " 3 - name of nobackup disk, e.g. nobackup for /nobackup/$USER"
 echo " 4 - wrf (default) or nowrf (for skipping installation of WRF-related software)"
 exit 1
endif
set tagname=$1
set staticinstall=$2
set nobdisk=$3
set wrforno=$4

if ("a$tagname" != "ano") then
 echo -n "++++ Starting s/w installation for tagname=$tagname at: ";date
 cd $HOME/scitor/NWP
 set datestr=`date '+%Y%m%d'`
 set STILT_SCRIPTS=/u/$USER/scitor/NWP/p1564/bin
 set STILT_HOME=/u/$USER/scitor/NWP/p1229
 /bin/rm -fr p1564
 /bin/rm -fr nCast-wrf
 /bin/rm -fr local_files
 /bin/rm -fr generic
 /bin/rm -fr p1229
 /bin/rm -fr arl-format
 if ("a$wrforno" == "awrf") then
    echo "Installing WRF-related software"
    /bin/rm -fr mm5_tar
    /bin/rm -fr WPS
    /bin/rm -fr WPPV3
    /bin/rm -fr WRF_util
    /bin/rm -fr WRFV3
 else
    echo "Skipping WRF-related software"
 endif
 tar zxf ../NWP-${tagname}.tar.gz
 foreach file ( local_files/* )
  echo "Copying file $file to NWP directory"
  /bin/cp -p $file $file:t 
 end
 if (-e generic/scripts/make-patch.pl) then
    echo "Replacing local copy by link to generic/scripts/make-patch.pl"
    if (-e make-patch.pl) /bin/rm make-patch.pl
    ln -sf generic/scripts/make-patch.pl .
 endif
 chmod +x qsub_pl make-patch.pl
 foreach file ( local_files/.cshrc* )
  echo "Copying file $file to home directory"
  /bin/cp $file $HOME/$file:t
 end

 # stilt
 cd p1229/stilt/src
 echo -n "++++ Now in stilt/src: ";pwd
 foreach file ( Makefile module_defsize.f90)
  /bin/mv ${file} ${file}.org
  /bin/cp ${STILT_SCRIPTS}/${file} .
  echo "Replaced ${file}.org by ${file}, diffs are:"
  diff ${file}.org ${file}
 end
 echo -n "++++ Starting stilt/src compilation at: ";date
 make SITE=Pleiades STYLE=opt >&! compile-${datestr}.out
 echo "Executables created:"
 /bin/ls -l | grep hymodelc
 cd ../multi
 echo -n "++++ Starting stilt/multi compilation at: ";date
 make FC=ifort CFLAGS='-check bounds -free -O -w -I. -assume byterecl -mcmodel=medium'  >&! compile-${datestr}.out
 echo "Executables created:"
 /bin/ls -l | grep hymodelc
 cd ../..
 mkdir -p Exe/Copy1
 cd Exe/Copy1
 echo -n "++++ Now creating links in: ";pwd
 ln -s ../../stilt/bdyfiles/ROUGLEN.ASC
 ln -s ../../stilt/bdyfiles/LANDUSE.ASC
 ln -s ../../stilt/bdyfiles/ASCDATA.CFG
 ln -s ../../stilt/multi/hymodelc
 ls -l
 ls -lL

 cd ${STILT_HOME}/stiltR
 echo -n "++++ Now in stiltR: ";pwd
 foreach file ( stilt.r )
  /bin/mv ${file} ${file}.org
  /bin/cp ${STILT_SCRIPTS}/${file} .
  echo "Replaced ${file}.org by ${file}, diffs are:"
  diff ${file}.org ${file}
 end
 foreach file ( Trajeccheck.r )
  /bin/cp ${STILT_SCRIPTS}/${file} .
  echo "Added ${file}"
 end
 cd ../..

 # arl-format
 cd arl-format
 echo -n "++++ Now in arl-format: ";pwd
 echo -n "++++ Running install-arl.csh at: ";date
 csh install-arl.csh ifort >&! install-arl-${datestr}.out
 echo "Executables created:"
 /bin/ls -l | egrep '.x$'
 /bin/ls -l data2arl/wrfgrib2arl | grep wrfgrib2arl
 /bin/ls -l data2arl/wrfnc2arl | grep wrfnc2arl

 if ("a$wrforno" == "awrf") then
  # WRFV3
  echo "Checking for WRF version number for configure.wrf file: v32 or v33"
  set wrfvnn=unknown
  foreach test_vnn ( v32 v33 )
    echo $tagname | grep $test_vnn
    set notfound=$status
    if ($notfound == 0) set wrfvnn=wrf${test_vnn}
  end
  if ("a$wrfvnn" == "aunknown") then
     echo "Cannot determine v32 or v33 from tagname=$tagname, aborting"
     exit 1
  else 
     echo "Using configure file configure.wrf.pleiades.${wrfvnn}"
  endif
  /bin/cp -p WRF_util/inhouse_util/configure.wrf.pleiades.${wrfvnn} WRFV3/configure.wrf
  cd p1229/wrfv2
  perl ../../make-patch.pl -list wrf-patch.in -inst $HOME/scitor/NWP/WRFV3 -verbose
  cd ../../WRFV3
  echo -n "++++ compiling WRFV3 at: ";date
  ./compile em_real >&! compile-${datestr}.out
  echo "Executables created:"
  /bin/ls -l main | grep exe
  cd ..
  foreach file ( mm5_tar/*gz )
   tar zxf $file
  end
  # WPS
  /bin/cp -p WRF_util/inhouse_util/configure.wps.pleiades WPS/configure.wps
  cd WRF_util/inhouse_util
  perl ../../make-patch.pl -list wps-patch.in -inst $HOME/scitor/NWP/WPS -verbose
  cd ../../WPS
  csh wps-scripts-install.csh
  echo -n "++++ compiling WPS at: ";date
  ./compile >&! compile-${datestr}.out
  echo "Executables created:"
  /bin/ls -l | grep exe
  /bin/ls -lL | grep exe
  cd ..
  # WPPV3
  sed -e "/^WRF_DIR/s/tnehrkor/$USER/" WRF_util/inhouse_util/configure.wpp.pleiades >! WPPV3/configure.wpp
  echo "diff WRF_util/inhouse_util/configure.wpp.pleiades WPPV3/configure.wpp"
  diff WRF_util/inhouse_util/configure.wpp.pleiades WPPV3/configure.wpp
  cd WPPV3
  echo -n "++++ compiling WPPV3 at: ";date
  ./compile >&! compile-${datestr}.out
  echo "Executables created:"
  /bin/ls -l exec | grep exe
  cd ..
 endif

 cd ..
 csh generic/scripts/fixgroup s0930
 csh generic/scripts/fixpermissions

 echo -n "++++ Finished s/w installation at: ";date

endif

if ("a$staticinstall" != "ano") then
 # Install static files for WRF runs
 # (tar balls in ~/scitor)

 echo -n "++++ Starting static installation at: ";date
 echo "============ WARNING: this part is untested, make sure that everything worked! ============="

 mkdir -p /${nobdisk}/$USER/test1
 cd /${nobdisk}/$USER/test1
 echo -n "++++ Now in test1: ";pwd
 tar zxf $HOME/scitor/${staticinstall}-static.tar.gz
 set tsfile=` /bin/ls /u/tnehrkor/scitor/NWP/nCast-wrf/etc/tslist* | grep ${staticinstall}`
 echo "Copying the following tslist files to static directories.  Copying $tsfile to just plain 'tslist'"
 /bin/ls -l /u/tnehrkor/scitor/NWP/nCast-wrf/etc/tslist*
 foreach dir ( `find . -type d -name static -print | grep -v run_` )
   echo "/bin/cp -p $HOME/scitor/NWP/nCast-wrf/etc/tslist* $dir"
   /bin/cp -p $HOME/scitor/NWP/nCast-wrf/etc/tslist* $dir
   echo "/bin/cp -p $tsfile $dir/tslist"
   /bin/cp -p $tsfile $dir/tslist
   /bin/ls -l $dir/tslist*
 end
# put a copy of NARRfixed.wps in ~/scitor
  if (-e $HOME/scitor/NARRfixed.wps == 0) then
   echo "Copying /u/tnehrkor/scitor/NARRfixed.wps to $HOME/scitor"
   cp -p /u/tnehrkor/scitor/NARRfixed.wps $HOME/scitor
  endif
# edit namelist.wps: location of NARRfixed.wps
  set orgfiles = ( ${staticinstall}/static/namelist.wps )
  set orgdir="/data/ncep-data/narr/"
  set newdir="/u/${USER}/scitor/"
  set sedorgdir=`echo $orgdir | sed -e 's/\//\\\//g'`
  echo $sedorgdir
  set sednewdir=`echo $newdir | sed -e 's/\//\\\//g'`
  foreach file ( $orgfiles )
   /bin/mv $file $file.org
   sed -e "/NARRfixed.wps/s/$sedorgdir/$sednewdir/" $file.org > $file
   echo "diff $file.org $file"
   diff $file.org $file
  end

# set up workdir, indir, outdir
  mkdir -p /${nobdisk}/$USER/workdir
  mkdir -p /${nobdisk}/$USER/indir
  mkdir -p /${nobdisk}/$USER/outdir
 echo -n "++++ Finished static installation at: ";date

endif
