
# Bourne shell (and compatible) script, to be "source".

# $Id: functions,v 1.3 2002/11/23 18:28:17 unsaved Exp $

# functions used by the hsqldb Linux scripts
#

# using $JAVA_HOME
use_java_home()
{
	# jdkhome is an unfortunate name, since it could just as well
	# be a JRE home.  That's probably why Sun calls it JAVA_HOME.
	if [ ! -z "$JAVA_HOME" -a -z "$jdkhome" ] ; then
    	jdkhome=$JAVA_HOME
	fi
} # use_java_home()

#
# parse arguments
#

parse_args() {
while [ $# -gt 0 ] ; do
#    echo "Processing arg: '$1'"
    case "$1" in
        -h|-help) cat <<EOF
Usage: $0 {options} arguments

Options can be

   -h -help
        shows usage
   -jdkhome <path>
        specifies the JDK directory

   -hotspot
   -client
   -server
   -classic
   -native
   -green
        specifies the type of JVM

   -J<jvm_options>
        passes <jvm_option> to JVM
   -cp:p <classpath>
        prepends <classpath> to classpath
   -cp:a <classpath>
        appends <classpath> to classpath

All other options and arguments are passed to the program.
See documentation for details.

EOF
exit 1
;;
        -jdkhome) shift; if [ $# -gt 0 ] ; then jdkhome=$1; fi;;
        #-mainclass) shift; if [ $# -gt 0 ] ; then db_class=$1; fi;;
        -cp|-cp:a)
            shift;
            if [ $# -gt 0 ] ; then
                if [ ! -z "$postfixcp" ] ; then postfixcp="$postfixcp:" ; fi
                postfixcp=$postfixcp$1;
            fi
            ;;

        -cp:p)
            shift;
            if [ $# -gt 0 ] ; then
                if [ ! -z "$prefixcp" ] ; then prefixcp="$prefixcp:" ; fi
                prefixcp=$prefixcp$1;
            fi
            ;;

        -hotspot|-client|-server|-classic|-native|-green) thread_flag=$1;;
        -J-hotspot|-J-client|-J-server|-J-classic|-J-native|-J-green) thread_flag=`expr $1 : '-J\(.*\)'`;;
        -J*) jopt=`expr "$1" : '-J\(.*\)'`; jargs="$jargs \"$jopt\"";;
        *) args="$args \"$1\"" ;;
    esac
    shift
done
} # parse_args()


