#!/bin/sh

echo "Executing $0 "$@""
prog=`basename $0`

usage()
{
 echo
 echo "$prog <codeline> <product label> <base codeline> <base label>"
 echo
 echo "eg. $prog configbuilder CB.2.0.0.1 redcell22 RC.2.2.0.42"
 echo

 echo "$prog will integrate the nt install from the base codeline product to the"
 echo "product branch of the release depot.  It then updates the setup.ini file"
 echo "and the raw files to the release depot."
}

if [ "$DEBUG" = "0" ]
then
    set -vx
fi

stat=0

if [ $# -lt 2 ]
then
    echo $prog, Error: Incorrect number of parameters passed
    usage
    exit 1
fi

CODELINE=$1
# Make sure codeline is lower case.
CODELINE=`echo $CODELINE | tr '[A-Z]' '[a-z]'`

from="//release/internal/$3/CDLayout/install/nt"
from_label=$4
to_label=$2
to="//release/internal/$CODELINE/CDLayout/install/nt"

# setup environment.
setenv $CODELINE > /tmp/env.$$

if [ $? != 0 ]
then
        echo setenv $CODELINE failed
        exit 1
fi


. /tmp/env.$$
if [ $? != 0 ]
then
        echo setenv $CODELINE failed
        exit 1
fi

rm /tmp/env.$$


PRODUCT=`echo $CODELINE | tr -d [0-9]`
SOURCE_CODELINE=$CODELINE

# CODELINE is changing from that defined by setenv here.  Be sure that any
# future reference to CODELINE is agreeable to this.
if [ "$PRODUCT" = "$CODELINE" ]
then

   VER=`echo $to_label | awk -F. '{ print $2$3}'`
   if [ $VER" = "" ]
   then
      echo "$prog, Error: Could not determine version from label $to_label"
      exit 1
   fi

   CODELINE=$CODELINE$VER
   export CODELINE
fi


base=false
if [ "$PRODUCT" != "oware" ]
then
    base=true
    if [ x$from_label = x ]
    then
       echo $prog, Error: Incorrect number of parameters passed
       usage
       exit 1
    fi
fi

# Verify that the label version and codeline version are the same.

if [ $base = "true" ]
then
    if [ "`echo $3 | tr -d '[a-z]'`" != "`echo $from_label | awk -F. '{print $2$3}'`" ]
    then
        echo "$prog, Error: Label $from_label and Codeline $3 are not compatible"
        exit 1
    fi
fi


if [ "`echo $CODELINE | tr -d '[a-z]'`" != "`echo $to_label | awk -F. '{print $2$3}'`" ]
then
    echo "$prog, Error: Label $to_label and $CODELINE are not compatible"
    exit 1
fi


p4path=""

# client-spec to perform operations.
rel_client="scm-${CODELINE}-release"
int_client="scm-${CODELINE}-integrate"
build_client="scm-${CODELINE}-build"
source_client="scm-${CODELINE}-source"
environment_client="build-environment"
BUILDENV_ROOT=`p4 -c $environment_client info | grep "Client root" | \
        awk '{print $3}' | sed 's/\\\/\//g'`
if [ "$BUILDENV_ROOT" != "" ]
then
     BUILDENV_ROOT="$BUILDENV_ROOT/tmp/work/$CODELINE/buildenv"
else
     echo "$prog, Error: BUILDENV_ROOT not defined."
     exit 1
fi


# Get client root.
release_root=`p4 -c $rel_client info | grep "Client root" | awk '{print $3}' \
               | sed 's/\\\/\//g'`
stat=`expr $? + $stat`

build_root=`p4 -c $build_client info | grep "Client root" | awk '{print $3}' \
   | sed 's/\\\/\//g'`
stat=`expr $? + $stat`

base_root=`p4 -c $source_client info | grep "Client root" | awk '{print $3}' \
   | sed 's/\\\/\//g'`
stat=`expr $? + $stat`

if [ "$release_root" = "" -o "$build_root" = "" -o "$base_root" = "" ]
then
    echo "$prog, Error: root undefined"
    exit 1
fi

if [ x$LOG_DIR = x ]
then
    LOG_DIR=$release_root/logs
fi


if [ -d $release_root ]
then
    rm -fr $release_root/$CODELINE/*
fi
stat=`expr $? + $stat`

if [ -d $build_root ]
then
    rm -fr $build_root/*
fi
stat=`expr $? + $stat`

if [ $stat -ne 0 ]
then
   echo "$prog exiting due to errors"
fi

p4path="CDLayout/install/nt"

# From files must be on client before integrate.
# flush client workspace for integration.
# I would send output to /dev/null but perforce exits with 0 on an error
# The only way to know there is an error is to grep for error:

if [ $base = "true" ]
then 

    echo "Using $int_client for integration"
    p4 -s -c $int_client flush -f $from/...@$from_label | grep ^error:

    if [ $? -eq 0 ]
    then
      echo "$prog, Error: 'p4 -s -c $int_client flush -f $from/...@$from_label' failed"
      stat=1
    fi


    echo "Integrating  $from/...@$from_label to $to/..."
    p4 -s -c $int_client integrate -i $from/...@$from_label \
       $to/... | tee -a $LOG_DIR/integrate.out | \
       grep -v "all revision(s) already integrated." | grep ^error:

    if [ $? -eq 0 ]
    then
       echo "$prog, Error: Integration failed."
       stat=1
    fi


# submit integration

    if [ $stat -eq 0 ]
    then
      echo "Submitting integration"
      p4_submit "Release integration from $3 @$from_label to $1 for $to_label" \
              $int_client 
      stat=`expr $? + $stat`
    else
      echo "$prog, Warning: Integration not submitted due to errors"
    fi

fi


# Get raw files from build_client, these map onto the release_client spec.
# This should include both raw files from the source depot and the jars from
# the build depot.


echo "Pulling raw files from //$build_client/...@$to_label"
p4 -s -c $build_client sync -f //$build_client/...@$to_label \
  | tee $LOG_DIR/rawfiles.out | grep ^error:

if [ $? -eq 0 ]
then
   echo "$prog, Error: 'p4 -s -c $build_client sync -f $build_root/...@$to_label' failed."
   stat=1
fi


if [ "$PRODUCT" = "oware" ]
then

     echo "Adding files to release from //oware/$SOURCE_CODELINE/...@$to_label if they exist"
     p4 -c $rel_client sync -f //oware/$SOURCE_CODELINE/...@$to_label
     stat=`expr $? + $stat`

     # Create empty directories that perforce misses.
     if [ ! -f $BUILDENV_ROOT/release.bom ]
     then
        p4 -c $environment_client sync -f //build/$CODELINE/buildenv/release.bom     
        stat=`expr $? + $stat`
     fi

     for file in `cat $BUILDENV_ROOT/release.bom | sed 's/\.\.\.//g'`
     do
         file=`echo $file | eval sed 's/^$CODELINE\\\\//$PRODUCT\\\\//g'`

         if [ "$file" = "" ]
         then
             echo "$prog, Error: "`echo $file | eval sed 's/^$CODELINE\\\\//$PRODUCT\\\\//g'` failed."
             stat=1
         fi

         if [ ! -d "$build_root/$file" ]
         then
            if [ ! -f "$build_root/$file" ]
            then
               echo "$prog, Warning: $file does not exist."
               echo " Creating directory $build_root/$file"
               mkdir -p $build_root/$file
               stat=`expr $? + $stat`
            fi
         fi
     done

     install_root=$base_root/install-src/nt/$CODELINE
     if [ -d $install_root ]
     then
         echo "Error: install_root not defined."
         echo "$prog aborted"
         exit 1
     fi

     rm -fr $install_root/*
     stat=`expr $? + $stat`

     echo "Pulling install source from $source_client to $install_root/..."
     p4 -s -c $source_client sync -f $install_root/... \
       | tee $LOG_DIR/installsrc.out | grep "^error:"

     if [ $? -eq 0 ]
     then
        echo "$prog, Error: 'p4 -s -c $source_client sync -f $install_root/...' failed."
        stat=1
     fi


     OWARE_VERSION=`echo $to_label | awk -F. \
              'BEGIN {ORS=""} {for (i=2; i<=NF-1; i++) print $i"."; print $NF}'`
     export OWARE_VERSION ; echo OWARE_VERSION = $OWARE_VERSION


# Convert / to \ for dos.  Temp until bash version of build_oware.bat used.
     PUBLISH_PATH=$release_root/$CODELINE/CDLayout/install/nt
     PUBLISH_PATH=`echo $PUBLISH_PATH | sed  's/\//\\\/g'`     
     export PUBLISH_PATH ; echo PUBLISH_PATH = $PUBLISH_PATH

     STAGE_PATH=$build_root
     STAGE_PATH=`echo $STAGE_PATH | sed  's/\//\\\/g'`
     export STAGE_PATH ; echo STAGE_PATH = $STAGE_PATH

     PROJECT_PATH=$install_root/installshield/Install

     cd $PROJECT_PATH

     PROJECT_PATH=`echo $PROJECT_PATH  | sed 's/\//\\\/g'`
     export PROJECT_PATH ; echo PROJECT_PATH = $PROJECT_PATH
     
     OWARE_SRC=$base_root/$SOURCE_CODELINE
     OWARE_SRC=`echo $OWARE_SRC | sed 's/\//\\\/g'`
     export OWARE_SRC ; echo OWARE_SRC = $OWARE_SRC
     
     OW3RD_SRC=$base_root/oware3rd
     OW3RD_SRC=`echo $OW3RD_SRC | sed 's/\//\\\/g'`
     export OW3RD_SRC ; echo OW3RD_SRC = $OW3RD_SRC

     if [ "$OWARE_VERSION" = "" -o \
          "$PUBLISH_PATH"  = "" -o \
          ! -d "$STAGE_PATH"    -o \
          ! -d "$PROJECT_PATH"  -o \
          ! -d "$OWARE_SRC"     -o \
          ! -d "$OW3RD_SRC" ]
     then
          echo "$prog, Error: Environment not completely defined"
          exit 1
     fi


     build_oware.bat

     update_depot -c $rel_client $release_root/$CODELINE/CDLayout/install/...
     stat=`expr $? + $stat`
     
else
     setupini="$release_root/$CODELINE/$p4path/setup.ini"
     rawini="$build_root/$PRODUCT/install/setup.ini"


     echo "Modifying $setupini"
     eval sed 's/^Version=.*/Version=$to_label/' $rawini > $setupini 
     stat=`expr $? + $stat`


     release_files=`dirname $setupini`
     update_depot -c $rel_client $release_files/... 
     stat=`expr $? + $stat`
fi


# submit changes
if [ $stat -eq 0 ]
then
    echo "Submitting install files using client $rel_client"
    p4_submit "Install files for $to_label" $rel_client
    stat=`expr $? + $stat`

    echo "Adding files to label $to_label"
    p4 -s -c $rel_client labelsync -a -l $to_label $release_root/$CODELINE/...
    stat=`expr $? + $stat`

else
    echo "$prog, Warning: Raw files not submitted due to errors"
fi


exit $stat