#!/bin/ksh
# *******************************************
# *                                         *
# * Prepare files for an IRIS Release CDROM *
# *                                         *
# *******************************************
#
#                COPYRIGHT (c) 1996, 2000, 2004 BY
#         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
# 
# THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
# ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
# INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
# COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
# OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
# TRANSFERED.

# ==============================
# Check IRIS_ROOT environment variable
#
if [ ! -r ${IRIS_ROOT}/install/make_iris_cdrom ] ; then
  echo "The 'IRIS_ROOT' env variable is not set properly" 1>&2 ; exit 1
fi

IRIS_REL="${IRIS_ROOT}/release/"
IRIS_INS="${IRIS_ROOT}/install/"
IRIS_INC="${IRIS_ROOT}/include/"

MACHINE=`uname -s`

# ==============================
# Process argument list
#
DEBUG="" ; VERBOSE="" ; PASSARGS="" ; RDARELEASE="" ; NOMANUALS=""

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

  if [ "${OPT}" = "-help" ] ; then
    echo "Command Options"
    echo "          -debug : Print commands, but do not make files"
    echo "            -rda : Make RDA file set, else IRIS"
    echo "              -v : Verbose progress messages"
    echo "     -source <X> : Add file or dir X to source list"
    echo "         -bigobj : Include all machine generated exe/obj files"
    echo "      -nomanuals : Do not make the 'man.tgz' manuals component"
    exit 0

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

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

  elif [  "${OPT}" = "-nomanuals" ] ; then
    NOMANUALS="true"

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

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

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

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

done

# ========================================
# Call make_iris_tape with option to send file to stdout instead of to
# tape.  Then redirect the stdout to a release file and compress.  Do
# for each record.
#

if [ "${DEBUG}" = "true" ] ; then
  echo rm -rf ${IRIS_REL}
  echo mkdir ${IRIS_REL}
  echo
else
  rm -rf ${IRIS_REL}
  mkdir ${IRIS_REL}
fi

echo "Preparing install files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 1 ${VERBOSE} ${PASSARGS} -debug
else
  ${IRIS_INS}make_iris_tape -stdout 1 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}install
  gzip -S .tgz --best ${IRIS_REL}/install
fi

echo "Preparing application files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 2 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 2 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}app
  gzip -S .tgz --best ${IRIS_REL}app
fi

echo "Preparing configuration files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 3 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 3 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}config
  gzip -S .tgz --best ${IRIS_REL}config
fi

if [ "${NOMANUALS}" = "true" ] ; then
  echo "Skipping manual files..."
else
  echo "Preparing manual files..."
  if [ "${DEBUG}" = "true" ] ; then
    ${IRIS_INS}make_iris_tape -stdout 4 ${VERBOSE} ${PASSARGS} -debug
    echo
  else
    ${IRIS_INS}make_iris_tape -stdout 4 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}man
    gzip -S .tgz --best ${IRIS_REL}man
  fi
fi

echo "Preparing emacs files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 5 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 5 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}emacs
  gzip -S .tgz --best ${IRIS_REL}emacs
fi

echo "Preparing header files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 6 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 6 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}headers
  gzip -S .tgz --best ${IRIS_REL}headers
fi

echo "Preparing source files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 7 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 7 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}source
  gzip -S .tgz --best ${IRIS_REL}source
fi

echo "Preparing object files..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 8 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 8 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}objects
  gzip -S .tgz --best ${IRIS_REL}objects
fi

echo "Preparing patches and templates..."
if [ "${DEBUG}" = "true" ] ; then
  ${IRIS_INS}make_iris_tape -stdout 9 ${VERBOSE} ${PASSARGS} -debug
  echo
else
  ${IRIS_INS}make_iris_tape -stdout 9 ${VERBOSE} ${PASSARGS} > ${IRIS_REL}tplates
  gzip -S .tgz --best ${IRIS_REL}tplates
fi

if [ "${MACHINE}" = 'Linux' -a "${RDARELEASE}" != "true" -a \
     -d "${IRIS_ROOT}/web" ] ; then
  echo "Preparing Webserver..."
  if [ "${DEBUG}" = "true" ] ; then
    ${IRIS_INS}make_iris_tape -stdout A ${VERBOSE} ${PASSARGS} -debug
    echo
  else
    ${IRIS_INS}make_iris_tape -stdout A ${VERBOSE} ${PASSARGS} > ${IRIS_REL}web
    gzip -S .tgz --best ${IRIS_REL}web
  fi
else
  echo "Skipping Webserver..."
fi

echo "Copying install programs and version file..."
if [ "${DEBUG}" = "true" ] ; then
  echo cp ${IRIS_INS}install      ${IRIS_REL}
  echo cp ${IRIS_INS}install.rf   ${IRIS_REL}
  echo cp ${IRIS_INS}instiris     ${IRIS_REL}
  echo cp ${IRIS_INC}sigversion.h ${IRIS_REL}version
  echo
else
  cp ${IRIS_INS}install      ${IRIS_REL}
  cp ${IRIS_INS}install.rf   ${IRIS_REL}
  cp ${IRIS_INS}instiris     ${IRIS_REL}
  cp ${IRIS_INC}sigversion.h ${IRIS_REL}version
fi
