#!/bin/bash
# --------------------------------------------------------------------------- #
# install_ww3_tar: install WAVEWATCH III version 6 from tar archive files     #
#                                                                             #
# -remarks :                                                                  #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      Jose-Henrique Alves    #
#                                                                             #
#                                                      October 2012 (origin)  #
#                                                      July 2013    (latest)  #
#                                                                             #
#    Copyright 2012-2013 National Weather Service (NWS),                      #
#       National Oceanic and Atmospheric Administration.  All rights          #
#       reserved.  WAVEWATCH III is a trademark of the NWS.                   #
#       No unauthorized use without permission.                               #
#                                                                             #
# --------------------------------------------------------------------------- #


# 0. Introductory flair
# --------------------------------------------------------------------------- #

  clear
  echo -e '\n\n\n                  ==================================='
  echo -e '              ------ Installing WAVEWATCH III  v.6 ------'
  echo -e '                  =================================== \n'
  echo -e '                  Script for installing package from tar files. '
  echo -e '                  Requires files in same directory as script.\n'
  echo -en ' Continue? [y|n] '
  read OK
  if [ "$OK" != "Y" ] && [ "$OK" != "y" ]
  then
    echo -e '\n Exiting installation... '
    exit
  fi

# 1. Preparations                                                             #
# --------------------------------------------------------------------------- #
# 1.a Internal variables


  dirs="model regtests smc_docs"
  for dir in $dirs
  do
    fname=wwatch3.*.$dir.tar
    eval "$dir=$fname"
    if ! [ -e $fname ]
    then 
      echo -e "\n One or more files essential to the installation missing."
      echo -e "  $dir file $fname not found, aborting! "
      exit
    fi
  done

# 1.b ID header  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo -e '\n\n\n'
  echo -e '                  ==================================='
  echo -e '              ------ Installing WAVEWATCH III  v.6 ------'
  echo -e '                  ==================================='
  echo -e '                                     from tar source \n'

# 1.c Test present directory and set location of environment file  - - - - - -

  ww3_dir=`pwd`

# homedir may be different than where cd takes...
  if [ $HOME ]
  then
    cd $HOME
  else  
    cd
  fi
  home_dir=`pwd`
  cd $ww3_dir

  echo -e ' This installation requires a configuration file (wwatch3.env).'
  ww3_dir=`pwd`
  main_dir=$ww3_dir/model

  if [ -f ${main_dir}/bin/wwatch3.env ]
  then
    echo -e '\n This is not a new installation at this location.'
    echo -e '  Existing local wwatch3.env will be used. Option to modify below...'
    ww3_env="${main_dir}/bin/wwatch3.env"
  else
    echo -e '\n Creating new env file from scratch.'
    ww3_env="${main_dir}/bin/wwatch3.env"
  fi

  echo -e "\n\n Installing in "
  echo -e "   $ww3_dir"

  OK="$NULL"
  until [ "$OK" = 'y' ] || [ "$OK" = 'Y' ] || \
        [ "$OK" = 'n' ] || [ "$OK" = 'N' ]
  do
    echo -en "\n   OK ? [y/n] "
    read OK
  done
  if [ "$OK" = 'n' ] || [ "$OK" = 'N' ]
  then
    echo -e '\n\n\n Change to correct directory. '
    echo -e '\n\n   --- INSTALL ABORTED --- \n\n' 
    exit
  fi

  mkdir -p $main_dir/bin
  cd $main_dir

