#!/bin/sh

# $Id: pkgbuild,v 1.9 2005/07/25 02:42:10 unsaved Exp $

# TODO:  Perhaps use RCS keywords to generate versions and write them
#        into the install scripts, the info file, and to use in the
#        stream package file name.

# Makes the very large assumption that files here are owned by
# user $USER and group $USER, and therefore fixes ownerships from
# "$USER $USER$" to "root bin".

set +u

PROGNAME=`basename "$0"`

Failout() {
    echo "Aborting $PROGNAME.  $@" 1>&2
    exit 1
}

case "`uname`" in
    SunOS)
	echonoterm() {	# Echo without trailing newline
	    echo "$@\c"
	}
    ;;
    Linux)
	echonoterm() {	# Echo without trailing newline
	    echo -n "$@"
	}
    ;;
    *)
    	echo 'Aborting.  Your OS is not supported yet.  If you seriously' 1>&2
	echo "want support for it, email ${AUTHOR_EMAILADDR}."
	exit 10
    ;;
esac

# Being VERY careful here, because we do a relative "rm -rf" below!!
[ "$0" = "./$PROGNAME" ] ||
 Failout "You have to run this script like './$PROGNAME'"

GENPROTO=
while [ "$#" -gt 0 ]; do
    case "$1" in -*n*)
    	pkgmk() {
	     echo "pkgmk $@" 1>&2
    	}
    	pkgtrans() {
	     echo "pkgtransmk $@" 1>&2
    	}
    	compress() {
	     echo "compress $@" 1>&2
    	}
    	rm() {
	     echo "rm $@" 1>&2
    	}
	;;
    esac
    case "$1" in -*p*) GENPROTO=1;; esac
    case "$1" in -*v*) set -x;; esac # Verbose
    shift
done
[ -n "$GENPROTO" ] || echo "Warning:  Using existing .proto file.
Quit and invoke with '-p' if any component file(s) have changed."


getent passwd hsqldb > /dev/null ||
 Failout "You need to have the user account 'hsqldb' on this sytem (dead shell ok)"
getent group hsqldb > /dev/null ||
 Failout "You need to have the group 'hsqldb' on this sytem"

PKGNAME=`sed -n '/^PKG=/ {
    s:^PKG=::
    p
}' cfg/HSQLhsqldb.info` ||
 Failout 'Failed to package name version from info file'
[ -n "$PKGNAME" ] || Failout 'Failed to retrieve package name from info file'
VERINFO=`sed -n '/^VERDIR=/ {
    s:^VERDIR=::
    p
}' cfg/HSQLhsqldb.info` || Failout 'Failed to retrieve version from info file'
[ -n "$VERINFO" ] || Failout 'Failed to retrieve version from info file'
VERPOST=`sed -n '/^BASE=/ {
    s:^BASE=::
    p
}' cfg/HSQLhsqldb.postinstall` ||
 Failout 'Failed to retrieve version from info postinstall script'
[ -n "$VERPOST" ] ||
 Failout 'Failed to retrieve version from info postinstall script'
VERPRE=`sed -n '/^BASE=/ {
    s:^BASE=::
    p
}' cfg/HSQLhsqldb.preremove ` ||
 Failout 'Failed to retrieve version from info preremove script'
[ -n "$VERPRE" ] ||
 Failout 'Failed to retrieve version from info preremove script'

[ "$VERPRE" = "$VERPOST" ] || Failout  \
 "Version specs (BASE) don't agree in preremove script and postinstall script"
[ "$VERPRE" = "$VERINFO" ] || 
 Failout "Version specs (BASE and VERDIR) don't agree in preremove script and 
info script"

case "$VERPRE" in hsqldb-*);; *)
    Failout  \
"Sorry, but this script requires BASE/VERDIR to be of the form 'hsqldb-VERSION'
  (Yours is '$VERPRE')";;
esac

VER=`echo $VERPRE | sed s:hsqldb-::`

PWD=`pwd`   # Bourne doesn't necessarily set $PWD
SRCBASE=`dirname $PWD`
SRCBASE=`dirname $SRCBASE`
SRCBASE=`dirname $SRCBASE`

[ -f "$SRCBASE/lib/hsqldb.jar" ] ||
Failout "Looks like your hsqldb.jar file is not built"

PROTOFILE="`echo cfg/H*.proto`"
[ -f "$PROTOFILE" ] ||
 Failout "You need to fix cfg/H*.proto, or fix the wildcard in this script
Even if you're generating it, you have to start with an old version."

[ -f cfg/HSQLhsqldb.copyright ] ||
cp -p ../../../doc/hsqldb_lic.txt cfg/HSQLhsqldb.copyright

[ -z "$USER" ] && USER=LOGNAME
export USER
[ -n "$GENPROTO" ] && {
    perl -v > /dev/null 2>&1 ||
     Failout 'You need perl in your search path to generate the proto file'
    echo 'Generating prototype file...'
    # Remove all lines after "# REGENERATE..."
    perl -pi -we 'if (/^# REGENERATE AFTER HERE/i) {print; exit;}' $PROTOFILE
    (cd $SRCBASE && pkgproto '.=$VERDIR') >> $PROTOFILE
    perl -ni.safe -we '
	next if m:^\s*\S\s+none\s+\$VERDIR/classes:;
	#next if m:^\s*\S\s+none\s+\$VERDIR/src:; Keep SOURCE!
	next if m:^[^#=]*build/packaging:;
    next if m:^[^#]*[/ ]CVS:;
	s/ $ENV{USER} $ENV{USER}$/ root bin/;
	s/ 0644 root bin$// unless /^\!default\s/;
	s/ 0664 root bin$// unless /^\!default\s/;
	s/ 0775 root bin$/ 0755 root bin/;
	print;
    ' $PROTOFILE
}

# Be careful if you edit this
[ -r "$PKGNAME" ] && {
    echonoterm "Remove old dir package '$PKGNAME'?  "
    read REPLY
    case "$REPLY" in [Yy]*)
    	echonoterm 'Removing old dir package...  '
    	rm -rf "./$PKGNAME"
    	echo Done;;
    	[Qq]*) exit 0;;
    esac
}

echo "Building version $VER with source base $SRCBASE"
pkgmk -o -b $SRCBASE -d . -f $PROTOFILE || exit 1

# Package file name format is the Solaris package standard:
#   PKGNAME-ver_with_underscores-platform.pkg[.Z]
# So, we need to convert dots to underscores in the version name
export VER
VER="`perl -we '$_ = $ENV{VER}; s:\.:_:g; print;'`"

echonoterm  \
 "ENTER to quit, Y to build stream package ${PKGNAME}-${VER}-solaris.pkg.Z:  "
read REPLY
case "$REPLY" in [Yy]*)
    rm -f "${PKGNAME}-${VER}-solaris.pkg" "${PKGNAME}-${VER}-solaris.pkg.Z"
    pkgtrans -os . "$PWD/${PKGNAME}-${VER}-solaris.pkg" $PKGNAME &&
    echonoterm 'Now compressing... '
    compress "${PKGNAME}-${VER}-solaris.pkg"
    echo 'Done
(If there is a message above that the file is "unchanged", that just means that
the file compression was skipped because compression resulted in no savings).'
    ;;
    [Qq]*) exit 0;;
esac
