#!/bin/bash
# --------------------------------------------------------------------------- #
# 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  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo ' '
  echo '                *****************************'
  echo '              ***   WAVEWATCH III archive   ***'
  echo '                *****************************'
  echo ' '


# 1.b Get data from setup file - - - - - - - - - - - - - - - - - - - - - - - - 

  source $(dirname $0)/w3_setenv
  main_dir=$WWATCH3_DIR
  temp_dir=$WWATCH3_TMP
  source=$WWATCH3_SOURCE
  list=$WWATCH3_LIST

  echo "Main directory    : $main_dir"
  echo "Scratch directory : $temp_dir"
  echo "Save source codes : $source"
  echo "Save listings     : $list"


  ww3_dir="`cd $main_dir/.. 1>/dev/null 2>&1 && pwd`"

  cd $ww3_dir

  echo -e "\n Archiving in $ww3_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 ${ww3_dir}/arc
  fi

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

  echo -e "\n"
  is="1 2 3 4 5"
  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 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_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="$ww3_dir/arc/guide.$ID.pdf"
  file_mod="$ww3_dir/arc/wwatch3.$ID.model.tar"
  file_man="$ww3_dir/arc/manual.$ID.pdf"
  file_reg="$ww3_dir/arc/wwatch3.$ID.regtests.tar"
  file_smc="$ww3_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_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_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_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 generate data tar files  - - - - - - - - - - - - - - - - - - - - - - - -

  mkdir -p ${ww3_dir}/arc

  if test "$make_gde" = 'y'
  then
    echo -e "\n Compiling guide.${ID}.pdf and copying to archive location..."
    cd ${ww3_dir}/guide
    make -B > make_guide.out
    make -B > make_guide.out
    cp -f guide.pdf ${ww3_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 ${ww3_dir}/manual
    make -B > make_man.out
    make -B > make_man.out
    cp -f manual.pdf ${ww3_dir}/arc/manual.${ID}.pdf
    cp -f README_manual_reference ${ww3_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 $ww3_dir
    tar --exclude=.git --exclude=.gitignore --exclude=wwatch3.env -cf ${ww3_dir}/arc/wwatch3.$ID.model.tar model
  fi
  if test "$make_reg" = 'y'
  then
    echo -e "\n Creating wwatch3.$ID.regtests.tar and copying to archive location..."
    cd $ww3_dir
    tar --exclude=.git --exclude=.gitignore -cf ${ww3_dir}/arc/wwatch3.$ID.regtests.tar regtests
  fi
  if test "$make_smc" = 'y'
  then
    echo -e "\n Creating wwatch3.$ID.smc_docs.tar and copying to archive location..."
    cd $ww3_dir
    tar --exclude=.git --exclude=.gitignore -cf ${ww3_dir}/arc/wwatch3.$ID.smc_docs.tar smc_docs
  fi


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

# Create master gziped tar file
  echo -e "\n Creating wwatch3.${ID}.tar.gz file with stand-alone package."
  cd ${ww3_dir}/arc
  if test "$make_gde" = 'y' || test "$make_man" = 'y'
  then
    tar zcvf wwatch3.${ID}.tar.gz install* *.pdf wwatch3.$ID.*.tar README*
  else
    tar zcvf wwatch3.${ID}.tar.gz install* wwatch3.$ID.*.tar 
  fi

# 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'