# 1.d Set up environment - - - - - - - - - - - - - - - - - - - - - - - - - - -
  
  echo -e '\n\n Setting up environment variables. '

  if [ -s ${ww3_env} ]
  then
    echo -e "   Existing config file found: "
    echo -e "     $ww3_env \n"
    echo -e  ' Current settings: '

    set `grep WWATCH3_LPR $ww3_env` ; shift
    prnt="$*"

    set `grep WWATCH3_F90 $ww3_env` ; shift
    fort="$*"

    set `grep WWATCH3_CC  $ww3_env` ; shift
    cc="$*"

    set `grep WWATCH3_TMP $ww3_env` ; shift
    temp_dir="$*"

    set `grep WWATCH3_SOURCE $ww3_env` ; shift
    s_source="$*"

    set `grep WWATCH3_LIST $ww3_env` ; shift
    s_list="$*"

    newfile='no'

  else
    echo -e '\n\n   Previous setup file not found. Variables will be set to defaults. '
    echo -e '\n     (User must check to see if these setting are appropriate.)      '
    prnt=printer
    fort=gfortran
    cc=gcc
    temp_dir=${main_dir}/tmp
    s_source=yes
    s_list=yes
    do_set='n'

    echo -e '\n\n Creating wwatch3.env locally'
    ww3_env=${main_dir}/bin/wwatch3.env
    newfile='yes'
  fi

    echo -e "      Printer (listings)       : $prnt "
    echo -e "      FORTRAN comp. (aux only) : $fort "
    echo -e "      C Compiler (aux only)    : $cc "
    echo -e "      Scratch directory        : $temp_dir "
    echo -e "      Save source code         : $s_source "
    echo -e "      Save listings            : $s_list "

    OK="$NULL"
    until [ "$OK" = 'y' ] || [ "$OK" = 'Y' ] || \
          [ "$OK" = 'n' ] || [ "$OK" = 'N' ]
    do
      echo -en "\n Update settings ? [y/n] "
      read OK
      case $OK in
       'y'|'Y') do_set='y' ;;
       'n'|'N') do_set='n' ;;
      esac
    done

  if [ "$do_set" = 'y' ]
  then
    
    echo -e '\n\n\n Modifying set-up '
    echo -e '\n Type n new settings, or press ENTER to keep [current ones]: \n'
    newfile='yes'
    OK="$NULL"
    until [ "$OK" = 'y' ] || [ "$OK" = 'Y' ]
    do
      echo -en "      Printer for listings [$prnt] : "
      instr="$NULL" ; read instr
      if [ -n "$instr" ]
      then
        prnt="$instr"
      fi

      echo -en "      Compiler for aux. [$fort] : "
      instr="$NULL" ; read instr
      if [ -n "$instr" ]
      then
        fort="$instr"
      fi

      echo -en "      Compiler for aux. [$cc] : "
      instr="$NULL" ; read instr
      if [ -n "$instr" ]
      then
        cc="$instr"
      fi

      OK="$NULL"
      until [ "$OK" = 'y' ]
      do
        echo -en "      Scratch space [$temp_dir] : "
        instr="$NULL" ; read instr
        if [ -n "$instr" ]
        then
          temp_dir="$instr"
        fi
        if [ -n "$temp_dir" ]
        then
          if [ -d $temp_dir ]
          then
            OK='y'
          else
            if `mkdir $temp_dir` 2> /dev/null
            then
              OK='y'
            fi
            rmdir $temp_dir
          fi
        fi
      done

      echo -en "      Save source code files (*.f)  [$s_source] : "
      instr="$NULL" ; read instr
      if [ -n "$instr" ]
      then
        if [ "$instr" = 'yes' ] || [ "$instr" = 'YES' ]
        then
          s_source='yes'
        else
          s_source='no'
        fi
      fi

      echo -en "      Save listing files  [$s_list] : "
      instr="$NULL" ; read instr
      if [ -n "$instr" ]
      then
        if [ "$instr" = 'yes' ] || [ "$instr" = 'YES' ]
        then
          s_list='yes'
        else
          s_list='no'
        fi
      fi

      echo -e ' '
      echo -e "   Modified settings:"
      echo -e "      Printer (listings)       : $prnt "
      echo -e "      FORTRAN comp. (aux only) : $fort "
      echo -e "      C Compiler (aux only)    : $cc "
      echo -e "      Scratch directory        : $temp_dir "
      echo -e "      Save sources             : $s_source "
      echo -e "      Save listings            : $s_list "
      echo -en "\n   New settings OK ? [y/n]  "
      read OK
      ww3_env=${main_dir}/bin/wwatch3.env
    done
  else
    echo -e '\n Keeping current configuration '
  fi

# 1.e Save environment in file - - - - - - - - - - - - - - - - - - - - - - - -

 if [ "${newfile}" = "yes" ]
 then

