#!/bin/sh
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__

#set -x

umask  022

TAR=tar
BASEDIR=/tmp/sge_dist
CDCONTAINER=`/bin/pwd`/cdcontainer

MKISOFS=mkisofs
ISOFSOPTIONS="-l -J -L -N -r -v"

#---------------------------------------------------------------------------
#
ErrUsage()
{
   echo "Usage: mk_dist -vdir vdir -version version [-basedir dir] \\"
   echo "               [-sgeee] [-bin] [-common] [-doc] binarchs ..." 
   echo ""
   echo "    -dir vdir        use subdirectory (and config) vdir to create distribution"
   echo "    -vdir vdir       use subdirectory vdir to create distribution"
   echo "    -version version string to build filename of distribution"
   echo "    -basedir         set base directory for outputfile location"
   echo "                     default: $BASEDIR" 
   echo "    -tar tarcmd      tar command which supports "z" option"
   echo "    -sgeee           create Grid Engine Enterprise Edition distribution"
   echo "    -bin             create tar.gz files from binaries in bin/,"
   echo "                     utilbin/ and examples/jobsbin"
   echo "    -common          create tar.gz file of \"common\" files"
   echo "    -doc             create tar.gz file of \"doc\" files"  
   echo ""
   exit 1
}

#---------------------------------------------------------------------------
#
Execute()
{
   if [ "$verbose" = true ]; then
      echo $* 
   fi
   $*
   if [ $? != 0 ]; then
      echo 
      echo "Command failed: $*"
      echo ""
      exit 1  
   fi
}

#------------------------------------------------------------------------
#-------------------------------------------------------------------------
# MAIN section
#

makebin=false
makecommon=false
makedoc=false
makesgeee=false
target=file
VDIR=""
VERSION=""
PRODUCT=sge
SELECTED_ARCHS=""
INSTALL_SCRIPT=inst_sge

ARGC=$#
while [ $ARGC -gt 0 ]; do
   case $1 in
   -basedir)
      shift
      ARGC=`expr $ARGC - 1`
      if [ $ARGC -lt 1 ]; then
         echo ""
         echo "Error: Missing argument to -basedir switch!"
         echo ""
         ErrUsage
      fi
      BASEDIR=$1
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -bin)
      echo "Creating tar file(s) of binaries in bin/ utilbin/ examples/jobsbin"
      makebin=true
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -common)
      echo "Creating tar file with common files of distribution"
      makecommon=true
      shift 
      ARGC=`expr $ARGC - 1`
      ;;
   -doc)
      echo "Creating tar file with doc files of distribution"
      makedoc=true
      shift 
      ARGC=`expr $ARGC - 1`
      ;;
   -h|-help)
      ErrUsage
      ;;
   -sgeee)
      echo "Creating Grid Engine Enterprise Edition distribution"
      makesgee=true
      INSTALL_SCRIPT=inst_sgeee
      PRODUCT=sgeee
      shift 
      ARGC=`expr $ARGC - 1`
      ;;
   -tar)
      shift
      ARGC=`expr $ARGC - 1`
      if [ $ARGC -lt 1 ]; then
         echo ""
         echo "Error: Missing argument to -tar switch!"
         echo ""
         ErrUsage
      fi
      TAR=$1
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -dir)
      shift
      ARGC=`expr $ARGC - 1`
      if [ $ARGC -lt 1 ]; then
         echo ""
         echo "Error: Missing argument to -dir switch!"
         echo ""
         ErrUsage
      fi
      VDIR=$1
      shift
      ARGC=`expr $ARGC - 1`
      if [ -f localext/$VDIR/mk_dist.private ]; then
         . ./localext/$VDIR/mk_dist.private
      fi
      ARGC=$#
      ;;
   -vdir)
      shift
      ARGC=`expr $ARGC - 1`
      if [ $ARGC -lt 1 ]; then
         echo ""
         echo "Error: Missing argument to -vdir switch!"
         echo ""
         ErrUsage
      fi
      VDIR=$1
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -version)
      shift
      ARGC=`expr $ARGC - 1`
      if [ $ARGC -lt 1 ]; then
         echo ""
         echo "Error: Missing argument to -version switch!"
         echo ""
         ErrUsage
      fi
      VERSION=$1
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -*)
      echo ""
      echo "Error: Unknown option: $1"
      ErrUsage $0
      ;;
   *)
      ARGC=0
      ;;
   esac
done

#--- END-OF-COMMANDLINE-PARSING------------------------------------------

PVMCONTENT="pvm/README pvm/*.sh pvm/*.template pvm/src"
DOC_PACKAGE="doc/*.pdf"
COMMON_DOCS="doc/INSTALL doc/UPGRADE doc/*.asc $ADD_COMMON_DOCS"
COMMON_PACKAGE="3rd_party catman ckpt $COMMON_DOCS examples/jobs $INSTALL_SCRIPT install_qmaster install_execd man $PVMCONTENT mpi qmon util"

if [ $makesgeee = false ]; then
   COMMON_PACKAGE="$COMMON_PACKAGE inst_sgeee README.inst_sgeee"
fi

if [ "$VERSION" = "" -o "$VDIR" = "" ]; then
   ErrUsage
fi

Execute mkdir -p $BASEDIR

SELECTED_ARCHS=$*

if [ $makebin = true ]; then
   if [ "$SELECTED_ARCHS" = "" ]; then
      ErrUsage
   fi

   # do any necessary postprocessing of the "bin" package here
   if [ -f localext/$VDIR/mk_dist.private.bin ]; then
      echo executing commands from mk_dist.private.bin
      . ./localext/$VDIR/mk_dist.private.bin
   fi

   cd $VDIR
   echo creating tar.gz files for $SELECTED_ARCHS
   for i in $SELECTED_ARCHS; do
         ADIR="bin/$i utilbin/$i examples/jobsbin/$i lib/$i"
         filename=$BASEDIR/${PRODUCT}-${VERSION}-bin-${i}.tar.gz         
         echo Command: $TAR cvzf $filename $ADIR
                       $TAR cvzf $filename $ADIR
   done
   cd ..
fi

if [ $makecommon = true ]; then
   # do any necessary postprocessing of the "common" package here
   if [ -f localext/$VDIR/mk_dist.private.common ]; then
      echo executing commands from mk_dist.private.common
      . ./localext/$VDIR/mk_dist.private.common
   fi

   cd $VDIR
   filename=$BASEDIR/${PRODUCT}-${VERSION}-common.tar.gz
   echo Command: $TAR cvzf $filename $COMMON_PACKAGE
                  $TAR cvzf $filename $COMMON_PACKAGE
   cd ..
fi

if [ $makedoc = true ]; then
   # do any necessary postprocessing of the "doc" package here
   if [ -f localext/$VDIR/mk_dist.private.doc ]; then
      echo executing commands from mk_dist.private.doc
      . ./localext/$VDIR/mk_dist.private.doc
   fi

   cd $VDIR
   filename=$BASEDIR/${PRODUCT}-${VERSION}-doc.tar.gz
   echo Command: $TAR cvzf $filename $DOC_PACKAGE
                  $TAR cvzf $filename $DOC_PACKAGE
   cd ..
fi

exit 0
