#! /bin/sh
#stringextr: | sed -n -e 's/^[^"]*"\([^"]*\)".*$/\1/p'`

if test "x$1" = x-g; then mode=gen; shift; fi
if test "x$1" = x-j; then shift; juicy=$1; shift; else juicy=$1; fi

if test -r $2; then
	gen=`grep " GENERATION " $2 | sed -n -e 's/^[^0-9]*\([0-9]*\)[^0-9]*$/\1/p'`
	if test "x$gen" = x; then gen=1; else gen=`expr $gen + 1`; fi
else
	gen=1
fi

ver=`head -25 $1 | sed -n -e 's/^\* \([^ ]*\) .*$/\1/p' | head -1`

if test "x$ver" = x; then ver=UNKNOWN; fi

creat=`date +%a\ %b\ %d\ %Y\ %X\ %Z`

line=`head -25 $juicy | grep '^= History ==' | head -1`

fruit=`echo "$line" | sed -n -e 's/^[^#]*#\([^#]*\)#$/\1/p' | head -1`

vend=`echo "$line" | sed -n -e 's/^=[^=]*=[=]*[ ]*\([^ ]*\)[ ]*.*$/\1/p' | head -1`

if grep HYBRID /etc/redhat-release >/dev/null 2>&1; then 
	tropic=define
	echo "version $ver $fruit #$gen by $vend at $creat"
else
	tropic=undef
	echo "version $ver GENERIC #$gen by $vend at $creat"
fi

if test x$mode = x; then exit 0; fi

cat >$2 <<EOF
/*
 * This file was generated by verextr.sh. Any changes made will go away.
 */

#ifndef VERSION_H
#define VERSION_H

#define VERSION "$ver"
#define BUILDDATE "$creat"
#define GENERATION $gen
#define FRUIT "$fruit"
#$tropic $fruit

#endif

EOF

