#!/bin/sh
#
# Purpose	: A script to run a full GIN software build, Based on perforce system
# By		: Yariv Sheizaf
# Date		: 16-Jul-2001 
# Argumnets	:
#			1 - configuration name
#			2 - Build number
#

if [ "$USER" != "cmadm" ]
then
	echo ""
	echo "$0 should be run with user cmadm. Exit..."
	echo ""
	exit 1
fi


HOSTN=`hostname`
if [ "$HOSTN" != "tavola" ]
then
	echo ""
	echo "$0 should be run from host tavola. Exit..."
	echo ""
	exit 1
fi

## Define servers
SUNSRVR="tavola"
LNXSRVR="panino"
INTLSRVR="beygel"

if [ $# -lt 2 ]
then
	echo "Usage: $0 configuration-name Build-number"
	echo "Example: $0 prd_2.0 1.8.6"
	exit 1
fi


CNFNAME=$1

## Check configuration existing in P4 DB
ISCNF="n"
for i in `p4 dirs //Globecom/\* | awk -F/ '{print $NF}'`
do
	if [ "$i" = "$CNFNAME" ]
	then
		ISCNF="y"
	fi
done
if [ "$ISCNF" = "n" ]
then
	echo ""
	echo $CNFNAME" is not a valid configuration. Exit..."
	echo ""
	exit 1
fi

BUILDNUM=$2

## Check Z2H label existance
p4 files @Z2H_Build_$BUILDNUM 1>/dev/null 2>&1
if [ "$?" != "0" ]
then
	echo ""
	echo "Label Z2H_Build_"$BUILDNUM" not exist. Exit..."
	echo ""
	exit 1
fi

WRKROOTDIR=$HOME"/p4client/Globecom/"$CNFNAME"/Globecom"

# Synchornize to correct Z2H build label
echo ""
echo "Synchronize Z2H_Build_"$BUILDNUM
p4 sync //Globecom/$CNFNAME/Globecom/Z2H/...#none > /dev/null
rm -rf $WRKROOTDIR/Z2H
p4 sync //Globecom/$CNFNAME/Globecom/Z2H/...@Z2H_Build_$BUILDNUM > /dev/null

## Prevent write into P4 DB
echo "Set P4 protect to open-only mode"
p4 protect -o > /tmp/$$.p4protect
p4 protect -o | sed 's/write/open/' | p4 protect -i


## Clean working directorties
p4 sync //Globecom/$CNFNAME/Globecom/GIN/...#none > /dev/null
echo "Remove old GIN directories"
rm -rf $WRKROOTDIR/GIN
echo "Remove old GIN-LNX directories"
rm -rf $WRKROOTDIR/GIN-LNX
echo "Remove old GIN-INTL directories"
rm -rf $WRKROOTDIR/GIN-INTL

## Put source code in working directories
echo "Synchronize head revision of GIN"
p4 sync //Globecom/$CNFNAME/Globecom/GIN/... > /dev/null

## Put Z2H files in GIN working directories
echo "Copying Z2H files into GIN environment"
/public/scripts/cpz2h $CNFNAME

## Prepare Linux and SUN-Intel working directories
echo "Duplicate GIN to GIN-LNX"
cp -r $WRKROOTDIR/GIN $WRKROOTDIR/GIN-LNX
echo "Duplicate GIN to GIN-LNX"
cp -r $WRKROOTDIR/GIN $WRKROOTDIR/GIN-INTL

SUNDIR=$WRKROOTDIR"/GIN"
LNXDIR=$WRKROOTDIR"/GIN-LNX"
INTLDIR=$WRKROOTDIR"/GIN-INTL"

echo "Run BuildGin via rsh"

## Sun Solaris GIN build
rsh $SUNSRVR "cd $SUNDIR; /public/scripts/BuildGin > $$.BuildGin" &

## Sun intel GIN build
rsh $INTLSRVR  "cd $INTLDIR; /public/scripts/BuildGin > $$.BuildGinIntl" &

## Linux GIN build
rsh $LNXSRVR "cd $LNXDIR; /public/scripts/BuildGin > $$.BuildGinLnx" &

wait

## Pack tar.gz files from build product files into /public/ins
echo "Pack product files into /public/ins/*"$BUILDNUM".tar.gz"
/public/scripts/CollectRelease $SUNDIR Build$BUILDNUM


## Define and load build label
echo "Define and load GIN_Build_"$BUILDNUM" label"
p4 label -o -t GIN_Build_xxx GIN_Build_$BUILDNUM | sed 's/xxx/'$CNFNAME'/' | p4 label -i
p4 labelsync -l GIN_Build_$BUILDNUM > /dev/null
p4 label -o GIN_Build_$BUILDNUM | sed 's/Options:	unlocked/Options:	locked/' | p4 label -i



## Reopen P4 protect for write to programmers
echo "Reconstruct P4 protect to write-mode"
p4 protect -i < /tmp/$$.p4protect
rm -f /tmp/$$.p4protect

## Copy database scripts to /public/ins/MakeDB
CCBUILD="CC_Build_"$BUILDNUM
p4 sync -n //Globecom/$CNFNAME/Globecom/globalsale/db/...@$CCBUILD > /dev/null
if [ "$?" = "0" ]
then
	echo "Copy files to /public/ins/MakeDB"
	p4 sync //Globecom/$CNFNAME/Globecom/globalsale/db/...@$CCBUILD > /dev/null
	cp $WRKROOTDIR/globalsale/db/* /public/ins/MakeDB
else
	echo ""
	echo $CCBUILD" is not an exist label"
fi

echo ""

exit 0