#  cd $home_dir
  rm -f $ww3_env

  echo '#'                                                    > $ww3_env
  echo '# Environment variables for wavewatch III'           >> $ww3_env
  echo '# ---------------------------------------'           >> $ww3_env
  echo '#'                                                   >> $ww3_env
  echo ' '                                                   >> $ww3_env
  echo "WWATCH3_LPR      $prnt"                              >> $ww3_env
  echo "WWATCH3_F90      $fort"                              >> $ww3_env
  echo "WWATCH3_CC       $cc"                                >> $ww3_env
  echo "WWATCH3_DIR      $main_dir"                          >> $ww3_env
  echo "WWATCH3_TMP      $temp_dir"                          >> $ww3_env
  echo "WWATCH3_SOURCE   $s_source"                          >> $ww3_env
  echo "WWATCH3_LIST     $s_list"                            >> $ww3_env
  echo ' '                                                   >> $ww3_env

 fi

  if [ ! -d $temp_dir ]
  then
    mkdir $temp_dir
  fi
  cd $temp_dir
  rm -f * 2> /dev/null
  cd $main_dir

  OK="$NULL"
  until [ "$OK" = 'y' ] || [ "$OK" = 'Y' ] || \
        [ "$OK" = 'n' ] || [ "$OK" = 'N' ]
  do
    echo -en '\n Continue with actual implementation ? [y/n] '
    read OK
 done

  if [ "$OK" = 'n' ] || [ "$OK" = 'N' ]
  then
    echo -e '\n\n                    =============================== '
    echo -e '                  ------    End of program     ------ '
    echo -e '                    =============================== \n'
    exit
  else
    echo -e '\n Starting actual implementation ... '
    echo -e '   Use Ctrl-C to terminate program ... '
  fi

# Unpack tar files

  src_dir=$ww3_dir/arc_tmp # Source directory (before links or copies are made)
  mkdir -p $src_dir

  cd $src_dir

  echo -e "Unpacking $model"
  tar xf $ww3_dir/$model
  echo -e "Unpacking $regtests"
  tar xf $ww3_dir/$regtests
  echo -e "Unpacking $smc_docs" 
  cd $ww3_dir  
  tar xf $smc_docs 
 

# Make the model directories (at same level as manual and guide)
  for dir in aux bin ftn obj mod exe inp nml work esmf
  do
    if [ ! -d $dir ]
    then
      echo -e "   making directory model/$dir"
      mkdir -p model/$dir
    fi
  done

# Make the regtests directory  (at same level as manual and guide)
  if [ ! -d regtests ]
    then
    echo -e "   making directory regtests"
    mkdir regtests
  fi
# Make the smc_docs directory  (at same level as manual and guide)
  if [ ! -d smc_docs ]
    then
    echo -e "   making directory smc_docs"
    mkdir smc_docs
  fi

# 2.e Set file permissions - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd $src_dir/model/aux
  chmod 644 *.gs
  chmod 644 spec_ids*
  chmod 644 *.f

  cd $src_dir/model/bin
  chmod 744 *

  cd $src_dir/model/ftn
  chmod 644 *.ftn
  chmod 644 *.f90

  cd $src_dir/model/inp
  chmod 644 *.inp track_i.ww3

  cd $src_dir/model/nml
  chmod 644 *.nml

  cd $src_dir/regtests/bin
  chmod 744 *

# Prelim: setting tmp directory to store screen redirects
  mkdir -p $main_dir/tmp

# --------------------------------------------------------------------------- #
# 3. Set-up / update aux directory                                            #
# --------------------------------------------------------------------------- #

  ldir="aux"
  rm -f ${main_dir}/tmp/${ldir}_setup.out

  cd $main_dir/${ldir}
  all_files=`ls -p $src_dir/model/${ldir} | grep -v '/$'`

  echo -e '\n\n =============================== '
  echo -e '--- Set up / update directories --- '
  echo -e ' ============================================================== '
  echo -e "    Directory `pwd` "

# 3.a FORTRAN executables  - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo -e '\n FORTRAN executables: '
  echo -e ' -------------------- '

  for code in w3adc w3prnt w3list w3split
  do

    echo -e "\n   Program $code ... " | tee ${main_dir}/tmp/${ldir}_setup.out
    if [ -f "$src_dir/model/${ldir}/$code.f" ]
    then
      cp -f $src_dir/model/${ldir}/$code.f .
      echo -e "      New $code.f was copied to $main_dir/${ldir} " | tee ${main_dir}/tmp/${ldir}_setup.out
    else
      echo -e "\n      No source for ${code}.f found, aborting! " | tee ${main_dir}/tmp/${ldir}_setup.out
      exit 30
    fi

    echo -e "      Generating $code in $main_dir/bin " | tee ${main_dir}/tmp/${ldir}_setup.out
    echo -e "         Compiling ... " | tee ${main_dir}/tmp/${ldir}_setup.out
    if `$fort $code.f > fort.out 2> fort.err`
    then
      rm -f $main_dir/bin/$code
      if [ -f a.out ]
      then
        echo -e "         Storing ... " | tee ${main_dir}/tmp/${ldir}_setup.out
        mv a.out $main_dir/bin/$code
      else
        echo -e "\n\n\n      --- ERROR: FILE a.out NOT FOUND --- " | tee ${main_dir}/tmp/${ldir}_setup.out
        echo -e ' Compilation error messages: '
        cat fort.out 
        echo -e ''
        cat fort.err 
        echo -e ''
        exit
       fi
    else
      echo -e "\n\n\n      --- ERROR IN COMPILING $code.f --- " | tee ${main_dir}/tmp/${ldir}_setup.out
      echo -e ' Compilation error messages: '
      cat fort.out 
      echo -e '' 
      cat fort.err 
      echo -e '\n '
      exit
    fi
    rm -f fort.*
    tmp_files=$all_files
    all_files=`echo -e $tmp_files | sed -e "s/$code.f//g"`

  done
  echo -e '\n All essential FORTRAN codes compiled correctly ' | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' ---------------------------------------------- ' | tee ${main_dir}/tmp/${ldir}_setup.out
  sleep 2