append_jars_to_cp() {
    # N.b.  This adds all the jars of a directory to $cp.
    # That may be ok in some other situation, but you probably should
    # NOT run "append_jars_to_cp $HSQLDB_HOME/lib" (for one thing, that
    # directory may contain non-inter-compatible libraries).
    dir="$1"
    for ex in jar zip ; do
        if [ "`echo ${dir}/*.$ex`" != "${dir}/*.$ex" ] ; then
            for x in ${dir}/*.$ex ; do
                if [ ! -z "$cp" ] ; then cp="$cp:" ; fi
                cp="$cp$x"
            done
        fi
    done
}

prefixTo() {
    # Purpose of this is to conditionally add the : delimiter.
    # Reason is that an extra delimiter can implicitly add $PWD.
    # Either arg may be null, but that may add $PWD to the resultant path.

    # N.b.  1st arg should be (or eval to) a variable NAME, therefore
    # it should usually be like 'cp', not like '$cp'.

    [ $# -ne 2 ] && {
	echo "ERROR.  Syntax:  origvar=`prefixto newprefix '$origvar'`" 1>&2
	return
    }
    _VARNAME="$1"; shift
    _NEWELEMENT="$1"; shift
    eval _ORIGVAL="\${$_VARNAME}"
    if [ -n "$_ORIGVAL" ]; then 
	eval "$_VARNAME=${_NEWELEMENT}:${_ORIGVAL}"
    else
	eval "$_VARNAME=$_NEWELEMENT"
    fi
    unset _VARNAME
    unset _NEWELEMENT
    unset _ORIGVAL
}

appendTo() {
    # Purpose of this is to conditionally add the : delimiter.
    # Reason is that an extra delimiter can implicitly add $PWD.
    # Either arg may be null, but that may add $PWD to the resultant path.

    # N.b.  1st arg should be (or eval to) a variable NAME, therefore
    # it should usually be like 'cp', not like '$cp'.

    [ $# -ne 2 ] && {
	echo "ERROR.  Syntax:  origvar=`prefixto newprefix '$origvar'`" 1>&2
	return
    }
    _VARNAME="$1"; shift
    _NEWELEMENT="$1"; shift
    eval _ORIGVAL="\${$_VARNAME}"
    if [ -n "$_ORIGVAL" ]; then 
	eval "$_VARNAME=${_ORIGVAL}:$_NEWELEMENT"
    else
	eval "$_VARNAME=$_NEWELEMENT"
    fi
    unset _VARNAME
    unset _NEWELEMENT
    unset _ORIGVAL
}

build_cp() {
    # N.b.  This rebuilds from scratch.  Does not add to an existing cp.
    base="$1"
    cp=   # Clear any previous value

    # Required for Java v. 1.1.  Put Java system libs right up front
    [ -n "$jdkhome" ] && [ -r "$jdkhome/lib/classes.zip" ] &&
     prefixTo cp "$jdkhome/lib/classes.zip"

    [ -n "$base" ] || return   # Nothing to do if no arg given to this funct.
    for Lib in hsqldb.jar servlet.jar; do
	[ -r "$base/lib/$Lib" ] && appendTo cp "$base/lib/$Lib"
    done
}

#
# check JDK
#

check_jdk()
{
	if [ -z "$jdkhome" ] ; then
    		echo "Cannot find JDK. Please set the JAVA_HOME environment variable to point"
    		echo "to your JDK installation directory, or use the -jdkhome switch"
    		echo ""
    		exit 1
	fi

	if [ ! -x "${jdkhome}/bin/java" ] ; then
    		echo "Cannot find JDK at ${jdkhome}. Please set the JAVA_HOME"
    		echo "environment variable to point to your JDK installation directory,"
    		echo "or use the -jdkhome switch"
    		echo ""
    	exit 1
	fi
} # check_jdk()check_jdk()


# JDK tools

build_jdk_cp()
{
	for ex in jar zip ; do
    	# XXX does this still work if ${jdkhome} contains spaces?
    	if [ "`echo ${jdkhome}/lib/*.$ex`" != "${jdkhome}/lib/*.$ex" ] ;then
        	for x in ${jdkhome}/lib/*.$ex ; do
            	if [ ! -z "$cp" ] ; then cp="$cp:" ; fi
            	cp="${cp}$x"
        	done
    	fi
	done
} # build_jdk_cp()

# user-specified prefix and postfix CLASSPATH
build_usr_cp()
{

	if [ ! -z "${prefixcp}" ] ; then
    	cp="${prefixcp}:$cp"
	fi

	if [ ! -z "${postfixcp}" ] ; then
    	cp="$cp:${postfixcp}"
	fi
} # build_usr_cp()

# Simple Age Function
# Adapted from Blaine Simpson's Age script
# Age one file at a time only, so pass it will process only the first parameter
# All the files are moved by one age with the overaged ones being deleted
# Max is aging is set to 3.
# Maybe improved later to add more functionality.
age()
{
	MAXAGE=3
	let OVERAGE=MAXAGE+1
	LOGPATH="$1"
	DIRNAME=`dirname $LOGPATH`
	FILE=`basename $LOGPATH`
	cd $DIRNAME
	i=$MAXAGE
	let j=i+1
	while [ $i -gt 0 ]; do
		[ -e $FILE.$i ] && {
			mv $FILE.$i $FILE.$j 2>&- || {
				echo "ERROR: Failed to move file '$FILE.$i' to '$FILE.$j'"
			}
		}
		let i=i-1
		let j=i+1
	done

	mv $FILE $FILE.$j

	#delete overaged files
	[ -a $FILE.$OVERAGE ] && {
		`rm -rf $FILE.$OVERAGE`
	}

} # age()


# Doing the main processing

pre_main()
{
	#
	# defaults
	#
	use_java_home

	# Process arguments given on the command line.
	#$parse_args "$@"
	# arguements are handled directly by Java main()

	check_jdk

	#ulimit -n 1024

	#
	# main
	#

	#
	# build CLASSPATH
	#

	# dbhome comes first

	build_cp ${dbhome}
	#build_jdk_cp
} # pre_main()

NewerThan() {
    # Must use this in place of the shell "-nt" expression test, because
    # -nt is very non-portable (e.g. not available in SunOS sh).
    [ "$#" -eq 2 ] || {
    	echo "SYNTAX:  NewerThan file/1 file/2" 1>&2
    	return 1
    }
    _LINE1_=`ls -1 -d -t "$1" "$2" 2> /dev/null | head -1`
    [ "$_LINE1_" = "$1" ]
}
