#!/bin/sh

usage()
{
  echo
  echo "usage: $0 [options] codeline"
  echo
  echo "The following options are recognized:"
  echo
  echo "-h	displays this usage message."
  echo
  echo "-c <client spec> This overides the default client of $client"
  echo "-f <file>  where file is a data file containing assignments."
  echo "           see the default //build/build.env for an example."
  echo
  echo "Note: if you want the file to come from perforce then give the"
  echo "      full depot path ( //<depot>/<path>/<file> ) else it is"
  echo "      assumed the file is local."    

}

# set default values.
setup_file="//build/build.env"
client=build-environment
bash_tmp="c:/cygwin/tmp"
use_p4=false
CODELINE=""
setup_default=true
client_default=true
stat=0

if [ $# -eq 0 ]
then
   usage
   exit 1
fi


while [ $# -gt 0 ]
do
	if [ "$1" = "" ]
	then
		dummy=1
	elif [ $1 = "-h" ]
	then
                usage
		exit 0
	elif [ $1 = "-f" ]
	then
                shift
                if [ $setup_file != $1 ]
                then
                   setup_default=false
                fi 
                setup_file=$1
        elif [ $1 = "-c" ]
        then
                shift
                client=$1
                client_default=false
	else
		if [ x$CODELINE = x ]
		then
			#
			# Codeline to build
			#
			CODELINE=$1
		else
                        echo
			echo "Error: Too many arguments."
                        usage
                        exit 1
		fi
	fi
	shift
done

if [ x$CODELINE = x ]
then
        echo
        echo "Error: The codeline parameter is empty"
	usage
        exit 1
fi


CODELINE=`echo $CODELINE | tr '[A-Z]' '[a-z]'`
PRODUCT=`echo $CODELINE | tr -d [0-9]`

# Capitalize CODELINE
# build.env uses capitals.
UPCODELINE=`echo $CODELINE | tr '[a-z]' '[A-Z]'`
UPPRODUCT=`echo $UPCODELINE | tr -d [0-9]`


# Determine if file comes from perforce.
# If path begins "//" then assume perforce.
f1=`echo $setup_file | awk -F/ '{print $1}'`
f2=`echo $setup_file | awk -F/ '{print $2}'`

if [ "$f1" = "" -a "$f2" = "" ]
then
   use_p4=true

   if [ $setup_default = "false" -a $client_default = "true" ]
   then 
      client=`p4 set | grep P4CLIENT | awk '{print $1}' | awk -F= '{print $2}'`
      stat=`expr $? + $stat`
   fi

fi

if [ $use_p4 = "true" ]
then

# Change client spec option to nocrlf.  Otherwise, will get errors
# when trying to create the environement setup file.
   if [ $OS = "Windows_NT" ]
   then
      p4 -c $client client -o | grep ^Options: | grep " nocrlf "  1> /dev/null
      err=$?

      if [ $err != 0 ]
      then
         echo Client spec modified.  Changed Option "crlf" to "nocrlf".
         p4 -c $client client -o | sed 's/ crlf / nocrlf /' | \
               p4 -c $client client -i
         stat=`expr $? + $stat` 
      fi

   fi

# Change all path field separators "\"  to unix notation of "/".
   client_file=`p4 -c $client where $setup_file | awk '{print $3}' | sed 's/\\\/\//g'`
   stat=`expr $? + $stat`
# get file
   p4 -c $client sync -f $setup_file 1> /dev/null
   stat=`expr $? + $stat`

else
   client_file=$setup_file
fi 


if [ ! -f "$client_file" ]
then
     echo Error: $0, $client_file not found.
     exit 1
fi

# splits is the number of files that csplit will produce. One for each search
# criteria. If you add an additional search criteria, then be sure to
# increase the value of splits by 1 for each criteria added.
splits=2

search='%^ALL_PRODUCTS$%  /"^$"/ %^ALL_${UPPRODUCT}$% /"^$"/'

if [ $UPCODELINE != $UPPRODUCT ]
then
   search=$search' %^${UPCODELINE}$% /"^$"/'
   splits=`expr $splits + 1`
fi


trap 'echo Interrupt! Cleaning up...; rm $bash_tmp/$CODELINE$$*; exit 1' 1 2 15

eval csplit -s -f $bash_tmp/$CODELINE$$ $client_file $search 1>/dev/null
stat=`expr $? + $stat`


if [ ! -f "$bash_tmp/$CODELINE$$0$splits" ]
then
    echo Error: $0, $CODELINE is not defined in $client_file
    rm -f $bash_tmp/${CODELINE}$$*
    exit 1
fi


cat $bash_tmp/${CODELINE}$$0[0-`expr $splits - 1`] | \
     grep = > $bash_tmp/${CODELINE}$$
stat=`expr $? + $stat`

echo CODELINE=$CODELINE >> $bash_tmp/${CODELINE}$$
stat=`expr $? + $stat`

echo PRODUCT=$PRODUCT >> $bash_tmp/${CODELINE}$$
stat=`expr $? + $stat`

# This command was replaced by the csplit command above.
#awk  'BEGIN {RS=""} /'^$UPCODELINE$'|'^ALL_PRODUCTS$'|'^ALL_$UPPRODUCT$'/' \
#     $client_file | grep = > $bash_tmp/$CODELINE$$
# stat=`expr $? + $stat`

# Check status before setting up environment
if [ $stat -ne 0 ]
then
   echo Error: $0 failed to setup environment for $CODELINE.
   exit $stat
fi

awk -F= '{print; print "export " $1}' $bash_tmp/${CODELINE}$$
stat=`expr $? + $stat`


#.  $bash_tmp/$CODELINE$$.env
#stat=`expr $? + $stat`

rm -f $bash_tmp/$CODELINE$$*

if [ $stat -ne 0 ]
then
   echo Error: $0 failed to setup environment for $CODELINE.
   echo "echo $stat"
   exit $stat
fi

#export CODELINE

#echo Environment set for $CODELINE