# Move on with other files in aux
  tnr_lnk=0
  tnr_new=0
  tnr_old=0
# 3.b GrADS aux scripts  - - - - - - - - - - - - - - - - - - - - - - - - - - -

  for list in 1 2
  do

    case $list in 
     1) echo -e '\n GrADS aux scripts' | tee ${main_dir}/tmp/${ldir}_setup.out
        echo -e ' -----------------'
        cd $src_dir/model/${ldir}
        files=`ls *.gs`
        files="$files spec_ids.gen" ;;
     2) echo -e '\n Other :'
        echo -e ' -------' 
        files=$all_files  ;;
    esac

    cd $main_dir/${ldir}

    nr_lnk=0

    if [ -z "$files" ]
    then
      echo -e '  No files found' | tee ${main_dir}/tmp/${ldir}_setup.out
    else
      for file in $files
      do
        nr_lnk=$(($nr_lnk + 1))
        echo -e "   (re)linking $file ..." >> ${main_dir}/tmp/${ldir}_setup.out 
        cp -f $src_dir/model/${ldir}/$file .
        tmp_files=$all_files
        all_files=`echo -e $tmp_files | sed -e "s/$file//"`
      done
    fi
    echo -e "  This section: $nr_lnk files copied."
    tnr_lnk=$(($tnr_lnk + $nr_lnk))

  done

# 3.c spec_ids file  - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo -e ' '
  echo -e '\n spec_ids file :'
  echo -e ' ---------------'

  cd $main_dir/${ldir}

  nr_new=0
  nr_old=0
  for file in spec_ids
  do
    if [ -f $file ]
    then
      echo -e "   File $file exists (will not be modified)." >> ${main_dir}/tmp/${ldir}_setup.outout
      nr_old=$(($nr_old + 1))
    else
      nr_new=$(($nr_new + 1))
      echo -e "   File $file does not exists." >> ${main_dir}/tmp/${ldir}_setup.out
      echo -e "      Copied from $file.gen (may be modified by user)" >> ${main_dir}/tmp/${ldir}_setup.out
      cp $file.gen $file
    fi
  done
  echo -e "  This section: $nr_old existing files found."
  echo -e "  This section: $nr_new files copied."
  tnr_lnk=$(($tnr_lnk + $nr_new))
  tnr_old=$(($tnr_old + $nr_old))

# Check if subdirectories present
  cd $src_dir/model/${ldir}
  ndirs=`ls -p | grep '/$'`
  issd=`echo $ndirs | awk '{print $1}'`

  cd ${main_dir}/${ldir}

  if [ ${issd} ]
  then
    for sdir in $ndirs
    do
      echo -e "\n Sub-directory ${sdir} found in ${ldir}, copying contents."
      cp -r $src_dir/model/${ldir}/${sdir} . 
    done
  fi

  echo -e "\n\n End of ${ldir} directory section " | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' ---------------------------- '

  if [ "$tnr_old" != '0' ]
  then
    echo -e "\n  Total of $tnr_old existing files found."
  fi
  if [ "$tnr_new" != '0' ]
  then
    echo -e "\n  Total of $tnr_new files copied."
  fi
  echo -e "\n  List of existing (old) and new files copied routed to: "
  echo -e "     ${main_dir}/tmp/${ldir}_setup.out "
  sleep 2

