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