#!/bin/bash
# --------------------------------------------------------------------------- #
# install_ww3_tar: install WAVEWATCH III version 4 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.4 ------'
  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

  src_dir=arc_tmp # Source directory (before links or copies are made)

  dirs="model cases 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  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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

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

  main_dir=`pwd`

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

  echo -e ' This installation requires a configuration file (wwatch3.env).'
  echo -e ' The current version allows two types of env files: '
  echo -e ' - A local [L] wwatch3.env (Allowing multiple independent installations).'
  echo -e ' - A generic [G] dot-file .wwatch3.env (Old-fashioned option).'
  echo -e ' [L] Installs new, uses existing or updates env file in current directory.'
  echo -e ' [G] Installs new, uses existing or updates env file in home directory, '
  echo -e "     (home is presumably $HOME}). \n"
  echo -en ' Type your choice now: '
  read sww3dir

  if [ "${sww3dir}" = "G" ] || [ "${sww3dir}" = "g" ]
  then

    ww3_env="${home_dir}/.wwatch3.env"   # setup file

    if [ -s $ww3_env ]
    then
      set `grep WWATCH3_DIR $ww3_env` ; shift ; main_dir="$*"
      echo -e "\n Install directory: " 
      echo -e "  $main_dir "
# Check if current location is same as in existing env file
      cur_dir=`pwd`
      if [ "${cur_dir}" != "${main_dir}" ]
      then
        echo -e "\n Current directory is not the same as WW3 directory "
        echo -e " in your existing .wwatch3.env (env) file."
        echo -e " This will cause problems for a G-type install."
        echo -e " Options: (1) ensure install is on dir listed in env,"
        echo -e " (2) install then copy to dir listed in env, or "
        echo -e " (3) create symbolic link between install dir and dir"
        echo -e " listed in env."  
      fi
    else
      echo -e "\n\n Generic dot-file .wwatch3.env file not found in "
      echo -e "    ${home_dir} \n"
      echo -en "\n Create .wwatch3.env file from scratch? [y|n] : "
      read wnew
      if [ "${wnew}" = "Y" ] || [ "${wnew}" = "y" ]
      then
        echo -e '\n Creating generic .wwatch3.env'
      else
        echo -e ' Enter correct location of env file relative to there.' 
        echo -e ' Alternatively, quit and choose local env file [option L] \n '
        main_dir=nodir.$$
        until [ -d $main_dir ]
        do
          echo -en "   Directory (relative to $home_dir) : "
          read OK
          if [ -n "$OK" ]
          then
            main_dir="$OK"
          fi
        done
        cd $main_dir
        main_dir=`pwd`
      fi
    fi
  elif [ "${sww3dir}" = "L" ] || [ "${sww3dir}" = "l" ]
  then
     main_dir=`pwd`
     if [ -f ${main_dir}/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}/wwatch3.env"
    else
      echo -e '\n Creating new env file from scratch.'
      ww3_env="${main_dir}/wwatch3.env"
    fi
 else
      echo -e "\n\n\n Chosen install type ${sww3dir} not available, aborting. \n"
      exit
 fi

 echo -e "\n\n Installing in "
  echo -e "   $main_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

  cd $main_dir

# 1.d Set up environment - - - - - - - - - - - - - - - - - - - - - - - - - - -
  clear
  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_F77 $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=f77
    cc=cc
    temp_dir=${main_dir}/tmp
    s_source=yes
    s_list=yes
    do_set='n'

    echo -e '\n\n Creating wwatch3.env locally (also in home if G option chosen). '
    ww3_env=${main_dir}/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
    clear
    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}/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_F77      $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

  if [ "${sww3dir}" = "G" ] || [ "${sww3dir}" = "g" ]
  then
    cp ${ww3_env} ${home_dir}/.wwatch3.env
  fi
 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
    clear
 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

  mkdir -p ${main_dir}/${src_dir}

  cd ${main_dir}/${src_dir}

  echo -e "Unpacking $model"
  tar xf ../$model
  echo -e "Unpacking $regtests"
  tar xf ../$regtests
  echo -e "Unpacking $cases"
  tar xf ../$cases
  echo -e "Unpacking $smc_docs" 
  tar xf ../$smc_docs 