# --------------------------------------------------------------------------- #
# 4. Set-up / update bin directory                                            #
# --------------------------------------------------------------------------- #

  ldir="bin"
  rm -f ${main_dir}/tmp/${ldir}_setup.out

  tnr_lnk=0
  tnr_new=0

  cd $main_dir/${ldir}
  all_files=`ls -p $src_dir/model/${ldir} | grep -v '/$'`

  echo -e '\n\n ==============================='
  echo -e '--- Set up / update directories ---' | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' =============================================================='
  echo -e "    Directory `pwd`" | tee ${main_dir}/tmp/${ldir}_setup.out

# 4.a basic aux scripts  - - - - - - - - - - - - - - - - - - - - - - - - - - -

  nr_lnk=0
  for list in 1 2
  do
    echo -e ' '
    case $list in 
      1) echo -e 'Scripts for compiling and linking :'
         echo -e '-----------------------------------'
         files='ad3_test ad3 w3_make w3_new make_makefile.sh'
         cd $src_dir/model/${ldir}
         #files="$files switch.gen `ls comp.*` `ls link.*`"
         files="$files `ls comp.*` `ls link.*`"
         cd $main_dir/${ldir} ;;
      2) echo -e 'Other :'
         echo -e '-------' 
         files=$all_files  ;;
    esac

    cd $main_dir/${ldir}

    if [ -z "$files" ]
    then
      echo -e '  No files found' | tee ${main_dir}/tmp/${ldir}_setup.out
    else
      for file in $files
      do
        nr_lnk=$(($nr_lnk + 1))
        echo -e "   (re)linking $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
        cp -f $src_dir/model/${ldir}/$file .
        tmp_files=$all_files
        all_files=`echo -e $tmp_files | sed -e "s/$file//"`
      done
    fi

  done
  echo -e "  This section: $nr_lnk files copied." 
  tnr_lnk=$(($tnr_lnk + $nr_lnk))

# 4.b comp and link shell scripts  - - - - - - - - - - - - - - - - - - - - - -
# Updating this to force the user to pick switch, comp and link instead of 
# automatically picking comp/link/switch.gen (which have been deleted)
#    nr_lnk=0
#    nr_old=0
#
#    echo -e ' '
#    echo -e 'Compile and link shell scripts :'
#    echo -e '--------------------------------'
#
#    for file in switch comp link
#    do
#      if [ -f $file ]
#      then
#        echo -e "   File $file exists (will not be modified)." >> ${main_dir}/tmp/${ldir}_setup.out
#        nr_old=$(($nr_old + 1))
#      else
#        nr_lnk=$(($nr_lnk + 1))
#        echo -e "   File $file does not exists." >> ${main_dir}/tmp/${ldir}_setup.out
#        echo -e "      Copied from $file.gen (has to be modified by user)" >> ${main_dir}/tmp/${ldir}_setup.out
#        cp $file.gen $file
#      fi
#    done
#    echo -e "  This section: $nr_old existing (old) links or files found"
#    echo -e "  This section: $nr_lnk files copied"
#    tnr_lnk=$(($tnr_lnk + $nr_lnk))
#    tnr_old=$(($tnr_old + $nr_old))
#
#    chmod 744 comp link
#    chmod 644 switch


  echo -e "\n\n End of ${ldir} directory section " | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' ---------------------------- '

  if [ "$tnr_old" != '0' ]
  then
    echo -e "\n   Total of $tnr_old existing links or files found."
  fi
  if [ "$tnr_new" != '0' ]
  then
    echo -e "\n   Total of $tnr_new files copied."
  fi
  echo -e "\n   List of existing (old) and new files copied routed to:"
  echo -e "     ${main_dir}/tmp/${ldir}_setup.out "
  sleep 2

