#!/bin/sh
# --------------------------------------------------------------------------- #
# arc_wwatch3_tar : Archive WAVEWATCH III in tar files which can be processed #
#                   by the installation program install_wwatch3_tar.          #
#                                                                             #
# Origination : Hendrik L. Tolman    May 2009                                 # 
# Updates :                                                                   #
# - Adapted to create tar files from svn directory structures                 #
#   (JH Alves, Aug 2013)                                                      #
#                                                                             #
#    Copyright 2009-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.                               #
#                                                                             #
# --------------------------------------------------------------------------- #
# 1. Preparations                                                             #
# --------------------------------------------------------------------------- #
# 1.a ID header  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  clear
  echo -e '\n\n\n               ======================================'
  echo -e '             ---   WAVEWATCH III archiving program  ---'
  echo -e '               ======================================'

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

  echo -e '\n\n Choose now where you configuration file wwatch3.env is located.'
  echo -e ' If you use a generic/system-wide wwatch3.env, type G. '
  echo -e ' If you use a local wwatch3.env, type L. ' 
  echo -en '\n\n Type your choice now [G|L]: '
  read sww3dir

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

    ww3_env="${HOME}/.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"
    else
      echo -e "\n\n Generic wwatch3.env file not found in"
      echo -e "  ${home_dir} "
      echo -e '\n  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 "\n\n   Directory (relative to $home_dir) : "
        read mdir
        if [ -n "$mdir" ]
        then
          main_dir="$mdir"
        fi
      done
      cd $main_dir
      main_dir=`pwd`
    fi

  elif [ "${sww3dir}" == "L" ] || [ "${sww3dir}" == "l" ]
  then
 
    echo -e "\n\n Enter the path for local wwatch3.env file."
    echo -e "  This may be the full or relative path ( ./ and ../ ): "
    echo -en "  " 
    read main_dir

    if [ -f ${main_dir}/wwatch3.env ]
    then
      echo -e '\n Local wwatch3.env found, moving ahead.'
      ww3_env="${main_dir}/wwatch3.env"
    else
      echo -e '\n\n Local file wwatch3.env not found. Exiting. \n\n'
      exit
    fi
  fi

  cd ${main_dir}
  main_dir=`pwd`

  echo -e "\n Archiving in $main_dir/arc"

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

  if test ! -d arc
  then
    mkdir ${main_dir}/arc
  fi

# --------------------------------------------------------------------------- #
# 2. Process requests                                                         #
# --------------------------------------------------------------------------- #
# 2.a Requested files

  echo -e "\n"
  is="1 2 3 4 5 6"
  for i in $is
  do
    OK="$NULL"
    until test "$OK" = 'y' || test "$OK" = 'Y' || \
          test "$OK" = 'n' || test "$OK" = 'N'
    do
      case $i in
        1) echo -en " Make guide file ? [y/n] : " ;;
        2) echo -en " Make manual file ? [y/n] : " ;;
        3) echo -en " Make model file ? [y/n] : " ;;
        4) echo -en " Make regtests file  ? [y/n] : " ;;
        5) echo -en " Make cases file  ? [y/n] : " ;;
        6) echo -en " Make smc_docs file? [y/n] : " ;;
      esac
      read OK
    done

    if test "$OK" = 'y' || test "$OK" = 'Y'
    then
      do_it='y'
    else
      do_it='n'
    fi

    case $i in
     1) make_gde=$do_it ;;
     2) make_man=$do_it ;;
     3) make_mod=$do_it ;;
     4) make_reg=$do_it ;;
     5) make_cas=$do_it ;;
     6) make_smc=$do_it ;;
    esac
  done

# 2.b File names - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  OK="$NULL"
  ID="$NULL"

  echo -e "\n\n You are required to enter an identification tag for archiving."
  echo -e "  This should be a short string (eg, v4.14, alpha.v4.14 etc)."

  until test "$OK" = 'y' || test "$OK" = 'Y'
  do
    until test "$ID"
    do
      echo -en "\n Enter identifier tag for this archive version [$ID] : " 
      read ID
    done
    echo -en "\n  Are you sure ? [y/-] : "
    read OK
  done

# 2.c Check for existence of files - - - - - - - - - - - - - - - - - - - - - -

  file_gde="$main_dir/arc/guide.$ID.pdf"
  file_mod="$main_dir/arc/wwatch3.$ID.model.tar"
  file_man="$main_dir/arc/manual.$ID.pdf"
  file_reg="$main_dir/arc/wwatch3.$ID.regtests.tar"
  file_cas="$main_dir/arc/wwatch3.$ID.cases.tar"
  file_smc="$main_dir/arc/wwatch3.$ID.smc_docs.tar"

  for i in 1 2 3 4 5
  do
    case $i in
     1) file=$file_gde ; make=$make_gde ;; 
     2) file=$file_man ; make=$make_man ;; 
     3) file=$file_mod ; make=$make_mod ;; 
     4) file=$file_reg ; make=$make_reg ;;
     5) file=$file_cas ; make=$make_cas ;;
     6) file=$file_smc ; make=$make_smc ;;
    esac
    if test -f $file && test "$make" = 'y'
    then
      echo -e "\n File $file exists."
      echo -en "  Replace ? [y/-] : "
      read OK
      if test "$OK" != 'y' && test "$OK" != 'Y'
      then
        case $i in
         1) make_gde='n' ;;
         2) make_man='n' ;;
         3) make_mod='n' ;;
         4) make_reg='n' ;;
         5) make_cas='n' ;;
         6) make_smc='n' ;;
        esac
      fi
    fi
  done

