#!/bin/sh
#C#
#
# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj 1993, 1995, 1996, 1997, 1999, 2000, 2001, 2002,
# 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013.
# All rights reserved.
#
#C#

# @file
#
# Make an IRIS Release Tape
#
# $URL: https://subversion.vaisala.com/svn/repos/sigmet/wr_iris_rda/branches/release8.13.3/base/install_tools/make_iris_tape $
#
# $Id: make_iris_tape 29329 2013-04-23 18:23:13Z jho $
#

TMP=/tmp/$$_tmp
TMPX=/tmp/$$_tmpx
TMPY=/tmp/$$_tmpy

trap "rm -f ${TMP} ${TMPX} ${TMPY} /tmp/needsetown; exit" INT EXIT

MACHINE=`uname -s`

# Save original directory
ORIGINAL_DIR=`pwd`
SOURCE_ROOT=${ORIGINAL_DIR%/*/*}
# ==============================
# Check IRIS_ROOT environment variable and other directories
#
if [ ! -r ${IRIS_ROOT}/install/make_iris_tape ] ; then
  echo "The 'IRIS_ROOT' env variable is not set properly" 1>&2 ; exit 1
fi

# ==============================
# Process argument list
#
export TAPE="" # Exported to be visible to tape utilities
DEBUG=""       # Set to 'echo' for debugging

RECLIST="" ; VERBOSE="" ; BIGOBJ="" ; RDARELEASE="" ; IRISRELEASE=""
OPTIONALSOURCEDIRS=""

BASICSOURCEDIRS=""
for ITEM in \
    base/antenna_lib \
    base/antennax/po \
    base/ant_utils \
    base/ascope \
    base/audio_lib \
    base/audio/po \
    base/bitex/po \
    base/bxutils_lib \
    base/config_lib \
    base/dpolapp_lib/po \
    base/dsp_lib \
    base/dspx \
    base/examples \
    base/exthdr_lib \
    base/fileformats_lib \
    base/himath_lib \
    base/icons \
    base/include \
    base/install_tools \
    base/irisscan_lib \
    base/keys \
    base/linux_configs \
    base/maps_lib \
    base/misc \
    base/misc_lib \
    base/nexrad \
    base/rscan_lib \
    base/rtdisp/po \
    base/rtq_lib \
    base/setup \
    base/sounds \
    base/suncal \
    base/tvsubs_lib \
    base/user_lib \
    base/uxsig_lib \
    base/vtv_lib \
    base/xsig_lib \
    base/zauto7/po \
    config_template/init \
    ; do
  if [ -r ${SOURCE_ROOT}/${ITEM} ] ; then
    BASICSOURCEDIRS="${BASICSOURCEDIRS} ${ITEM}"
  fi
done

BASICSOURCEFILES=""
for ITEM in \
    config.mk \
    config_template/init/DiffHelper \
    config_template/init/font0.bdf \
    config_template/init/font1.bdf \
    config_template/init/font2.bdf \
    config_template/init/font3.bdf \
    config_template/init/font4.bdf \
    config_template/init/font5.bdf \
    config_template/init/font6.bdf \
    config_template/init/rvp7-v21-u62.hex \
    config_template/init/rvp7-v24-u62.hex \
    config_template/Makefile \
    base/ant_utils/qant \
    base/fileformats_lib/unit_check \
    base/fileformats_lib/unit_test_reference.comp.tiff \
    base/fileformats_lib/unit_test_reference.ncomp.tiff \
    base/install_tools/instiris \
    base/install_tools/sigconfig \
    base/keys/keys.lst \
    base/linux_configs/desktop/bash_profile \
    base/linux_configs/desktop/mwmrc \
    base/linux_configs/desktop/Xdefaults \
    base/linux_configs/desktop/xinitrc \
    base/linux_configs/etc/profile.d/sigmet.csh \
    base/linux_configs/etc/profile.d/sigmet.sh \
    base/Makefile \
    base/misc_lib/structs.readme \
    Makefile \
    mkinstalldirs \
    ; do
  if [ -r ${SOURCE_ROOT}/${ITEM} ] ; then
    BASICSOURCEFILES="${BASICSOURCEFILES} ${ITEM}"
  fi
done

IRISSOURCEDIRS=""
for ITEM in \
    iris/adids_pipe \
    iris/archive/po \
    iris/archive2_pipe \
    iris/asterix_pipe \
    iris/bufr_pipe \
    iris/convert_lib \
    iris/custom \
    iris/dmrl \
    iris/dwr2_pipe \
    iris/ewis_pipe \
    iris/examiners \
    iris/examples \
    iris/grib1_pipe \
    iris/hdf5_lib \
    iris/hdf5_pipe \
    iris/include \
    iris/ingest/po \
    iris/ingfio/po \
    iris/input/po \
    iris/link_lib \
    iris/mcidas_pipe \
    iris/misc \
    iris/network/po \
    iris/ngpipes \
    iris/nordrad/po \
    iris/nordrad1_lib \
    iris/nordrad2 \
    iris/nordrad2_lib \
    iris/out_lib \
    iris/output \
    iris/pipes_in \
    iris/pipes_out \
    iris/pipesup_lib \
    iris/product/po \
    iris/rainbow_pipe \
    iris/prodsup_lib \
    iris/product/po \
    iris/reingest/po \
    iris/ribbon_lib/po \
    iris/ribbuild/po \
    iris/share_lib \
    iris/siris/po \
    iris/sserver/po \
    iris/sws_lib \
    iris/tdwr \
    iris/ualf_pipe \
    iris/uf_pipe \
    iris/watchdog/po \
    iris/window \
    iris/wrdata_lib \
    iris/xuif/po \
    ; do
  if [ -r ${SOURCE_ROOT}/${ITEM} ] ; then
    IRISSOURCEDIRS="${IRISSOURCEDIRS} ${ITEM}"
  fi
done

IRISSOURCEFILES=""
for ITEM in \
    iris/examples/run_servlet_tests \
    iris/Makefile \
    iris/pipes_in/AsciiToSetup \
    iris/pipes_in/post_8.12.5_wrap \
    iris/pipes_in/pre_8.12.6_wrap \
    iris/pipes_out/post_8.12.7_wrap \
    iris/pipes_out/pre_8.12.8_wrap \
    iris/pipes_out/sig_ftp \
    iris/pipes_out/sig_rcp \
    iris/tdwr/tdwr.start \
    iris/ualf_pipe/newzeland.ualf \
    iris/ualf_pipe/sample_flash.ualf \
    iris/ualf_pipe/sample_stroke.ualf \
    iris/ualf_pipe/sample.valf \
    ; do
  if [ -r ${SOURCE_ROOT}/${ITEM} ] ; then
    IRISSOURCEFILES="${IRISSOURCEFILES} ${ITEM}"
  fi
done

RDASOURCEDIRS=""
for ITEM in \
    rda/dsp \
    rda/include \
    rda/kernelmod \
    rda/netcards_lib \
    rda/pcicards \
    rda/rcp8/core \
    rda/rcp8/open \
    rda/rcp8/site \
    rda/rdasubs_lib \
    rda/rdautils \
    rda/softplane \
    rda/tsapi_lib \
    rda/ts/archfmt \
    rda/ts/export \
    rda/ts/switch \
    rda/ts/view \
    ; do
  if [ -r ${SOURCE_ROOT}/${ITEM} ] ; then
    RDASOURCEDIRS="${RDASOURCEDIRS} ${ITEM}"
  fi
done

RDASOURCEFILES=""
for ITEM in \
    rda/Makefile \
    rda/netcards_lib/sizes_master \
    rda/rcp8/Makefile \
    rda/rvp8main/Makefile \
    rda/rvp8proc/Makefile \
    rda/rvp9main/Makefile \
    rda/rvp9proc/Makefile \
    rda/rvpxproc/Makefile \
    rda/ts/Makefile \
    ; do
  if [ -r ${SOURCE_ROOT}/${ITEM} ] ; then
    RDASOURCEFILES="${RDASOURCEFILES} ${ITEM}"
  fi
done

while [ ! "$1" = "" ] ; do
  OPT="$1" ; shift

  if [ "${OPT}" = "-help" ] ; then
    echo "Command Options"
    echo "          -debug : Print commands, but do not use the tape"
    echo "            -rda : Make RDA file set, else IRIS"
    echo "              -v : Verbose"
    echo "    -tape <name> : Use specified tape drive"
    echo "  -stdout <list> : Stream a list of records to standard output"
    echo "     -source <X> : Add file or dir X to source list"
    echo "         -bigobj : Include all machine generated exe/obj files"
    exit 0

  elif [ "${OPT}" = "-v" ] ; then
    VERBOSE="v"

  elif [  "${OPT}" = "-debug" ] ; then
    DEBUG="echo" ; VERBOSE="v"

  elif [  "${OPT}" = "-tape" ] ; then
    if [ "${RECLIST}" != "" ] ; then
      echo "Can not specify both -tape and -stdout" 1>&2 ; exit 1
    fi
    if [ "$1" = "" ] ; then
      echo "Missing tape drive name" 1>&2 ; exit 1
    else
      TAPE="$1" ; shift
    fi

  elif [  "${OPT}" = "-stdout" ] ; then
    if [ "${TAPE}" != "" ] ; then
      echo "Can not specify both -tape and -stdout" 1>&2 ; exit 1
    fi
    if [ "$1" = "" ] ; then
      echo "Missing record list for -stdout" 1>&2 ; exit 1
    else
      RECLIST="$1" ; shift
    fi

  elif [  "${OPT}" = "-source" ] ; then
    if [ "$1" = "" ] ; then
      echo "Missing source file specification" 1>&2 ; exit 1
    fi
    if [ "${1##iris*}"    != "" -a \
         "${1##base*}"    != "" -a \
         "${1##rda*}"     != "" ] ; then
      echo "Source must begin w/ 'base', 'iris', or 'rda'" 1>&2 ; exit 1
    fi
    OPTIONALSOURCEDIRS="${OPTIONALSOURCEDIRS} $1" ; shift

  elif [  "${OPT}" = "-bigobj" ] ; then
    BIGOBJ="true"

  elif [  "${OPT}" = "-iris" ] ; then
    IRISRELEASE="true"

  elif [  "${OPT}" = "-rda" ] ; then
    RDARELEASE="true"

  else
    echo "Unknown option: '${OPT}'" 1>&2
    echo "  (Use -help for help)"   1>&2
    exit 1
  fi

done

if [ "${IRISRELEASE}" != "true" ] ; then
  if [ "${RDARELEASE}" != "true" ] ; then
    echo "Must specify either -rda or -iris" 1>&2
    exit 1
  fi
fi

# ========================================
# Machine specific inits
#
if [ "${RECLIST}" = "" ] ; then

  # If the record list is empty, then we are writing to tape.  Setup
  # the default tape drive if non was specified already, and appropriate
  # 'tar' command options.
  #
  if [ "${MACHINE}" = 'HP-UX' ] ; then
    if [ "${TAPE}" = "" ] ; then TAPE=/dev/rmt/0mn ; fi
    TAR="tar cbf${VERBOSE} 20 ${TAPE}"
  elif [ "${MACHINE}" = 'Linux' ] ; then
    if [ "${TAPE}" = "" ] ; then TAPE=/dev/st0 ; fi
    TAR="tar cbf${VERBOSE} 20 ${TAPE}"
  else
    echo "Unknown Machine: '${MACHINE}'" 1>&2 ; exit 1
  fi

  RECLIST="1234789ABCDEF" ; MESSAGES="true"
  ${DEBUG} mt rewind

else
  # If a record list was specified, then setup for writing to the standard
  # output.
  #
  MESSAGES="false"

  if [ "${MACHINE}" = 'HP-UX' ] ; then
    TAR="tar cbf 20 -"
  elif [ "${MACHINE}" = 'Linux' ] ; then
    TAR="tar cbf 20 -"
  else
    echo "Unknown Machine: '${MACHINE}'" 1>&2 ; exit 1
  fi
fi

# ========================================
# Check for inconsistent release options.
#
if [ "${RDARELEASE}" = "true" ] ; then
  if [ ! -r ${IRIS_ROOT}/bin/rvp9 ] ; then
    echo "Can not make an RDA release from this machine" 1>&2
    echo "Because there is no /usr/sigmet/bin/rvp9 file." 1>&2
    exit 1
  fi
else
  if [ ! -r ${IRIS_ROOT}/bin/siris ] ; then
    echo "Can not make an IRIS release from this machine" 1>&2
    echo "Because there is not /usr/sigmet/bin/siris." 1>&2 
    exit 1
  fi
fi

# ========================================
# First record is just the installation files
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}"  = "v"    ] ; then echo "" ; fi
  echo "Writing installation files..."
fi

if [ "${RECLIST##*1*}" = "" ] ; then
  cd ${IRIS_ROOT}
  ${DEBUG} ${TAR} install
  RECLIST="${RECLIST#1}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Executables and configuration files that are required both for
# initial installations and for upgrades.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing IRIS executables and app-defaults..."
fi

if [ "${RECLIST##*2*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}
  find bin                   "(" -type f -o -type l ")" -print > ${TMP}
  ${DEBUG} ${TAR} `sort -u ${TMP}`
  RECLIST="${RECLIST#2}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Files that are required for initial installation only.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing user and site configurations..."
fi

if [ "${RECLIST##*3*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}
  find config      -type f -print | egrep -v '^config/menu/' > ${TMP}
  find config/menu -type f -name "DEFAULT.*" -print         >> ${TMP}
  find config/menu -type f -name "Default.*" -print         >> ${TMP}

  # Note that this copies the entire tree below ${IRIS_ROOT}/config,
  # including unwanted files.  This should be improved in the next version.

  # Add some directories that may be empty on the machine making the
  # release and therefore might not exist after installation.
  #
  for DIR in backups images init listings menu overlay pipes ; do
    if [ -d "config/${DIR}/" ] ; then 
      egrep "^config/${DIR}" ${TMP} > /dev/null
      EMPTY=$?
      if [ "1" = "${EMPTY}" ] ; then
        echo "config/${DIR}/" >> ${TMP} ; fi
    fi
  done

  ${DEBUG} ${TAR} `sort -u ${TMP}`
  RECLIST="${RECLIST#3}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Manuals.  Extract all '.pdf' files from the
# various subdirectories of the manuals tree.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing manuals..."
fi

if [ "${RECLIST##*4*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}

  if [ "${RDARELEASE}" = "true" ] ; then
      find manuals/extraspdf \
	  manuals/IRIS_and_RDA_Utilities_Manual.pdf \
          manuals/Software_Installation_Manual.pdf \
          manuals/RVP8_Users_Manual.pdf \
          manuals/RVP900_Users_Manual.pdf \
          manuals/RCP8_Users_Manual.pdf \
          manuals/release_notes \
          manuals/IRIS_and_RDA_Dual_Polarization_Users_Manual.pdf \
          -type f -name '*.pdf' >> ${TMP}
  else
      find manuals/extraspdf \
	  manuals/IRIS_Product_and_Display_Manuals.pdf \
          manuals/IRIS_Programmers_Manual.pdf \
          manuals/IRIS_Radar_Manual.pdf \
          manuals/IRIS_and_RDA_Utilities_Manual.pdf \
          manuals/Software_Installation_Manual.pdf \
          manuals/RVP8_Users_Manual.pdf \
          manuals/RVP900_Users_Manual.pdf \
          manuals/RCP8_Users_Manual.pdf \
          manuals/release_notes \
          manuals/IRIS_and_RDA_Dual_Polarization_Users_Manual.pdf \
          -type f -name '*.pdf' >> ${TMP}
  fi

  ${DEBUG} ${TAR} `sort -u ${TMP}`

  RECLIST="${RECLIST#4}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# The headers, source and object areas consists of three sections.
# The headers and object are always supplied in their entirety; the source
# can be filled in with some optional source this particular
# customer has purchased.
#
#          Headers               Source          Objects
#          -----                 ------          -----
#      /usr/sigmet/include        devel tree     /usr/sigmet/lib
#                            (removing .o, etc)  
#                                                
#

# ------------------------------

# First Record : Everything except the contents of #2 and #3.
# However, all '.o' files are excluded to save space, the '.a' files
# are included to allow linking.  Also, all .i and .prj files are
# excluded since they are for internal development only.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing headers..."
fi

if [ "${RECLIST##*7*}" = "" ] ; then
# Make up a list of files for the first data set, and whittle it down
# by removing the stuff that will end up in the other two.
#
  cd ${IRIS_ROOT} ; rm -f ${TMP} ;

  find include -type f -print > ${TMP}

  ${DEBUG} ${TAR} `sort -u < ${TMP}`
  RECLIST="${RECLIST#7}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ------------------------------
# Second Record : Source files
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing source files ${OPTIONALSOURCEDIRS} ..."
fi

if [ "${RECLIST##*8*}" = "" ] ; then

  if [ "${IRISRELEASE}" = "true" ] ; then
      SOURCEDIRS="${BASICSOURCEDIRS}  ${IRISSOURCEDIRS}  ${OPTIONALSOURCEDIRS}"
      SOURCEFILES="${BASICSOURCEFILES} ${IRISSOURCEFILES}"
  else
      SOURCEDIRS="${BASICSOURCEDIRS}  ${RDASOURCEDIRS}   ${OPTIONALSOURCEDIRS}"
      SOURCEFILES="${BASICSOURCEFILES} ${RDASOURCEFILES}"
  fi

  cd ${SOURCE_ROOT}

  rm -f ${TMPY}

  find ${SOURCEDIRS} \
      -type f -print | egrep -v "\/\.svn\/" | egrep "\.bm$|\.bz2$|\.c$|\.C$|\.conf$|\.cpp$|\.DAT$|\/DEFAULT\.|\/Default\.|\.h$|\.hpp$|\.inc$|\/LINGUAS$|Makefile$|\Makevars$|\.mk$|\.pdf$|\.pm$|\.po$|\.pot$|\/POTFILES\.in$|\.rc$|\.rf$|\.sin$|\*.sh$|\.txt$|\.uil$|\.wav$|\.xbm$" > ${TMPY}

  find ${SOURCEFILES} \
      -type f -print >> ${TMPY}

  ${DEBUG} ${TAR} `sort -u < ${TMPY}`
  RECLIST="${RECLIST#8}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ------------------------------
# Third Record : Objects (actually just libraries)
#
cd ${IRIS_ROOT} ;

if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing libraries..."
fi

if [ "${RECLIST##*9*}" = "" ] ; then
  if [ "${BIGOBJ}" != "true" ] ; then rm -f ${TMPX} ; fi
  find lib -type f -print >> ${TMPX}

  ${DEBUG} ${TAR} `sort -u < ${TMPX}`
  RECLIST="${RECLIST#9}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# Patch files and configuration templates that are required both for
# initial installations and for upgrades.
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Writing patches and templates..."
fi

if [ "${RECLIST##*A*}" = "" ] ; then
  cd ${IRIS_ROOT} ; rm -f ${TMP}

  find config_template       -type f -print        > ${TMP}
  find config_template/listings -type d -print    >> ${TMP}
  find dt                    -type f -print       >> ${TMP}

  ${DEBUG} ${TAR} `sort -u ${TMP}`
  RECLIST="${RECLIST#A}" ; if [ "${RECLIST}" = "" ] ; then exit ; fi
fi

# ========================================
# All done
#
if [ "${MESSAGES}" = "true" ] ; then
  if [ "${VERBOSE}" = "v" ] ; then echo "" ; fi
  echo "Rewinding and unloading..."
fi
if [ "${TAPE}" != "" ] ; then
  ${DEBUG} mt offl
fi