# --------------------------------------------------------------------------- #
# 5. Set-up / update ftn directory                                            #
# --------------------------------------------------------------------------- #

  ldir="ftn"
  rm -f ${main_dir}/tmp/${ldir}_setup.out

  cd $src_dir/model/${ldir}
  all_files=`ls | grep -v '/$'`
  ftn_files=`ls *.ftn`
  cd $main_dir/${ldir}

  echo -e '\n\n  ==============================='
  echo -e '--- Set up / update directories ---' | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' =============================================================='
  echo -e "    Directory `pwd`" | tee ${main_dir}/tmp/${ldir}_setup.out

  tnr_new=0
  tnr_old=0

  for list in 1 2 3 4
  do
    echo -e ' '
    cd $src_dir/model/${ldir}
    case $list in 
      1) echo -e ' Main programs :'
         echo -e ' --------------' ; files=`ls ww3_*.ftn gx_*.ftn` ;;
      2) echo -e ' Subroutines (.ftn) :'
         echo -e ' --------------------' ; files=$ftn_files  ;;
      3) echo -e ' Subroutines (.f90) :'
         echo -e ' --------------------' ; files=`ls *.f90`  ;;
      4) echo -e ' Other :'
         echo -e ' -------' ; files=`echo -e $all_files | sed -e "s/SCRIP//g" | sed -e "s/PDLIB//g"` ;;
    esac
    cd $main_dir/ftn

    nr_new=0
    nr_old=0

    if [ -z "$files" ]
    then
      echo -e '  No files found'
    else
      for file in $files
      do
        if [ -f $file ]
        then
          nr_old=$(($nr_old + 1))
          echo -e "   Old link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out 
        else
          nr_new=$(($nr_new + 1))
          echo -e "   New link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out 
        fi
        cp -f $src_dir/model/${ldir}/$file .
        tmp_files=$all_files
        all_files=`echo -e $tmp_files | sed -e "s/$file//"`
        tmp_files=$ftn_files
        ftn_files=`echo -e $tmp_files | sed -e "s/$file//"`
      done
    fi

    tnr_new=$(($tnr_new + $nr_new))
    tnr_old=$(($tnr_old + $nr_old))

    if [ "$nr_old" != '0' ]
    then
      echo -e "  This section: $nr_old existing files found."
    fi
    if [ "$nr_new" != '0' ]
    then
      echo -e "  This section: $nr_new files copied."
    fi

  done

# Deal with subdirectory SCRIP
  cd $src_dir/model/ftn/SCRIP
  files=`ls`
  nr_old=0
  nr_new=0
  if [ ! -d $main_dir/ftn/SCRIP ]
  then
    mkdir -p $main_dir/ftn/SCRIP
  fi
  cd $main_dir/ftn/SCRIP
 
  for file in $files
  do
    if [ -f $file ]
    then
      nr_old=$(($nr_old + 1))
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    else
      nr_new=$(($nr_new + 1))
      echo -e "   New link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    fi
    cp -f $src_dir/model/ftn/SCRIP/$file .
  done

# Deal with subdirectory PDLIB
  cd $src_dir/model/ftn/PDLIB
  files=`ls`
  nr_old=0
  nr_new=0
  if [ ! -d $main_dir/ftn/PDLIB ]
  then
    mkdir -p $main_dir/ftn/PDLIB
  fi
  cd $main_dir/ftn/PDLIB

  for file in $files
  do
    if [ -f $file ]
    then
      nr_old=$(($nr_old + 1))
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    else
      nr_new=$(($nr_new + 1))
      echo -e "   New link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    fi
    cp -f $src_dir/model/ftn/PDLIB/$file .
  done


  tnr_new=$(($tnr_new + $nr_new))
  tnr_old=$(($tnr_old + $nr_old))

  echo -e "\n\n End of ${ldir} directory section " | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' ---------------------------- '

  if [ "$tnr_old" != '0' ]
  then
    echo -e "\n   Total of $tnr_old existing files found."
  fi
  if [ "$tnr_new" != '0' ]
  then
    echo -e "\n   Total of $tnr_new files copied."
  fi
  echo -e "\n   List of existing (old) or files copie routed to:"
  echo -e "      ${main_dir}/tmp/${ldir}_setup.out "
  sleep 2

# --------------------------------------------------------------------------- #
# 5.1 update esmf directory                                                   #
# --------------------------------------------------------------------------- #

  ldir="esmf"
  rm -f ${main_dir}/tmp/${ldir}_setup.out
  nr_old=0
  nr_new=0
  cd $src_dir/model/${ldir}
  files=`ls`
  cd $main_dir/${ldir}
  for file in $files
  do
    if [ -f $file ]
    then
      nr_old=$(($nr_old + 1))
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    else
      nr_new=$(($nr_new + 1))
      echo -e "   New link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    fi
    cp -f $src_dir/model/esmf/$file .
  done
  tnr_new=$(($tnr_new + $nr_new))
  tnr_old=$(($tnr_old + $nr_old))

  echo -e "\n\n End of ${ldir} directory section " | tee ${main_dir}/tmp/${ldir}_setup.out
  echo -e ' ---------------------------- '

  if [ "$tnr_old" != '0' ]
  then
    echo -e "\n   Total of $tnr_old existing files found."
  fi
  if [ "$tnr_new" != '0' ]
  then
    echo -e "\n   Total of $tnr_new files copied."
  fi
  echo -e "\n   List of existing (old) or files copie routed to:"
  echo -e "      ${main_dir}/tmp/${ldir}_setup.out "
  sleep 2


