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