#!/bin/sh 
# Create a header file containing a version string, max. revision number and
# some timestamps of the project by scaning for the SVN $Id$-keyword lines 
# of the files supllied a arguments!
#
# The version string is read from the environment variable VERSION, an 
# optional text file, the header file itself or set to some default 
# (in that order!).
#
# The script should be called during the build procedure by make, scons...
# therefore the output header file contains an artificial $Id:-line from
# previous calls and is scanned additionally in case of incremental calls 
# with just a few changed (or no) FILES as argument.
FILES="$@"

# location of this script (specify the input/output filenames below rel. to this!)
DIR=`dirname "$0"`

# Optional text file containing the version string of the last official release
VERSION_FILE="$DIR/version.txt"
# That's the output header file (which is also read if present!)
HEADER_FILE="$DIR/src/version.h"
# Default version string (only used if none of the above files is existing!)
VERSION_DEFAULT="SVN"

# VERSION not set already? Get it from a text file,...
if ( [ -z "$VERSION" ] && [ -f "$VERSION_FILE" ] )
then
  VERSION=`cat "$VERSION_FILE"`
fi
#... or the (previously written) header file ... 
if ( [ -z "$VERSION" ] && [ -f "$HEADER_FILE" ] )
then
  VERSION=`grep '#define VERSION' "$HEADER_FILE" | grep -v VERSION_H | cut -d '"' -f 2`
fi 
#... or just use the default!
if [ -z "$VERSION" ] 
then
  VERSION="$VERSION_DEFAULT"
fi

# always scan additionally the header file
if [ -f "$HEADER_FILE" ]
then
  FILES="$HEADER_FILE $FILES"
fi

# maybe we want to know when the project was built?
BUILT=`date +%Y%m%d%H%M%S`

# extract&analyse the $Id$-keyword lines from the given source files
REVISION=0
CHANGED=0
DATE="2000-01-01"
TIME="00:00:00"
NAME=`basename $HEADER_FILE`
AUTHOR=`basename $0`   
for F in $FILES
do
  LINE=`grep '$Id:' $F`
  R=`echo "$LINE" | tr -s ' ' | cut -d' ' -f4`
  # largest revsion number...
  if ( [ -z $(echo $R | tr '[0-9]' ' ') ] && [ $R -ge $REVISION ] )
  then
    DSTRING=`echo "$LINE" | tr -s ' ' | cut -d' ' -f5`
    TSTRING=`echo "$LINE" | tr -s ' ' | cut -d' ' -f6`
    # build handy number for easy time comparisions 
    D=`echo "$DSTRING" | tr -d '-'`
    T=`echo "$TSTRING" | tr -d ':' | tr -d 'Z'`
    DT=$D$T
    # latest date/time...
    if ( [ -z $(echo $DT | tr '[0-9]' ' ') ] && [ $DT -ge $CHANGED ] )
    then
      REVISION=$R
      CHANGED=$DT
      DATE=$DSTRING
      TIME=$TSTRING
      NAME=`echo "$LINE" | tr -s ' ' | cut -d' ' -f3`
      AUTHOR=`echo "$LINE" | tr -s ' ' | cut -d' ' -f7`   
    fi  
  fi
done

cat <<EOF >$HEADER_FILE
/*
    This file was automatically generated by the 'setversion' script!
    You should not modify it manually (except for the version string), 
    as it may be re-generated.

    Last changed source file (read on input!):   
    \$Id: $NAME $REVISION $DATE $TIME $AUTHOR \$ 
*/

#ifndef VERSION_H
#define VERSION_H

#define VERSION		"$VERSION"
#define REVISION	"$REVISION"
#define CHANGED		$CHANGED
#define BUILT		$BUILT

#endif /* VERSION_H */
EOF

echo "Last change in repository was on $DATE at $TIME in file '$NAME' by '$AUTHOR'"
echo "Prepared header file '$HEADER_FILE' for version/revision: $VERSION/$REVISION"

exit
