#!/bin/sh -
#
#   newvers -- setup new program version file
#   Copyright (c) Ralf S. Engelschall, <rse@en.muc.de>
#
VERSION="1.0"
DATE="30 Aug 1994"

#   print version information line
print_version () {
    echo "This is NEWVERS, Version $VERSION ($DATE)"
}

#   give general help information
print_help () {
cat <<'EOT'
NEWVERS -- generate/maintain a version information file
Copyright (c) 1994 Ralf S. Engelschall, <rse@en.muc.de>

NEWVERS will create and update a version information file,
which can be setup as a C source file or a Perl source file.

Examples:
    $ newvers -m c -p TestProg version.c
    $ newvers -m perl -p TestProg version.pl
EOT
}

#   give usage information
print_usage () {
cat <<'EOT'
Usage: newvers [options] versionfile
    Options are:
        -l<lang>        set language to one of "c", "perl" 
        -p<progname>    set program-name
        -r<ver>.<rev>   set version to <Ver>.<Rev>
        -d              display current version only
        -V              print NEWVERS version
        -h              print help page
EOT
}

#   process the argument line
LANG=unknown
PROGNAME="-UNKNOWN-"
VERSION=unknown
REPORT=NO
set -- `getopt l:p:r:dVh $*`
if [ $? != 0 ]; then
    print_usage
    exit 2
fi
for opt in $*; do
    case $opt in
        -l)  LANG=$2;          shift; shift  ;;
        -p)  PROGNAME=$2;      shift; shift  ;;
        -r)  VERSION=$2;       shift; shift  ;;
        -d)  REPORT=YES;              shift ;;
        -V)  print_version;           exit 0 ;;  
        -h)  print_help; print_usage; exit 0 ;;  
        --)                    shift; break  ;;
    esac
done
case $# in
    1) VERSIONFILE=$1 ;;
    *) print_usage; exit 1 ;;
esac

#   determine language
if [ "$LANG" = "unknown" ]; then
    case $VERSIONFILE in
        *.c  ) LANG=c    ;;
        *.pl ) LANG=perl ;;
    esac
fi

##   determine version
if [ "$VERSION" = "unknown" ]; then
    if [ -r "$VERSIONFILE" ]; then
        #   grep out current information
        id=`grep "Version [0-9]*.[0-9]*" $VERSIONFILE | \
            head -1 | \
            sed -e 's%.*Version \([0-9]*\.[0-9]*\).*%\1%'`
        version=`echo $id | awk -F. '{ print $1 }'`
        if [ $REPORT = YES ]; then
            revision=`echo $id | awk -F. '{ print $2 }'`
        else
            revision=`echo $id | awk -F. '{ print $2 + 1 }'`
        fi
    else
        #   intialise to first version
        version=1
        revision=0
    fi
else
    #   take given version
    version=`echo $VERSION | awk -F. '{ print $1 }'`
    revision=`echo $VERSION | awk -F. '{ print $2 }'`
fi

if [ $REPORT = YES ]; then
    echo "$version.$revision"
    exit 0;
else
    echo "new version: $version.$revision"
fi

#   create date string
year=`date '+%y'`
month=`date '+%m'`
day=`date '+%d'`

#   create the version file according the the selected language  
tmpfile="/tmp/newvers.tmp.$$"
rm -f $tmpfile
case $LANG in
    c )
        cat >$tmpfile <<'EOF'
/* !! This file was automatically generated by NEWVERS !! */

/* interactive 'hello' string to identify us to the user */
char Hello[] = 
    "This is @PROGNAME@ Version @VERSION@.@REVISION@ (@YEAR@/@MONTH@/@DAY@)";

/* for logfiles, etc. */
char Version[] =
    "@VERSION@.@REVISION@ (@YEAR@/@MONTH@/@DAY@)";

/* a GNU --version output */
char GNUVersion[] =
    "@PROGNAME@ version @VERSION@.@REVISION@";

/* a UNIX what(1) id string */
char WhatID[] =
    "@(#)@PROGNAME@ Version @VERSION@.@REVISION@ (@YEAR@/@MONTH@/@DAY@)";

/* a RCS ident(1) id string */
char RCSIdentID[] =
    "$Id: @PROGNAME@ @VERSION@.@REVISION@ @YEAR@/@MONTH@/@DAY@ $";

EOF
        ;;
    perl )
        cat >$tmpfile <<'EOF'
# !! This file was automatically generated by NEWVERS !!

package Vers;

# interactive 'hello' string to identify us to the user
$Hello = 
    "This is @PROGNAME@ Version @VERSION@.@REVISION@ (@YEAR@/@MONTH@/@DAY@)";

# for logfiles, etc.
$Version =
    "@VERSION@.@REVISION@ (@YEAR@/@MONTH@/@DAY@)";

# a GNU --version output
$GNUVersion =
    "@PROGNAME@ version @VERSION@.@REVISION@";

# a UNIX what(1) id string
$WhatID =
    "@(#)@PROGNAME@ Version @VERSION@.@REVISION@ (@YEAR@/@MONTH@/@DAY@)";

# a RCS ident(1) id string
$RCSIdentID =
    "\$Id: @PROGNAME@ @VERSION@.@REVISION@ @YEAR@/@MONTH@/@DAY@ \$";

1;
EOF
        ;;
    * ) print_usage; exit 1
        ;;
esac

rm -f $VERSIONFILE
sed \
-e "s|@PROGNAME@|$PROGNAME|g" \
-e "s|@VERSION@|$version|g" \
-e "s|@REVISION@|$revision|g" \
-e "s|@YEAR@|$year|g" \
-e "s|@MONTH@|$month|g" \
-e "s|@DAY@|$day|g" <$tmpfile >$VERSIONFILE
rm -f $tmpfile

#eof