# Placeholder for cases
#  echo -e "Unpacking $cases"
#  tar xf ../$cases

  cd ${main_dir}/${src_dir}/model
  mv aux auxx
  cd ${main_dir}

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

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

  cd $main_dir/${src_dir}/model/auxx
  chmod 644 *.gs
  chmod 644 spec_ids*
  chmod 644 *.f

  cd $main_dir/${src_dir}/model/bin
  chmod 644 switch* comp* link*
  chmod 744 install*

  cd $main_dir/${src_dir}/model/ftn
  chmod 644 *.ftn
  chmod 644 *.f90

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

  cd $main_dir/${src_dir}/model/nml
  chmod 644 *.nml

#  cd $main_dir/${src_dir}/cases
#  chmod 744 mww3_case_*
#  chmod 755 mww3_data_*

  cd $main_dir/${src_dir}/regtests/bin
  chmod 744 *

# cases loop placeholder
#  for dir in 00 01 02 03
#  do
#    cd $main_dir/${src_dir}/cases/mww3_data_$dir
#    if [ "$dir" = '00' ]
#    then
#      chmod 644 gfs*
#      chmod 644 ice*
#    else
#      chmod 644 *.bot
#      chmod 644 *.obs*
#      chmod 644 *.mask
#      chmod 644 *.gs
#    fi
#    chmod 644 ww3_*
#
#  done

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

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

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

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

  clear
  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 "$main_dir/${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 auxx
  tnr_lnk=0
  tnr_new=0
  tnr_old=0
