1#!/bin/sh
2# *************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21# *************************************************************
22
23jarfilename="JavaSetup.jar"
24tempdir=/var/tmp/install_$$
25java_runtime="java"
26java_runtime_set="no"
27java_runtime_found="no"
28java_runtime_sufficient="no"
29java_versions_supported="1.4 1.5 1.6"
30rpm2cpio_found="no"
31rpm_found="no"
32is_64bit_arch="no"
33arch64string="x86_64"
34arch64string2="64-bit"
35sunjavahotspot="HotSpot"
36errortext=""
37errorcode=""
38
39start_java()
40{
41    umask 022
42
43    echo "Using $java_runtime"
44    echo `$java_runtime -version`
45    echo "Running installer"
46
47    # looking for environment variables
48
49    home=""
50    if [ "x" != "x$HOME" ]; then
51        home=-DHOME=$HOME
52    fi
53
54    log_module_states=""
55    if [ "x" != "x$LOG_MODULE_STATES" ]; then
56        log_module_states=-DLOG_MODULE_STATES=$LOG_MODULE_STATES
57    fi
58
59    getuid_path=""
60    if [ "x" != "x$GETUID_PATH" ]; then
61        getuid_path=-DGETUID_PATH=$GETUID_PATH
62    fi
63
64    if [ "x" != "x$jrefile" ]; then
65        jrecopy=-DJRE_FILE=$jrefile
66    fi
67
68    # run the installer class file
69    echo $java_runtime $home $log_module_states $getuid_path $jrecopy -jar $jarfilename
70    $java_runtime $home $log_module_states $getuid_path $jrecopy -jar $jarfilename
71}
72
73cleanup()
74{
75    if [ "x$tempdir" != "x" -a -d "$tempdir" ]; then
76        rm -rf $tempdir
77    fi
78}
79
80do_exit()
81{
82    exitstring=$errortext
83    if [ "x" != "x$errorcode" ]; then
84        exitstring="$exitstring (exit code $errorcode)"
85    fi
86
87    # simply echo the exitstring or open a xterm
88    # -> dependent from tty
89
90    if tty ; then
91        echo $exitstring
92    else
93        mkdir $tempdir
94
95        # creating error file
96        errorfile=$tempdir/error
97
98        cat > $errorfile << EOF
99echo "$exitstring"
100echo "Press return to continue ..."
101read a
102EOF
103
104        chmod 755 $errorfile
105
106        # searching for xterm in path
107        xtermname="xterm"
108        xtermfound="no";
109        for i in `echo $PATH | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'`; do
110            if [ -x "$i/$xtermname" -a ! -d "$i/$xtermname" ]; then
111                xtermname="$i/$xtermname"
112                xtermfound="yes"
113                break
114            fi
115        done
116
117        if [ $xtermfound = "no" -a "`uname -s`" = "SunOS" ]; then
118            if [ -x /usr/openwin/bin/xterm ]; then
119                xtermname=/usr/openwin/bin/xterm
120                xtermfound="yes"
121            fi
122        fi
123
124        if [ $xtermfound = "yes" ]; then
125            $xtermname -e $errorfile
126        fi
127    fi
128
129    cleanup
130
131    exit $errorcode
132}
133
134set_jre_for_uninstall()
135{
136    # if "uninstalldata" exists, this is not required
137    if [ ! -d "uninstalldata" ]; then
138        packagepath="RPMS"
139        jrefile=`find $packagepath -type f -name "jre*.rpm" -print`
140        jrefile=`basename $jrefile`
141        if [ -z "$jrefile" ]; then
142            jrefile="notfound"
143        fi
144
145        # check existence of jre rpm
146        if [ ! -f $packagepath/$jrefile ]; then
147            errortext="Error: Java Runtime Environment (JRE) not found in directory: $packagepath"
148            errorcode="4"
149            do_exit
150        fi
151    fi
152}
153
154install_linux_rpm()
155{
156    # Linux requires usage of rpm2cpio to install JRE with user privileges
157    # 1. --relocate /usr/java=/var/tmp does not work, because not all files are
158    #    relocatable. Some are always installed into /etc
159    # 2. --root only works with root privileges. With user privileges only the
160    #    database is shifted, but not the files.
161
162    # On Linux currently rpm2cpio is required (and rpm anyhow)
163
164    find_rpm2cpio()
165
166    if [ ! "$rpm2cpio_found" = "yes" ]; then
167        errortext="Error: Did not find rpm2cpio. rpm2cpio is currently required for installations on Linux."
168        errorcode="11"
169        do_exit
170    fi
171
172    find_rpm()
173
174    if [ ! "$rpm_found" = "yes" ]; then
175        errortext="Error: Did not find rpm. rpm is currently required for installations on Linux."
176        errorcode="12"
177        do_exit
178    fi
179
180    # jrefile=jre-6-linux-i586.rpm
181    # javahome=usr/java/jre1.6.0
182
183    packagepath="RPMS"
184
185    # using "uninstalldata" for uninstallation
186    if [ -d "uninstalldata" ]; then
187        packagepath="uninstalldata/jre"
188    fi
189
190    jrefile=`find $packagepath -type f -name "jre*.rpm" -print`
191    jrefile=`basename $jrefile`
192    if [ -z "$jrefile" ]; then
193        jrefile="notfound"
194    fi
195
196    # check existence of jre rpm
197    if [ ! -f $packagepath/$jrefile ]; then
198        errortext="Error: Java Runtime Environment (JRE) not found in directory: $packagepath"
199        errorcode="4"
200        do_exit
201    fi
202
203    PACKED_JARS="lib/rt.jar lib/jsse.jar lib/charsets.jar  lib/ext/localedata.jar lib/plugin.jar lib/javaws.jar lib/deploy.jar"
204
205    mkdir $tempdir
206
207    trap 'rm -rf $tempdir; exit 1' HUP INT QUIT TERM
208
209    tempjrefile=$tempdir/$jrefile
210    cp $packagepath/$jrefile $tempjrefile
211
212    if [ ! -f "$tempjrefile" ]; then
213        errortext="Error: Failed to copy Java Runtime Environment (JRE) temporarily."
214        errorcode="5"
215        do_exit
216    fi
217
218    # check if copy was successful
219    if [ -x /usr/bin/sum ]; then
220
221        echo "Checksumming..."
222
223        sumA=`/usr/bin/sum $packagepath/$jrefile`
224        index=1
225        for s in $sumA; do
226            case $index in
227                1)
228                    sumA1=$s;
229                    index=2;
230                    ;;
231                2)
232                    sumA2=$s;
233                    index=3;
234                    ;;
235            esac
236        done
237
238        sumB=`/usr/bin/sum $tempjrefile`
239        index=1
240        for s in $sumB; do
241            case $index in
242                1)
243                    sumB1=$s;
244                    index=2;
245                    ;;
246                2)
247                    sumB2=$s;
248                    index=3;
249                    ;;
250            esac
251        done
252
253        # echo "Checksum 1: A1: $sumA1 B1: $sumB1"
254        # echo "Checksum 2: A2: $sumA2 B2: $sumB2"
255
256        if [ $sumA1 -ne $sumB1 ] || [ $sumA2 -ne $sumB2 ]; then
257            errortext="Error: Failed to install Java Runtime Environment (JRE) temporarily."
258            errorcode="6"
259            do_exit
260        fi
261    else
262        echo "Can't find /usr/bin/sum to do checksum. Continuing anyway."
263    fi
264
265    # start to install jre
266    echo "Extracting ..."
267    olddir=`pwd`
268    cd "$tempdir"
269    rpm2cpio $tempjrefile | cpio -i --make-directories
270    rm -f $tempjrefile # we do not need it anymore, so conserve discspace
271
272    javahomeparent=usr/java
273    javahomedir=`find $javahomeparent -maxdepth 1 -type d -name "jre*" -print`
274    javahomedir=`basename $javahomedir`
275    if [ -z "$javahomedir" ]; then
276        javahomedir="notfound"
277    fi
278
279    javahome=$javahomeparent/$javahomedir
280
281    if [ ! -d ${javahome} ]; then
282        errortext="Error: Failed to extract the Java Runtime Environment (JRE) files."
283        errorcode="7"
284        do_exit
285    fi
286
287    UNPACK_EXE=$javahome/bin/unpack200
288    if [ -f $UNPACK_EXE ]; then
289        chmod +x $UNPACK_EXE
290        packerror=""
291        for i in $PACKED_JARS; do
292            if [ -f $javahome/`dirname $i`/`basename $i .jar`.pack ]; then
293                # printf "Creating %s\n" $javahome/$i
294                $UNPACK_EXE $javahome/`dirname $i`/`basename $i .jar`.pack $javahome/$i
295                if [ $? -ne 0 ] || [ ! -f $javahome/$i ]; then
296                    printf "ERROR: Failed to unpack JAR file:\n\n\t%s\n\n" $i
297                    printf "Installation failed. Please refer to the Troubleshooting Section of\n"
298                    printf "the Installation Instructions on the download page.\n"
299                    packerror="1"
300                    break
301                fi
302
303                # remove the old pack file
304                rm -f $javahome/`dirname $i`/`basename $i .jar`.pack
305            fi
306        done
307        if [  "$packerror" = "1" ]; then
308            if [ -d $javahome ]; then
309                /bin/rm -rf $javahome
310            fi
311
312            errortext="Error: Failed to extract the Java Runtime Environment (JRE) files."
313            errorcode="8"
314            do_exit
315        fi
316    fi
317
318    PREFS_LOCATION="`echo \"${javahome}\" | sed -e 's/^jdk.*/&\/jre/'`/.systemPrefs"
319
320    if [ ! -d "${PREFS_LOCATION}" ]; then
321        mkdir -m 755 "${PREFS_LOCATION}"
322    fi
323    if [ ! -f "${PREFS_LOCATION}/.system.lock" ]; then
324        touch "${PREFS_LOCATION}/.system.lock"
325        chmod 644 "${PREFS_LOCATION}/.system.lock"
326    fi
327    if [ ! -f "${PREFS_LOCATION}/.systemRootModFile" ]; then
328        touch "${PREFS_LOCATION}/.systemRootModFile"
329        chmod 644 "${PREFS_LOCATION}/.systemRootModFile"
330    fi
331
332    if [ x$ARCH = "x32" ] && [ -f "$javahome/bin/java" ]; then
333        "$javahome/bin/java" -client -Xshare:dump > /dev/null 2>&1
334    fi
335
336    java_runtime=$tempdir/$javahome/bin/java
337
338    # Make symbolic links to all TrueType font files installed in the system
339    # to avoid garbles for Japanese, Korean or Chinese
340    language=`printenv LANG | cut -c 1-3`
341    if [ x$language = "xja_" -o x$language = "xko_" -o x$language = "xzh_" ]; then
342	    font_fallback_dir=$javahome/lib/fonts/fallback
343	    echo "Making symbolic links to TrueType font files into $font_fallback_dir."
344	    mkdir -p $font_fallback_dir
345	    ttf_files=`locate "*.ttf" | xargs`
346        if [ x$ttf_files = "x" ]; then
347            ttf_files=`find /usr/share/fonts/ -name "*ttf"`
348            if [ x$ttf_files = "x" ]; then
349                ttf_files=`find /usr/X11R6/lib/ -name "*ttf"`
350            fi
351        fi
352	    ln -s $ttf_files $font_fallback_dir
353    fi
354
355    echo "Done."
356    cd "$olddir"
357}
358
359find_rpm2cpio()
360{
361    # searching for rpm2cpio in path
362    for i in `echo $PATH | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'`; do
363        if [ -x "$i/rpm2cpio" -a ! -d "$i/$rpm2cpio" ]; then
364            rpm2cpio_found="yes"
365            break
366        fi
367    done
368}
369
370find_rpm()
371{
372    # searching for rpm in path
373    for i in `echo $PATH | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'`; do
374        if [ -x "$i/rpm" -a ! -d "$i/$rpm" ]; then
375            rpm_found="yes"
376            break
377        fi
378    done
379}
380
381check_architecture()
382{
383    # Check, if system and installation set fit together (x86 and sparc).
384    # If not, throw a warning.
385    # Architecture of the installation set is saved in file "installdata/xpd/setup.xpd"
386    # <architecture>sparc</architecture> or <architecture>i386</architecture>
387    # Architecture of system is determined with "uname -p"
388
389    setupxpdfile="installdata/xpd/setup.xpd"
390
391    if [ -f $setupxpdfile ]; then
392        platform=`uname -p`	# valid values are "sparc" or "i386"
393        searchstring="<architecture>$platform</architecture>"
394        match=`cat $setupxpdfile | grep $searchstring`
395
396        if [ -z "$match" ]; then
397            # architecture does not fit, warning required
398            if [ "$platform" = "sparc" ]; then
399                echo "Warning: This is an attempt to install Solaris x86 packages on Solaris Sparc."
400            else
401                echo "Warning: This is an attempt to install Solaris Sparc packages on Solaris x86."
402            fi
403        fi
404    fi
405}
406
407find_jre_in_path()
408{
409    # searching for java runtime in path
410    for i in `echo $PATH | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'`; do
411        if [ -x "$i/$java_runtime" -a ! -d "$i/$java_runtime" ]; then
412            java_runtime="$i/$java_runtime"
413            java_runtime_found="yes"
414            break
415        fi
416    done
417}
418
419check_jre_version()
420{
421    # check version of an installed JRE
422    javaoutput=`$java_runtime -version 2>&1 | tail ${tail_args} -1`
423    hotspot=`echo $javaoutput | grep $sunjavahotspot`
424    if [ ! -z "$hotspot" ]; then
425        for i in $java_versions_supported; do
426            versionmatch=`echo $javaoutput | grep $i`
427            if [ ! -z "$versionmatch" ]; then
428                java_runtime_sufficient="yes"
429                break
430            fi
431        done
432
433        # check new version format, where version number is not part of line 3 (1.6)
434        if [ ! "$java_runtime_sufficient" = "yes" ]; then
435            javaoutput=`$java_runtime -version 2>&1 | head ${tail_args} -3`
436            for i in $java_versions_supported; do
437                versionmatch=`echo $javaoutput | grep $i`
438                if [ ! -z "$versionmatch" ]; then
439                    java_runtime_sufficient="yes"
440                    break
441                fi
442            done
443        fi
444    fi
445}
446
447check_linux_jre_version()
448{
449    # check version of an installed JRE
450    javaoutput=`$java_runtime -version 2>&1 | head -1`
451
452    for i in $java_versions_supported; do
453        versionmatch=`echo $javaoutput | grep $i`
454        if [ ! -z "$versionmatch" ]; then
455            java_runtime_sufficient="yes"
456            break
457        fi
458    done
459}
460
461check_64bit_architecture()
462{
463    # check system architecture using "uname -m"
464    # unameoutput=`uname -m 2>&1`
465    # x64str=`echo $unameoutput | grep $arch64string`
466
467    # check system architecture using "file /usr/bin/file"
468    fileoutput=`file /usr/bin/file 2>&1`
469    x64str=`echo $fileoutput | grep $arch64string2`
470
471    if [ ! -z "$x64str" ]; then
472        is_64bit_arch="yes"
473        echo "64-bit Linux"
474    fi
475}
476
477# the user might want to specify java runtime on the commandline
478USAGE="Usage: $0 [ -j <java_runtime> ]"
479while getopts hj: opt; do
480    echo "Parameter: $opt"
481    case $opt in
482        j)  java_runtime_set="yes";
483            java_runtime="${OPTARG}"
484            if [ ! -f "$java_runtime" ]; then
485                errortext="Error: Invalid java runtime $java_runtime, file does not exist."
486                errorcode="2"
487                do_exit
488            fi
489            if [ ! -x "$java_runtime" ]; then
490                errortext="Error: Invalid java runtime $java_runtime, not an executable file."
491                errorcode="3"
492                do_exit
493            fi
494            java_runtime_found="yes";
495            ;;
496        h)  echo ${USAGE}
497            errortext=""
498            errorcode=""
499            do_exit
500            ;;
501        \?) echo ${USAGE}
502            errortext=""
503            errorcode=""
504            do_exit
505            ;;
506    esac
507done
508
509# changing into setup directory
510cd "`dirname "$0"`"
511
512# prepare jre, if not set on command line
513if [ "$java_runtime_set" != "yes" ]; then
514    platform=`uname -s`
515    if [ "`uname -s`" = "Linux" ]; then
516        check_64bit_architecture
517        if [ "$is_64bit_arch" = "no" ]; then
518            install_linux_rpm
519        else
520            find_jre_in_path
521            if [ "$java_runtime_found" = "yes" ]; then
522                check_linux_jre_version
523                if [ ! "$java_runtime_sufficient" = "yes" ]; then
524                    errortext="Error: Did not find a valid Java Runtime Environment (JRE). Required JRE versions: $java_versions_supported"
525                    errorcode="14"
526                    do_exit
527                fi
528            else
529                errortext="Error: Did not find an installed Java Runtime Environment (JRE)."
530                errorcode="15"
531                do_exit
532            fi
533        fi
534    elif [ "`uname -s`" = "SunOS" ]; then
535        check_architecture
536        find_jre_in_path
537        if [ "$java_runtime_found" = "yes" ]; then
538            check_jre_version
539            if [ ! "$java_runtime_sufficient" = "yes" ]; then
540                errortext="Error: Did not find a valid Java Runtime Environment (JRE). Required JRE versions: $java_versions_supported"
541                errorcode="9"
542                do_exit
543            fi
544        else
545            errortext="Error: Did not find an installed Java Runtime Environment (JRE)."
546            errorcode="10"
547            do_exit
548        fi
549    else
550        errortext="Error: Platform $platform not supported for Java Runtime Environment (JRE) installation."
551        errorcode="1"
552        do_exit
553    fi
554fi
555
556# jre for Linux is also required, if java runtime is set (for uninstallation mode)
557if [ "$java_runtime_set" = "yes" ]; then
558    platform=`uname -s`
559    if [ "`uname -s`" = "Linux" ]; then
560        set_jre_for_uninstall
561    fi
562fi
563
564start_java
565
566cleanup
567
568exit 0
569