# --------------------------------------------------------------------------- #
# 6. Set-up / update inp directory                                            #
# --------------------------------------------------------------------------- #

  rm -f ${main_dir}/tmp/inp_setup.out

  cd $src_dir/model/inp
  files=`ls ww3_*.inp track_i.ww3`
  files="$files `ls gx_*.inp`"
  cd $main_dir/inp
  echo -e ' '

  echo -e '\n\n ==============================='
  echo -e '--- Set up / update directories ---' | tee ${main_dir}/tmp/inp_setup.out
  echo -e ' =============================================================='    
  echo -e "    Directory `pwd`" | tee ${main_dir}/tmp/inp_setup.out
  echo -e ' '

  nr_old=0
  nr_new=0

  echo -e ' Input files (.inp) :'
  echo -e ' ------------------'

  for file in $files
  do
    if [ -f $file ]
    then
      nr_old=$(($nr_old + 1))
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/inp_setup.out
    else
      nr_new=$(($nr_new + 1))
      echo -e "   New link $file ..." >> ${main_dir}/tmp/inp_setup.out
    fi
    cp -f $src_dir/model/inp/$file .
  done

  if [ "$nr_old" != '0' ]
  then
    echo -e "  This section: $nr_old existing files found."
  fi
  if [ "$nr_new" != '0' ]
  then
    echo -e "  This section: $nr_new files copied."
  fi

  echo -e '\n\n End of inp directory section '
  echo -e ' ---------------------------- '

  if [ "$nr_old" != '0' ]
  then
    echo -e "\n   Total of $nr_old existing files found."
  fi
  if [ "$nr_new" != '0' ]
  then
    echo -e "\n   Total of $nr_new files copied."
  fi
  echo -e "\n   List of existing (old) or files copied routed to :"
  echo -e "     ${main_dir}/tmp/inp_setup.out "
  sleep 2

# --------------------------------------------------------------------------- #
# 6b. Set-up / update nml directory                                            #
# --------------------------------------------------------------------------- #
  rm -f ${main_dir}/tmp/nml_setup.out

  cd $src_dir/model/nml
  files=`ls ww3_*.nml`
#  files="$files `ls gx_*.nml`"
  cd $main_dir/nml
  echo -e ' '

  echo -e '\n\n ==============================='
  echo -e '--- Set up / update directories ---' | tee ${main_dir}/tmp/nml_setup.out
  echo -e ' =============================================================='    
  echo -e "    Directory `pwd`" | tee ${main_dir}/tmp/nml_setup.out
  echo -e ' '

  nr_old=0
  nr_new=0

  echo -e ' Input files (.nml) :'
  echo -e ' ------------------'

  for file in $files
  do
    if [ -f $file ]
    then
      nr_old=$(($nr_old + 1))
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/nml_setup.out
    else
      nr_new=$(($nr_new + 1))
      echo -e "   New link $file ..." >> ${main_dir}/tmp/nml_setup.out
    fi
    cp -f $src_dir/model/nml/$file .
  done

  if [ "$nr_old" != '0' ]
  then
    echo -e "  This section: $nr_old existing files found."
  fi
  if [ "$nr_new" != '0' ]
  then
    echo -e "  This section: $nr_new files copied."
  fi

  echo -e '\n\n End of nml directory section '
  echo -e ' ---------------------------- '

  if [ "$nr_old" != '0' ]
  then
    echo -e "\n   Total of $nr_old existing files found."
  fi
  if [ "$nr_new" != '0' ]
  then
    echo -e "\n   Total of $nr_new files copied."
  fi
  echo -e "\n   List of existing (old) or files copied routed to :"
  echo -e "     ${main_dir}/tmp/nml_setup.out "
  sleep 2