# 3.b GrADS auxx scripts  - - - - - - - - - - - - - - - - - - - - - - - - - - -

  for list in 1 2
  do

    case $list in 
     1) echo -e '\n GrADS auxx scripts' | tee ${main_dir}/tmp/${ldir}_setup.out
        echo -e ' -----------------'
        cd $main_dir/${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=`expr $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=`expr $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=`expr $nr_old + 1`
    else
      nr_new=`expr $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=`expr $tnr_lnk + $nr_new`
  tnr_old=`expr $tnr_old + $nr_old`

# Check if subdirectories present
  cd ${main_dir}/${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 ${main_dir}/${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 $main_dir/${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 auxx 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 $main_dir/${src_dir}/model/${ldir}
         files="$files switch.gen `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=`expr $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=`expr $tnr_lnk + $nr_lnk`

# 4.b comp and link shell scripts  - - - - - - - - - - - - - - - - - - - - - -
    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=`expr $nr_old + 1`
      else
        nr_lnk=`expr $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=`expr $tnr_lnk + $nr_lnk`
    tnr_old=`expr $tnr_old + $nr_old`

    chmod 744 comp link
    chmod 644 switch

# Local install: delete ww3_env, create new variable pointing to local wwatch3.env
  if [ "${sww3dir}" = "L" ] || [ "${sww3dir}" = "l" ]
  then

    loc_dir=$(echo $main_dir | sed 's/\//\\\//g')
    echo -e "\n This install uses a local wwatch3.env.\n"
    echo -e " Pointing ww3_env variable in bin scripts to:\n"
    echo -e " ${loc_dir}/wwatch3.env \n"

    cd $main_dir/${ldir}

    linkfiles=`ls link*`
    locfiles="${linkfiles} ad3 ad3_test all_switches find_switch list ln3 \
              make_makefile.sh make_MPI make_OMP sort_all_switches sort_switch \
              w3_clean w3_make w3_new w3_setup ww3_gspl.sh"
    for lfile in ${locfiles}
    do

      echo -e "\n Changing ww3_env in script ${lfile}"
# Delete generic ww3_env
      sed '/ww3_env="${HOME}\/\.wwatch3.env"/d' ${lfile} > dumfile
# Add new local ww3_env
      sed -e "s/# WW3ENV/  export ww3_env=$loc_dir\/wwatch3.env/g" dumfile > ${lfile}
      chmod 744 ${lfile}
    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 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 $main_dir/${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 $main_dir/${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"` ;;
    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=`expr $nr_old + 1`
          echo -e "   Old link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out 
        else
          nr_new=`expr $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=`expr $tnr_new + $nr_new`
    tnr_old=`expr $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 $main_dir/${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=`expr $nr_old + 1`
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    else
      nr_new=`expr $nr_new + 1`
      echo -e "   New link $file ..." >> ${main_dir}/tmp/${ldir}_setup.out
    fi
    cp -f ../../${src_dir}/model/ftn/SCRIP/$file .
  done

  tnr_new=`expr $tnr_new + $nr_new`
  tnr_old=`expr $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 $main_dir/${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=`expr $nr_old + 1`
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/inp_setup.out
    else
      nr_new=`expr $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 $main_dir/${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=`expr $nr_old + 1`
      echo -e "   Old link $file ..." >> ${main_dir}/tmp/nml_setup.out
    else
      nr_new=`expr $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. Set-up / update cases directory                                           #
# --------------------------------------------------------------------------- #

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

  cd $main_dir/${src_dir}/cases

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

  tmp_files=`ls`
  all_files=
  all_dirs=
  for file in $tmp_files
  do
    if [ -d $file ]
    then
      all_dirs="$all_dirs $file"
    else
      all_files="$all_files $file"
    fi
  done
  cd $main_dir/cases
  echo -e ' '

# 7.a Sets of test cases

  for list in 1 2
  do
    echo -e ' '
    cd ../${src_dir}/cases
    case $list in 
      1) echo -e 'Multi-scale tests :' ; \
         echo -e '-------------------' ; \
               files=`ls mww3_case_*` ;;
      2) echo -e 'Other tests :' ; \
         echo -e '-------------' ; files=$all_files ;;
    esac
    cd $main_dir/cases
   
    nr_old=0
    nr_new=0

    if [ -z "$files" ]
    then 
      echo -e '  No files found'
    else
      for file in $files
      do
        if [ -f $file ]
        then
          nr_old=`expr $nr_old + 1`
          echo -e "   Old link $file ..." >> ${main_dir}/tmp/cases_setup.out
        else
          nr_new=`expr $nr_new + 1`
          echo -e "   New link $file ..." >> ${main_dir}/tmp/cases_setup.out
        fi
        cp -f ../${src_dir}/cases/$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
  done
  tnr_old=`expr $tnr_old + $nr_old`
  tnr_new=`expr $tnr_new + $nr_new`

# 7.b Directories

  echo -e ' '
  echo -e 'Data directories :'
  echo -e '------------------'

  for dir in $all_dirs
  do

    nr_new=0
    nr_old=0

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

    cd $main_dir/${src_dir}/cases/$dir
    files=`ls`
    cd $main_dir/cases/$dir
    nr_old=0

    if [ -z "$files" ]
    then 
      echo -e '  No files found'
    else
      for file in $files
      do
        if [ -f $file ]
        then
          nr_old=`expr $nr_old + 1`
          echo -e "      Old link $file ..." >> ${main_dir}/tmp/cases_setup.out
        else
          nr_new=`expr $nr_new + 1`
          echo -e "      New link $file ..." >> ${main_dir}/tmp/cases_setup.out
        fi
        cp -f ../../${src_dir}/cases/$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=`expr $tnr_old + $nr_old`
    tnr_new=`expr $tnr_new + $nr_new`

  done


  cd $main_dir/cases
  cp -f ../bin/switch .

  echo -e '\n\n End of cases 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/cases_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 $main_dir/${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=`ls`

  for dir in $tmp_dirs
  do

    nr_old=0
    nr_new=0

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

    cd $main_dir/${src_dir}/regtests/$dir
    files=`ls -p | grep -v '/$'`
    ndirs=`ls -p | grep '/$'`
    cd $main_dir/regtests/$dir

    if [ -z "$files" ]
    then
      echo -e '  No files found'
    else
      for file in $files
      do
        if [ -f $file ]
        then
          nr_old=`expr $nr_old + 1`
          echo -e "      Old link $file ..." >> ${main_dir}/tmp/reg_setup.out
        else
          nr_new=`expr $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=`expr $tnr_old + $nr_old`
    tnr_new=`expr $tnr_new + $nr_new`
 

    for ndir in $ndirs
    do
      lngth=`echo $ndir | wc -c`
      lngth=`expr $lngth - 2`
      ndir=`echo $ndir | cut -c1-$lngth`
      nr_old=0
      nr_new=0

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

      cd $main_dir/${src_dir}/regtests/$dir/$ndir
      files=`ls -p | grep -v '/$'`
      sdirs=`ls -p | grep '/$'`
      cd $main_dir/regtests/$dir/$ndir

      if [ -z "$files" ]
      then
        echo -e '  No files found'
      else
        for file in $files
        do
          if [ -f $file ]
          then
            nr_old=`expr $nr_old + 1`
            echo -e "      Old link $file ..." >> ${main_dir}/tmp/reg_setup.out
          else
            nr_new=`expr $nr_new + 1`
            echo -e "      New link $file ..." >> ${main_dir}/tmp/reg_setup.out
          fi
          cp -f ../../../${src_dir}/regtests/$dir/$ndir/$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=`expr $tnr_old + $nr_old`
      tnr_new=`expr $tnr_new + $nr_new`

      if [ ! -z "$sdirs" ]
      then
        for sdir in $sdirs
        do
          lngth=`echo $sdir | wc -c`
          lngth=`expr $lngth - 2`
          sdir=`echo $sdir | cut -c1-$lngth`
          nr_old=0
          nr_new=0

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

          cd $main_dir/${src_dir}/regtests/$dir/$ndir/$sdir
          files=`ls -p | grep -v '/$'`
          cd $main_dir/regtests/$dir/$ndir/$sdir

          if [ -z "$files" ]
          then
            echo -e '  No files found'
          else
            for file in $files
            do
              if [ -f $file ]
              then
                nr_old=`expr $nr_old + 1`
                echo -e "      Old link $file ..." >> ${main_dir}/tmp/reg_setup.out
              else
                nr_new=`expr $nr_new + 1`
                echo -e "      New link $file ..." >> ${main_dir}/tmp/reg_setup.out
              fi
              cp ../../../../${src_dir}/regtests/$dir/$ndir/$sdir/$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=`expr $tnr_old + $nr_old`
          tnr_new=`expr $tnr_new + $nr_new`

        done

      fi

    done

  done

  cd $main_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

# --------------------------------------------------------------------------- #
# 7.2 Set-up / update smc_docs directory                                      #
# --------------------------------------------------------------------------- #

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

  cd $main_dir/${src_dir}/smc_docs

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


  tmp_files=`ls`
  all_files=
  tmp_dirs=
  for file in $tmp_files
  do
    if [ -d $file ]
    then
      tmp_dirs="$tmp_dirs $file"
    else
      all_files="$all_files $file"
    fi
  done
  cd $main_dir/smc_docs 
  echo -e ' '

  nr_old=0
  nr_new=0

  files=$all_files
  if [ -z "$files" ]
  then
    echo -e '  No files found'
  else
    for file in $files
    do
      if [ -f $file ]
      then
        nr_old=`expr $nr_old + 1`
        echo -e "   Old link $file ..." >> ${main_dir}/tmp/smc_setup.out
      else
        nr_new=`expr $nr_new + 1`
        echo -e "   New link $file ..." >> ${main_dir}/tmp/smc_setup.out
      fi
      cp -f ../${src_dir}/smc_docs/$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=`expr $tnr_old + $nr_old`
  tnr_new=`expr $tnr_new + $nr_new`



  for dir in $tmp_dirs
  do

    nr_old=0
    nr_new=0

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

    cd $main_dir/${src_dir}/smc_docs/$dir
    files=`ls -p | grep -v '/$'`
    ndirs=`ls -p | grep '/$'`
    cd $main_dir/smc_docs/$dir

    if [ -z "$files" ]
    then
      echo -e '  No files found'
    else
      for file in $files
      do
        if [ -f $file ]
        then
          nr_old=`expr $nr_old + 1`
          echo -e "      Old link $file ..." >> ${main_dir}/tmp/smc_setup.out
        else
          nr_new=`expr $nr_new + 1`
          echo -e "      New link $file ..." >> ${main_dir}/tmp/smc_setup.out
        fi
        cp -f ../../${src_dir}/smc_docs/$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=`expr $tnr_old + $nr_old`
    tnr_new=`expr $tnr_new + $nr_new`
 

    for ndir in $ndirs
    do
      lngth=`echo $ndir | wc -c`
      lngth=`expr $lngth - 2`
      ndir=`echo $ndir | cut -c1-$lngth`
      nr_old=0
      nr_new=0

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

      cd $main_dir/${src_dir}/smc_docs/$dir/$ndir
      files=`ls -p | grep -v '/$'`
      sdirs=`ls -p | grep '/$'`
      cd $main_dir/smc_docs/$dir/$ndir

      if [ -z "$files" ]
      then
        echo -e '  No files found'
      else
        for file in $files
        do
          if [ -f $file ]
          then
            nr_old=`expr $nr_old + 1`
            echo -e "      Old link $file ..." >> ${main_dir}/tmp/smc_setup.out
          else
            nr_new=`expr $nr_new + 1`
            echo -e "      New link $file ..." >> ${main_dir}/tmp/smc_setup.out
          fi
          cp -f ../../../${src_dir}/smc_docs/$dir/$ndir/$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=`expr $tnr_old + $nr_old`
      tnr_new=`expr $tnr_new + $nr_new`

      if [ ! -z "$sdirs" ]
      then
        for sdir in $sdirs
        do
          lngth=`echo $sdir | wc -c`
          lngth=`expr $lngth - 2`
          sdir=`echo $sdir | cut -c1-$lngth`
          nr_old=0
          nr_new=0

          if [ ! -d $main_dir/smc_docs/$dir/$ndir/$sdir ]
          then
            mkdir $main_dir/smc_docs/$dir/$ndir/$sdir
            echo -e "\n Making directory $dir/$ndir/$sdir..."
          else
            echo -e "\n Checking directory $dir/$ndir/$sdir ..."
          fi

          cd $main_dir/${src_dir}/smc_docs/$dir/$ndir/$sdir
          files=`ls -p | grep -v '/$'`
          cd $main_dir/smc_docs/$dir/$ndir/$sdir

          if [ -z "$files" ]
          then
            echo -e '  No files found'
          else
            for file in $files
            do
              if [ -f $file ]
              then
                nr_old=`expr $nr_old + 1`
                echo -e "      Old link $file ..." >> ${main_dir}/tmp/smc_setup.out
              else
                nr_new=`expr $nr_new + 1`
                echo -e "      New link $file ..." >> ${main_dir}/tmp/smc_setup.out
              fi
              cp ../../../../${src_dir}/smc_docs/$dir/$ndir/$sdir/$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=`expr $tnr_old + $nr_old`
          tnr_new=`expr $tnr_new + $nr_new`

        done

      fi

    done

  done

  cd $main_dir/smc_docs

  echo -e '\n\n End of smc_docs 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/smc_setup.out "
  sleep 2

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

  cd $main_dir/work
  echo -e ' '

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

# 8.a comp link and switch

  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 ../auxx/$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

# 8.d hook in present script

  cd $main_dir
  if [ ! -f install_ww3_tar ]
  then
    echo -e " Install script not in main directory, create link."
    ln -sf ./bin/install_ww3_tar .
  else
    lch=`ls -l install_ww3_tar | cut -c1-1`
    if [ "$lch" != 'l' ]
    then
      echo -e " Install script not a link ..."
      difft=`diff install_ww3_tar ./${src_dir}/model/bin/.`
      if [ -z "$difft" ]
      then
        echo -e " Install script identical to tar version, replace by link."
        ln -sf ./bin/install_ww3_tar .
      else
        echo -e " Previous install script found, different from tar version:"
        echo -e "  moved to extension .old. Creating link to tar version."
        mv install_ww3_tar install_ww3_tar.old
        ln -sf ./bin/install_ww3_tar .
      fi
    else
      echo -e " Install script link checked: all OK."
    fi 
  fi
  sleep 2

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

  rm -rf ${main_dir}/${src_dir}

  mkdir -p ${main_dir}/arc
  mv ${main_dir}/wwatch3.*.tar* ${main_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 ------------------------------------------------------------- #