# 2.d Final check before action  - - - - - - - - - - - - - - - - - - - - - - -

  action="$NULL"
  if [ "$make_gde" = 'n' ] && [ "$make_man" = 'n' ] && \
     [ "$make_mod" = 'n' ] && [ "$make_reg" = 'n' ] && \
     [ "$make_cas" = 'n' ] && [ "$make_smc" = 'n' ] 
  then
    action='n'
  else
    action='y'
    echo -e '\n Files to be generated :'
    echo -e ' -----------------------'
    if test "$make_gde" = 'y'
    then
      echo -e "   $file_gde"
      if [ -f arc/$file_gde ]
      then
        rm -f arc/$file_gde
      fi
    fi
    if test "$make_man" = 'y'
    then
      echo -e "   $file_man"
      if [ -f arc/$file_man ]
      then
        rm -f arc/$file_man
      fi
    fi
    if test "$make_mod" = 'y'
    then
      echo -e "   $file_mod"
      if [ -f arc/$file_mod ]
      then
        rm -f arc/$file_mod
      fi
    fi
    if test "$make_reg" = 'y'
    then
      echo -e "   $file_reg"
      if [ -f arc/$file_reg ]
      then
        rm -f arc/$file_reg
      fi
    fi
    if test "$make_cas" = 'y'
    then
      echo -e "   $file_cas"
      if [ -f arc/$file_cas ]
      then
        rm -f arc/$file_cas
      fi
    fi
    if test "$make_smc" = 'y'
    then
      echo -e "   $file_smc"
      if [ -f arc/$file_smc ]
      then
        rm -f arc/$file_smc
      fi
    fi
  fi

  echo -en "\n Continue ? [y/-] : "
  read OK
  if test "$OK" != 'y' && test "$OK" != 'Y'
  then
    action='n'
  fi

  if test "$action" = 'n'
  then
    echo -e '\n\n\n                   ==============================='
    echo -e '                 ---       End of program        ---'
    echo -e '                   =============================== \n'
  fi

# 6.c genarate data tar files  - - - - - - - - - - - - - - - - - - - - - - - -

  mkdir -p ${main_dir}/arc

  if test "$make_gde" = 'y'
  then
    echo -e "\n Compiling guide.${ID}.pdf and copying to archive location..."
    cd ${main_dir}/guide
    make -B > make_guide.out
    make -B > make_guide.out
    cp -f guide.pdf ${main_dir}/arc/guide.${ID}.pdf
    make clean
  fi
  if test "$make_man" = 'y'
  then
    echo -e "\n Compiling manual.${ID}.pdf and copying to archive location..."
    cd ${main_dir}/manual
    make -B > make_man.out
    make -B > make_man.out
    cp -f manual.pdf ${main_dir}/arc/manual.${ID}.pdf
    cp -f README_manual_reference ${main_dir}/arc/README_manual_reference
    make clean
  fi
  if test "$make_mod" = 'y'
  then
    echo -e "\n Creating wwatch3.$ID.model.tar and copying to archive location..."
    cd $main_dir/svn
    tar cf ${main_dir}/arc/wwatch3.$ID.model.tar model --exclude=.svn
  fi
  if test "$make_reg" = 'y'
  then
    echo -e "\n Creating wwatch3.$ID.regtests.tar and copying to archive location..."
    cd $main_dir/svn
    tar cf ${main_dir}/arc/wwatch3.$ID.regtests.tar regtests --exclude=.svn
  fi
  if test "$make_cas" = 'y'
  then
    echo -e "\n Creating wwatch3.$ID.cases.tar and copying to archive location..."
    cd $main_dir/svn
    tar cf ${main_dir}/arc/wwatch3.$ID.cases.tar cases --exclude=.svn
  fi
  if test "$make_smc" = 'y'
  then
    echo -e "\n Creating wwatch3.$ID.smc_docs.tar and copying to archive location..."
    cd $main_dir/svn
    tar cf ${main_dir}/arc/wwatch3.$ID.smc_docs.tar smc_docs --exclude=.svn
  fi


# Copy install_ww3_tar to arc
  cp -f ${main_dir}/bin/install_ww3_tar ${main_dir}/arc

# Create master gziped tar file
  echo -e "\n Creating wwatch3.${ID}.tar.gz file with stand-alone package."
  cd ${main_dir}/arc
  tar zcvf wwatch3.${ID}.tar.gz install* *.pdf wwatch3.$ID.*.tar README*

# Cleanup
  echo -e "\n Cleaning up arc location (only package file is left)." 
  rm -f *.pdf wwatch3.$ID.*.tar install* README*

# All done (one hopes...)

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

