#!/bin/sh
#C#
#
# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj 2013.
# All rights reserved.
#
#C#

# @file
#
# Script to copy and assemble all files necessary to build a combined
# IRIS/RDA DVD.
#
# $URL$
#
# $Id$
#


######################################################################
# Process the argument list
######################################################################
HELP="" ; VERSION=""

while [ "$1" != "" ] ; do
  OPT="$1" ; shift
  case ${OPT} in

  -help | -? )
    HELP="true" ;;

  -version )
    if [ "${1}" = "" ] ; then
      HELP="true"
    else
      VERSION="${1}" ; shift
    fi ;;

  * )
    echo "Unknown option: '${OPT}'" 1>&2 ; HELP="true" ;;
 esac
done

if [ "${VERSION}" = "" ] ; then
  HELP="true"
fi

######################################################################
# Sanity check certain arguments, and print HELP messages if these
# were requested either directly or indirectly.
######################################################################
if [ "${HELP}" = "true" ] ; then
  echo "Command Line Options:"
  echo "  -version <VER> : Specify IRIS/RDA version to use in this build"
  exit 0
fi

######################################################################
# The root of the directory from which the ISO will be created
######################################################################
ROOTDIR="/usr/images/irisrda_image"

######################################################################
## iso files
######################################################################
ISOIMAGEDIR="${ROOTDIR}/isoimage"

######################################################################
#PRODUCT EXAMPLES 
######################################################################
PRODUCTEXAMPLESDIR="${ISOIMAGEDIR}/other"

######################################################################
# The manuals
######################################################################
MANUALSDIR="${ISOIMAGEDIR}/manuals"

######################################################################
# Name of create iso image
######################################################################
ISOFILE="irisrda_image.iso"
ISOIMAGE="${ROOTDIR}/${ISOFILE}"

######################################################################
# Various lists of files and directories
######################################################################
SIGBRUFILES="
  /usr/sigmet/bin/sigbrush
  /usr/sigmet/dt/icons/hour32.bm
  /usr/sigmet/dt/icons/hour32m.bm
  /usr/sigmet/bin/app-defaults/sigbru"

################################################################
# Delete the old release tree and create a new one.  This gives us 
# a fresh start.
################################################################
sudo rm -rf ${ROOTDIR}
sudo mkdir -m 775 -p ${ROOTDIR}

if [ ! -w ${ROOTDIR} ] ; then
    echo "ERROR: Could not create ${ROOTDIR}"
    exit 1
fi

sudo mkdir -m 775 ${ISOIMAGEDIR}
sudo mkdir -m 775 ${PRODUCTEXAMPLESDIR}
sudo mkdir -m 775 ${MANUALSDIR}

################################################################
# Copy the manuals directories from den-hazy
################################################################
echo "---Copying MANUALS from den-hazy..."
cd ${MANUALSDIR}
sudo cp -r /sigmet_info/manuals/* ./
if [ "$?" != "0" ] ; then
    echo "ERROR: Copy failed"
    exit 1
fi

################################################################
# Copy product examples from den-hazy:/export/images/ProductExamples
################################################################
echo "---Copying product examples from den-hazy..."
cd ${PRODUCTEXAMPLESDIR}
sudo cp /images/ProductExamples/* .
if [ "$?" != "0" ] ; then
    echo "ERROR: Copy failed"
    exit 1
fi

##############################################################
## Copy sigconfig from this current host
##############################################################
RCS_INSTALL_LOC="/usr/sigmet/install"
cd ${ISOIMAGEDIR}
sudo cp ${RCS_INSTALL_LOC}/sigconfig .
sudo cp ${RCS_INSTALL_LOC}/iriscentos7.repo .
sudo cp ${RCS_INSTALL_LOC}/iriscentos8.repo .
sudo cp ${RCS_INSTALL_LOC}/requirements.txt .
if [ "$?" != "0" ] ; then
    echo "ERROR: Copy failed"
    exit 1
fi

####################################################################
# Copy the IRIS/RDA release files from wes-hazy 
####################################################################
REDHAT_RELEASES="CENTOS7 CENTOS8"
SIGMET_APPS="iris rda extras"

for REDHAT_RELEASE in ${REDHAT_RELEASES} ; do
  for SIGMET_APP in ${SIGMET_APPS} ; do
    LOCALDIR=${REDHAT_RELEASE}/${SIGMET_APP}

#  echo "LOCALDIR=${LOCALDIR}"  
#  echo "HOSTNAME=${HOSTNAME}"
#  echo "SIGMET_APP=${SIGMET_APP}"
#  echo "REDHAT_RELEASE=${REDHAT_RELEASE}"

    echo "---Copying release ${LOCALDIR} from ${HOSTNAME}..."
    cd ${ISOIMAGEDIR}; mkdir -m 775 -p ${LOCALDIR} ; cd ${LOCALDIR}
    if   [ "${SIGMET_APP}" = "extras"  ] ; then
     sudo cp -r /images/cdrom_extras/${LOCALDIR}/*  .
      if [ "$?" != "0" ] ; then
  	echo "ERROR: Copy failed"
	exit 1
      fi
    else
      sudo cp /images/builds/${VERSION}/${LOCALDIR}/* .
      if [ "$?" != "0" ] ; then
	echo "ERROR: Copy failed"
	exit 1
      fi
      sudo rm -f *-source.tgz

      # Reduce size of the release by sharing as many common files as we
      # can across the different platforms.  
      #
      if [ "${SIGMET_APP}" = "iris" ] ; then
        ln -s ../../other/prod.tgz prod.tgz
        ln -s ../../other/prod_raw.tgz prod_raw.tgz
      fi
    fi
  done
done

##############################################################
## Create and iso image 
##############################################################
DISK_LABEL="irisrda_${VERSION}"
cd ${ISOIMAGEDIR}
mkisofs -o ${ISOIMAGE} -relaxed-filenames  -l -R -J \
  -V ${DISK_LABEL} -v -T -d .

# Compute the md5sum:
cd ${ROOTDIR} 
md5sum ${ISOFILE} > MD5SUM

cp ${ISOIMAGE} /images/builds/${VERSION}/
if [ "$?" != "0" ] ; then
    echo "ERROR: Copy of ISO failed"
    exit 1
fi
cp MD5SUM /images/builds/${VERSION}/

