#!/bin/sh
# $Id: BuildNumber,v 6.5 2001/06/23 08:02:49 cher Exp $

# Copyright (C) 1996-2001 Alexander Chernov <cher@ispras.ru>

#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

# BuildNumber : build number generation
# SYNOPSYS:
#   BuildNumber <component-name>
# the corresponding number file is updated
# Build information is printed on stdout

# SYNOPSYS:
#   BuildNumber [ options ] component

werror() {
  echo $@ >&2
  exit 1
}

if [ x"${PROJECT_ENV_PREFIX}" = x ]
then
  if [ -f ../env_prefix.sh ]
  then
  . ../env_prefix.sh
  elif [ -f ../../env_prefix.sh ]
  then
  . ../../env_prefix.sh
  else
    echo "Cannot get project environment prefix" >&2
    exit 1
  fi
  if [ x"${PROJECT_ENV_PREFIX}" = x ]
  then
    echo "Cannot get project environment prefix" >&2
    exit 1
  fi
fi

home_var_name=${PROJECT_ENV_PREFIX}'_HOME'
config_var_name=${PROJECT_ENV_PREFIX}'_CONFIG'
mode_var_name=${PROJECT_ENV_PREFIX}'_MODE'

eval L_PRJ_HOME='$'${home_var_name}
eval L_PRJ_CONFIG='$'${config_var_name}
eval L_PRJ_MODE='$'${mode_var_name}

export L_PRJ_HOME L_PRJ_CONFIG L_PRJ_MODE

cfgfile="${L_PRJ_HOME}/etc/.cfg-${L_PRJ_CONFIG}/compile.sh"
[ -f "${cfgfile}" ] && . ${cfgfile}
[ x"${AWK}" = x ] && AWK=awk

noincrease=0
releasemode=0
copybuild=0
outputfile=
dumpempty=0
countnumber=0
usecount=0
numberfile=

while :
do
  case $1 in
    -p) noincrease=1 ;;
    -r) releasemode=1;;
    -e) dumpempty=1;;
    -c) copybuild=1;;
    -o) outputfile=$2; shift;;
    -d) builddb=$2; shift;;
    -N) numberfile=$2; countnumber=1; shift;;
    -U) numberfile=$2; usecount=1; shift;;
    *)  break;;
  esac
  shift
done

if [ x"${countnumber}" = x1 ]
then
  OLDNUM=0
  [ -f "${numberfile}" ] && OLDNUM=`cat "${numberfile}"`
  ${AWK} -v OUTFILE="${numberfile}" -v OLDNUM=${OLDNUM} -f ${L_PRJ_HOME}/sources/tools/newbuild.awk "$@"
  exit 0
fi

if [ x"${usecount}" = x1 ]
then
  CUR_DATE=`date`
  AMI=`whoami 2>/dev/null`
  HOST=`hostname`
  [ x"${numberfile}" = x ] && werror "Release number file not specified"
  [ -f "${numberfile}" ] || werror "Release number file do not exist"
  CUR_BUILD=`cat "${numberfile}"`

  if [ "$outputfile" != "" ]
  then
    echo 'const int  Build_Number =' $CUR_BUILD ';'        >  $outputfile
    echo 'const char Build_Date[] =' \"$CUR_DATE\" ';'  >> $outputfile
    echo 'const char Build_User[] =' \"$AMI\" ';'       >> $outputfile
    echo 'const char Build_Host[] =' \"$HOST\" ';'      >> $outputfile
  else
    echo 'const int  Build_Number =' $CUR_BUILD ';'
    echo 'const char Build_Date[] =' \"$CUR_DATE\" ';'
    echo 'const char Build_User[] =' \"$AMI\" ';'
    echo 'const char Build_Host[] =' \"$HOST\" ';'
  fi
  exit 0
fi

if [ "$1" = "" ]
then
  echo "$0: component name expected" >&2
  exit 1
fi

if [ x"$BUILD_NO_INCREASE" != x ]
then
  noincrease=1
fi

if [ x"$builddb" = x ]
then
  # try to use BUILD_DB environment variable
  if [ x"$BUILD_DB" = x ]
  then
    # check for ./buildinfo
    if [ -f ./buildinfo ]
    then
      FILE=./buildinfo
      noincrease=1
      releasemode=1
    else
      # no information where to fetch build information
      # So, generate some dummy
      dumpempty=1
    fi
  else
    # use environment BUILD_DB
    builddb=$BUILD_DB
    FILE=$builddb/$1.num
  fi
else
  # use parameter
  FILE=$builddb/$1.num
fi

if [ $dumpempty = 1 ]
then
  [ "$outputfile" != "" -a -f "$outputfile" ] && exit 0

  if [ "$outputfile" != "" ]
  then
    cat >$outputfile <<EOF
const int  Build_Number = 0;
const char Build_Date[] = "";
const char Build_User[] = "";
const char Build_Host[] = "";
EOF
  else
    cat <<EOF
const int  Build_Number = 0;
const char Build_Date[] = "";
const char Build_User[] = "";
const char Build_Host[] = "";
EOF
  fi
  exit 0
fi

if [ -f $FILE ]; then :;
else
  echo 0 > $FILE
fi

CUR_BUILD=`cat $FILE`

if [ $copybuild = 1 ]
then
  echo $CUR_BUILD > buildinfo
fi

if [ $releasemode = 0 ]
then
  LOGFILE=${builddb}/$1.log
  CUR_DATE=`date`
  AMI=`whoami 2>/dev/null`
  HOST=`hostname`
  if [ "$noincrease" = "0" ]
  then 
    CUR_BUILD=`expr $CUR_BUILD + 1`
    echo -n $CUR_BUILD > $FILE
  fi
#  echo BuildNumber: Build_Number = $CUR_BUILD >&2
else
  CUR_BUILD=-$CUR_BUILD
fi

[ x"$AMI" = x ] && AMI=UNKNOWN

if [ "$copybuild" = 0 ]
then
  if [ "$outputfile" != "" ]
  then
    echo 'const int  Build_Number = '$CUR_BUILD ';'     >  $outputfile
    echo 'const char Build_Date[] =' \"$CUR_DATE\" ';'  >> $outputfile
    echo 'const char Build_User[] =' \"$AMI\" ';'       >> $outputfile
    echo 'const char Build_Host[] =' \"$HOST\" ';'      >> $outputfile
  else
    echo 'const int  Build_Number = '$CUR_BUILD ';'
    echo 'const char Build_Date[] =' \"$CUR_DATE\" ';'
    echo 'const char Build_User[] =' \"$AMI\" ';'
    echo 'const char Build_Host[] =' \"$HOST\" ';'
  fi
fi

if [ $copybuild = 1 -a "$LOGFILE" != "" ]
then
  echo exported ${CUR_BUILD} $CUR_DATE by ${AMI}@${HOST} >> $LOGFILE
elif [ $releasemode = 0 -a "$LOGFILE" != "" ]
then
  echo "compiled ${CUR_BUILD} $CUR_DATE by ${AMI}@${HOST} (`uname`)">> $LOGFILE
fi

exit 0
