#!/bin/sh -
##
##   NEWVERS -- setup new program version file
##   Copyright (c) 1994-1997 Ralf S. Engelschall, <rse@engelschall.com>
##
##   NOTICE: This intentionally written in Bourne-Shell instead
##           of my preferred language Perl because this is also
##           used as a version display tool in "./configire" steps...
##

VERSION="2.1.2"
   DATE="02-05-1997"

#   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-1997 Ralf S. Engelschall, <rse@engelschall.com>

NEWVERS will create and update a version information file,
which can be setup in either plain text or the C or Perl language.

Examples:
    $ newvers -m txt  -p TestProg version.txt
    $ 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 "txt", "c" or "perl" 
        -p<progname>      set program name
        -r<v>.<r>[.pb]<p> set release version string
        -i{v|r|P|p|b|a|s} increase version, revision or {alpha,batch,patch,snap}level
        -d                display current version only
        -D                display current version only (incl. date)
        -V                print NEWVERS version
        -h                print help page
EOT
}

#   process the argument line
LANG=unknown
PROGNAME="-UNKNOWN-"
VERSION=unknown
REPORT=NO
REPORTFULL=NO
INCREASE=P
set -- `getopt l:p:r:i:dDVh $*`
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  ;;
        -i)  INCREASE=$2;      shift; shift  ;;
        -d)  REPORT=YES;              shift ;;
        -D)  REPORT=YES; REPORTFULL=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
        *.txt )       LANG=txt  ;;
        *.c )         LANG=c    ;;
        *.pl | *.pm ) LANG=perl ;;
		* )           echo "Unkown language type"; exit 1 ;;
    esac
fi

#   determine version
if [ "$VERSION" = "unknown" ]; then
    if [ -r "$VERSIONFILE" ]; then
        #   grep out current information
        id=`grep 'Version [0-9]*.[0-9]*[.abps][0-9]* ([0-9]*-[0-9]*-[0-9]*)' $VERSIONFILE | \
            head -1 | \
            sed -e 's%.*Version \([0-9]*\)\.\([0-9]*\)\([.abps]\)\([0-9]*\) (\([0-9]*-[0-9]*-[0-9]*\)).*%\1:\2:\3:\4:\5%'`
        version=`echo $id | awk -F: '{ print $1 }'`
        revision=`echo $id | awk -F: '{ print $2 }'`
        bptype=`echo $id | awk -F: '{ print $3 }'`
        bplevel=`echo $id | awk -F: '{ print $4 }'`
        date=`echo $id | awk -F: '{ print $5 }'`

        if [ $REPORT = NO ]; then
            case $INCREASE in
                b )
                    bplevel=`expr $bplevel + 1`
					bptype=b
                    ;;
                a )
                    bplevel=`expr $bplevel + 1`
					bptype=a
                    ;;
                s )
                    bplevel=`expr $bplevel + 1`
					bptype=s
                    ;;
                P )
                    bplevel=`expr $bplevel + 1`
					bptype=.
                    ;;
                p )
                    bplevel=`expr $bplevel + 1`
					bptype=p
                    ;;
                r ) 
                    revision=`expr $revision + 1`
                    bplevel=0
                    ;;
                v )
                    version=`expr $version + 1`
                    revision=0
                    bplevel=0
                    ;;
            esac
            date=`date '+%d-%m-19%y'`
        fi

    else
        #   intialise to first version
        version=0
        revision=5
        bptype=b
        bplevel=0
        date=`date '+%d-%m-19%y'`
    fi
else
    #   take given version
    VERSION=`echo $VERSION | sed -e 's%\([0-9]*\)\.\([0-9]*\)\([.abps]\)\([0-9]*\).*%\1:\2:\3:\4%'`
    version=`echo $VERSION | awk -F: '{ print $1 }'`
    revision=`echo $VERSION | awk -F: '{ print $2 }'`
    bptype=`echo $VERSION | awk -F: '{ print $3 }'`
    bplevel=`echo $VERSION | awk -F: '{ print $4 }'`
    date=`date '+%d-%m-19%y'`
fi

if [ $REPORT = YES ]; then
    if [ $REPORTFULL = YES ]; then
        echo "$version.$revision$bptype$bplevel ($date)"
    else
        echo "$version.$revision$bptype$bplevel"
    fi
    exit 0;
else
    echo "new version: $version.$revision$bptype$bplevel ($date)"
fi

#   create date string
year=`date '+19%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
    txt )
        cat >$tmpfile <<'EOF'

  This is @PROGNAME@ Version @VERSION@.@REVISION@@BPTYPE@@BPLEVEL@ (@DAY@-@MONTH@-@YEAR@)

EOF
        ;;
    c )
        cat >$tmpfile <<'EOF'
/* !! This file was automatically generated by NEWVERS !! */

/* for logfiles, etc. */
char @PROGNAME@_Version[] =
    "@VERSION@.@REVISION@@BPTYPE@@BPLEVEL@ (@DAY@-@MONTH@-@YEAR@)";

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

/* a GNU --version output */
char @PROGNAME@_GNUVersion[] =
    "@PROGNAME@ Version @VERSION@.@REVISION@@BPTYPE@@BPLEVEL@";

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

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

/* a WWW id string */
char @PROGNAME@_WebID[] =
    "@PROGNAME@/@VERSION@.@REVISION@@BPTYPE@@BPLEVEL@";

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

package Vers;

# for logfiles, etc.
$@PROGNAME@_Version =
    "@VERSION@.@REVISION@@BPTYPE@@BPLEVEL@ (@DAY@-@MONTH@-@YEAR@)";

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

# a GNU --version output
$@PROGNAME@_GNUVersion =
    "@PROGNAME@ Version @VERSION@.@REVISION@@BPTYPE@@BPLEVEL@";

# a UNIX what(1) id string
$@PROGNAME@_WhatID =
    "@(#)@PROGNAME@ Version @VERSION@.@REVISION@@BPTYPE@@BPLEVEL@ (@DAY@-@MONTH@-@YEAR@)";

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

# a WWW id string
$@PROGNAME@_WebID =
    "@PROGNAME@/@VERSION@.@REVISION@@BPTYPE@@BPLEVEL@";
1;
EOF
        ;;
    * ) print_usage; exit 1
        ;;
esac

rm -f $VERSIONFILE

#   now create the version file
sed \
-e "s|@PROGNAME@|$PROGNAME|g" \
-e "s|@VERSION@|$version|g" \
-e "s|@REVISION@|$revision|g" \
-e "s|@BPTYPE@|$bptype|g" \
-e "s|@BPLEVEL@|$bplevel|g" \
-e "s|@YEAR@|$year|g" \
-e "s|@MONTH@|$month|g" \
-e "s|@DAY@|$day|g" <$tmpfile >$VERSIONFILE
rm -f $tmpfile


##EOF##