# --------------------------------------------------------------------------- #
# 7.1 Set-up / update regtests directory                                           #
# --------------------------------------------------------------------------- #

  rm -f ${main_dir}/tmp/reg_setup.out
  tnr_old=0
  tnr_new=0

  cd $src_dir/regtests

  echo -e '\n\n ================================'
  echo -e '--- Set up / update directories ---' | tee ${main_dir}/tmp/reg_setup.out
  echo -e ' =============================================================='    
  echo -e "    Directory `pwd`" | tee ${main_dir}/tmp/reg_setup.out

  tmp_dirs=$(find . ! -path . -type d)

  for dir in $tmp_dirs
  do
    lngth=`echo $dir | wc -c`
    dir=`echo $dir | cut -c3-$lngth`
    nr_old=0
    nr_new=0

    if [ ! -d $ww3_dir/regtests/$dir ]
    then
      mkdir $ww3_dir/regtests/$dir
      echo -e "\n Making directory $dir ..."
    else
      echo -e "\n Checking directory $dir ..."
    fi

    cd $src_dir/regtests/$dir
    files=`ls -p | grep -v '/$'`
    cd $ww3_dir/regtests/$dir

    if [ -z "$files" ]
    then
      echo -e '  No files found'
    else
      for file in $files
      do
        if [ -f $file ]
        then
          nr_old=$(($nr_old + 1))
          echo -e "      Old link $file ..." >> ${main_dir}/tmp/reg_setup.out
        else
          nr_new=$(($nr_new + 1))
          echo -e "      New link $file ..." >> ${main_dir}/tmp/reg_setup.out
        fi
        cp -f $src_dir/regtests/$dir/$file .
        tmp_files=$all_files
        all_files=`echo -e $tmp_files | sed -e "s/$file//"`
      done
      if [ "$nr_old" != '0' ]
      then
        echo -e "  This section: $nr_old existing files found."
      fi
      if [ "$nr_new" != '0' ]
      then
        echo -e "  This section: $nr_new new files copied." 
      fi
 
    fi

    tnr_old=$(($tnr_old + $nr_old))
    tnr_new=$(($tnr_new + $nr_new))
 
  done

  cd $ww3_dir/regtests

  chmod 744 bin/*

  echo -e '\n\n End of regtests directory section '
  echo -e ' ---------------------------------- '

  if [ "$tnr_old" != '0' ]
  then
    echo -e "\n   Total of $tnr_old existing files found."
  fi
  if [ "$tnr_new" != '0' ]
  then
    echo -e "\n   Total of $tnr_new files copied."
  fi
  echo -e "\n   List of existing (old) or files copied routed to :"
  echo -e "     ${main_dir}/tmp/reg_setup.out "
  sleep 2


# --------------------------------------------------------------------------- #
# 8. Set-up / update work directory                                           #
# --------------------------------------------------------------------------- #

  cd $main_dir/work


  echo -e ' \n\n ==============================='
  echo -e '--- Set up / update directories ---'
  echo -e ' =============================================================='    
  echo -e "    Directory `pwd`"

## 8.a comp link and switch
#  Requiring user to do this, so this is no more. 
#  echo -e ' '
#  echo -e ' Setting up links to comp link and switch ...'
#  ln -sf ../bin/switch
#  ln -sf ../bin/comp
#  ln -sf ../bin/link

# 8.b selected GrADS scripts

  echo -e ' Setting up links to selected GrADS scripts ...'
  for file in cbarn.gs colorset.gs spec.gs source.gs 1source.gs spec_ids
  do
    if [ ! -f $file ]
    then
      ln -sf ../aux/$file .
    fi
  done

# 8.c input files

  echo -e ' Setting up links to input files ...'
  cd $main_dir/inp
  files=`ls`
  cd $main_dir/work

  for file in $files
  do
    if [ ! -f $file ]
    then
      ln -sf ../inp/$file .
    fi
  done

  sleep 2

# --------------------------------------------------------------------------- #
# 7. Clean up arc_tmp
# --------------------------------------------------------------------------- #

  rm -rf $src_dir

  mkdir -p ${ww3_dir}/arc
  mv ${ww3_dir}/wwatch3.*.tar* ${ww3_dir}/arc

# --------------------------------------------------------------------------- #
# 9. The end                                                                  #
# --------------------------------------------------------------------------- #

  echo -e '\n\n =============================== '
  echo -e ' --- Final remarks ---'
  echo -e ' ============================================================== \n'    
  echo -e ' To run the WAVEWATCH III executables and the scripts to generate '
  echo -e ' and update these executables from arbitrary directories, add the'
  echo -e ' following directories to the path of your interactive shell : \n'
  echo -e "      $main_dir/bin"
  echo -e "      $main_dir/exe"
  echo -e "\n Note that 'comp' and 'link' and 'switch' are user/machine specific."
  echo -e "\n   Several comp and link files for known compilers are found in:"
  echo -e "   ${main_dir}/bin"
  echo -e "\n   If you cannot find one that suits your machine/preferences, "
  echo -e "   create custom scripts based on the existing ones and add to bin."

  echo -e '\n\n                    ==============================='
  echo -e '                  ---       End of program        --- '
  echo -e '                    =============================== \n '

# end of script ------------------------------------------------------------- #