#!/usr/local/bin/bash
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#

Usage()
{
  echo "Syntax:nachtCompilation normal|weekend Release [BIT64]"
  echo "Complete compilation normal or at weekend (clears worktree)"
  exit 0
}

if [ $# -lt 1 ]; then
 Usage
fi

User=cron
Mode=$1
shift
Release=$1
shift

. /rel/TOOL/tool/bin/SetupMachineEnv.bash ${Release}

#---------------------------------------------------
#
# Prepare special release handling....
#   instead of having 'release features' distributed around the script
#   please decide here on release specific stuff...
#
#
# Special igme version handling
#
if [ "x$Release" = "x73" ]; then
    IgmeVersion="7.3.00.develop"
else
    IgmeVersion=""
fi
#
#---------------------------------------------------
#
# OWN depends on 32/64 support and actual release
#
# RR (i.e. 72,73,74,80) leads to
#   /rel/RR i.e. /rel/73
#      32Bit if 64Bit is not supported (DEC)
#      64Bit if 32Bit is not supported (LINUX)
#   /rel/RR_32
#      32Bit if 64Bit is supported too
#   /rel/RR_64
#      64Bit if 32Bit is supported too
#
if [ "$BIT32Supported" = "NO" -o "$BIT64Supported" = "NO" ]; then
        OWN=/rel/${Release}
else
        if [ "$1" = "BIT64" ]; then
            OWN=/rel/${Release}_64
        else
            OWN=/rel/${Release}_32
        fi
fi

#
# Coding reacts on BIT64 Environment variable set (even if value is 0...)
# So for 32Bit compilation unset BIT64 is needed
#
if [ "$1" = "BIT64" ]; then
     BIT64=1
     export BIT64
     shift
else
     unset BIT64
fi

Pid=$$
LoginTime=`date`
# Reenable this to get history records

TERM=vt100
export TERM

giveup()
{
	exit 0;
}

trap "giveup" HUP INT QUIT TERM PIPE

TST=
echo OWN = $OWN

LOG=$OWN/currentLogin
if [ -f $LOG ]; then
 echo "remuser on `hostname` busy."
 cat $LOG
 giveup
fi

cleanup()
{
        exit 0
}

trap "cleanup" HUP INT QUIT TERM PIPE
#
# Keep only last 80 latest entries
#
( cd $HOME/nachtlauf; rm -rf xxx `ls -t1 | tail +80` )


DBROOT=$OWN/usr
DEV_STUDIO=$Release
PYTHONPATH=$TOOL/lib/Python
PERL5LIB=$TOOL/bin:$TOOL/lib/perl5:$OWN/usr/misc

export OWN DBROOT PYTHONPATH DEV_STUDIO PERL5LIB

#-----------------------------------------------------------------
#
# Update IgmeVersion dependend on release if not already set release specific
#
if [ "x$IgmeVersion" = "x" ]; then
    IgmeRelease="`echo $Release | cut -c1`.`echo $Release | cut -c2`"
    IgmeBuild="`echo $Release | cut -c3-`"
    if [ "x$IgmeBuild" = "x" ]; then
#
# Create the most actual develop iprofile for this release
#
        IgmeVersion="`$TOOL/bin/igme -l | cut -c1-15 | grep develop | grep "^$IgmeRelease" | sort -n | tail -1`"
    else
        if [ $IgmeBuild -lt 10 ]; then
            IgmeVersion="$IgmeRelease.0$IgmeBuild.develop"
        else
            IgmeVersion="$IgmeRelease.$IgmeBuild.develop"
        fi
    fi
fi

echo IgmeBuild=$IgmeBuild
echo IgmeVersion=$IgmeVersion
