File: functions

package info (click to toggle)
hsqldb1.8.0 1.8.0.10%2Bdfsg-11%2Bdeb12u1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 13,440 kB
  • sloc: java: 75,802; xml: 11,392; sql: 1,556; sh: 847; makefile: 57
file content (294 lines) | stat: -rw-r--r-- 7,008 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

# 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" ]
}