#!/bin/sh -x

## A Script to collect compiled binaries and libraries on the integration machine.
## Everything is collected into tar files, to be passed on to the system test machine.
## Two input parameters are required:
##	1. The location of the root of the compilation tree
##	2. The release name
##
## A default tar is made for a Solaris SUN machine. Additional versions are copied from the
## root directory by adding a -LNX suffix for Linux and -INTL for Intel-Solaris.
#
## If GIN-LNX (for Linux) or GIN-INTL (for Intel-Solaris) directories are not found, the
## collection of the missing version is skipped.
##

if [ $# -lt 2 ]; then
	echo "Usage: $0 dir release-name"
	exit 1
fi

if [ -d $1 ]; then
	echo "\n Collecting binaries and libraries from $1 ....\n"
else
	echo "Usage: $0 dir release-name"
	exit 1
fi

RELNAME=Rel-$2
RELDIR=$1
RELDIRLNX=$1-LNX
RELDIRINTL=$1-INTL
SCRIPTSDIR="/public/scripts"
CONFDIR="/public/ins/conf/GLB"

# Copy the executables to /tmp/bin-release *******************

if [ -d /tmp/bin-release ]; then
	# Clean up old files
	rm -rf /tmp/bin-release/*
else
	# Create directory
	mkdir /tmp/bin-release
fi

cd /tmp/bin-release

# Start copying binaries

echo " Copying binaries ...."
cp $RELDIR/SDA/sdad .
cp $RELDIR/SDU/sdud .
cp $RELDIR/NPDD/Npdd .
cp $RELDIR/LSM/Lsm .
cp $RELDIR/Z2H/XmlConvertor .
cp $RELDIR/GTL/setconf .
cp $RELDIR/GTL/snmpconf .
cp $RELDIR/GTL/http .
cp $SCRIPTSDIR/isalive.sh .
cp $CONFDIR/startIsAlive.sh .
cp $CONFDIR/startTrapsFilter.sh .
cp $CONFDIR/sdad.sh .
cp $CONFDIR/sdud.sh .
cp $CONFDIR/Npdd.sh .

# Tar the content of the directory

/usr/bin/tar cvf /public/ins/Bin-SUN-$RELNAME.tar .
gzip /public/ins/Bin-SUN-$RELNAME.tar

# Repeat for Linux, if build exists

if [ -d $RELDIRLNX ]; then

  rm -rf /tmp/bin-release/*
  cp $RELDIRLNX/SDA/sdad .
  cp $RELDIRLNX/SDU/sdud .
  cp $RELDIRLNX/NPDD/Npdd .
  cp $RELDIRLNX/LSM/Lsm .
  cp $RELDIRLNX/Z2H/XmlConvertor .
  cp $RELDIRLNX/GTL/setconf .
  cp $RELDIRLNX/GTL/snmpconf .
  cp $RELDIRLNX/GTL/http .
  cp $SCRIPTSDIR/isalive.sh .
  cp $CONFDIR/startIsAlive.sh .
  cp $CONFDIR/startTrapsFilter.sh .
  cp $CONFDIR/sdad.sh .
  cp $CONFDIR/sdud.sh .
  cp $CONFDIR/Npdd.sh .


  # Tar the content of the directory

  /usr/bin/tar cvf /public/ins/Bin-LNX-$RELNAME.tar .
  gzip /public/ins/Bin-LNX-$RELNAME.tar
fi

# Repeat for Intel-Solaris, if build exists

if [ -d $RELDIRINTL ]; then

  rm -rf /tmp/bin-release/*
  cp $RELDIRINTL/SDA/sdad .
  cp $RELDIRINTL/SDU/sdud .
  cp $RELDIRINTL/NPDD/Npdd .
  cp $RELDIRINTL/LSM/Lsm .
  cp $RELDIRINTL/Z2H/XmlConvertor .
  cp $RELDIRINTL/GTL/setconf .
  cp $RELDIRINTL/GTL/snmpconf .
  cp $RELDIRINTL/GTL/http .
  cp $SCRIPTSDIR/isalive.sh .
  cp $CONFDIR/startIsAlive.sh .
  cp $CONFDIR/startTrapsFilter.sh .
  cp $CONFDIR/sdad.sh .
  cp $CONFDIR/sdud.sh .
  cp $CONFDIR/Npdd.sh .

  # Tar the content of the directory

  /usr/bin/tar cvf /public/ins/Bin-INTL-$RELNAME.tar .
  gzip /public/ins/Bin-INTL-$RELNAME.tar
fi

# Cleanup
rm -rf /tmp/bin-release/*

# Copy the libraries to /tmp/lib-release *****************************

if [ -d /tmp/lib-release ]; then
	# Clean up old files
	rm -rf /tmp/lib-release/*
else
	# Create directory
	mkdir /tmp/lib-release
fi

cd /tmp/lib-release

# Start copying libraries

echo " Copying libraries ...."

cp $RELDIR/DPT/lib*.so .
cp $RELDIR/GNS/*.so .
cp $RELDIR/GTL/lib*.so .
cp $RELDIR/GRB/lib*.so .
cp $RELDIR/RUS/lib*.so .
cp $RELDIR/PICI/lib*.so .
cp $RELDIR/SDA/lib*.so .
cp $RELDIR/ZAX/lib*.so .
cp $RELDIR/SDU/lib*.so .
cp $RELDIR/UWS/*.so .

# Create version sufix

for libr in `ls *.so`; do
   mv $libr $libr.0
   ln -s $libr.0 $libr
done

# Tar the content of the directory

/usr/bin/tar cvf /public/ins/Lib-SUN-$RELNAME.tar .
gzip /public/ins/Lib-SUN-$RELNAME.tar

# Clean up
rm -rf /tmp/lib-release/*

# Repeat for Linux, if build exists

if [ -d $RELDIRLNX ]; then

  # Start copying libraries

  echo " Copying libraries ...."

  cp $RELDIRLNX/DPT/lib*.so .
  cp $RELDIRLNX/GNS/*.so .
  cp $RELDIRLNX/GTL/lib*.so .
  cp $RELDIRLNX/GRB/lib*.so .
  cp $RELDIRLNX/RUS/lib*.so .
  cp $RELDIRLNX/PICI/lib*.so .
  cp $RELDIRLNX/SDA/lib*.so .
  cp $RELDIRLNX/ZAX/lib*.so .
  cp $RELDIRLNX/SDU/lib*.so .
  cp $RELDIRLNX/UWS/*.so .

  # Create version sufix

  for libr in `ls *.so`; do
    mv $libr $libr.0
    ln -s $libr.0 $libr
  done

  # Tar the content of the directory

  /usr/bin/tar cvf /public/ins/Lib-LNX-$RELNAME.tar .
  gzip /public/ins/Lib-LNX-$RELNAME.tar
fi

# Clean up
rm -rf /tmp/lib-release/*

# Repeat for Intel-Solaris, if build exists

if [ -d $RELDIRINTL ]; then

  # Start copying libraries

  echo " Copying libraries ...."

  cp $RELDIRINTL/DPT/lib*.so .
  cp $RELDIRINTL/GNS/*.so .
  cp $RELDIRINTL/GTL/lib*.so .
  cp $RELDIRINTL/GRB/lib*.so .
  cp $RELDIRINTL/RUS/lib*.so .
  cp $RELDIRINTL/PICI/lib*.so .
  cp $RELDIRINTL/SDA/lib*.so .
  cp $RELDIRINTL/ZAX/lib*.so .
  cp $RELDIRINTL/SDU/lib*.so .
  cp $RELDIRINTL/UWS/*.so .

  # Create version sufix

  for libr in `ls *.so`; do
    mv $libr $libr.0
    ln -s $libr.0 $libr
  done

  # Tar the content of the directory

  /usr/bin/tar cvf /public/ins/Lib-INTL-$RELNAME.tar .
  gzip /public/ins/Lib-INTL-$RELNAME.tar
fi

# Clean up
rm -rf /tmp/lib-release/*

echo "\n\n The $RELNAME release is ready for distribution"