#!/bin/sh

################################################################################
# Check the target host for installation requirements. At the moment that
# means checking the java version.
#
# Code42 Software Inc, Minneapolis, MN
# http://www.code42.com/
# 
# Copyright (c) 2008 Code42 Software, All Rights Reserved
################################################################################

BINSLOC="/bin /usr/bin /usr/local/bin"
OKJAVA="1.5 1.6 1.7"

echo ""
echo "Reviewing Java installation"
while [ "${JAVACHECK}" != "1" ] ; do

        for binpath in ${BINSLOC} ; do
                if [ -f ${binpath}/java ] ; then
                        JAVACOMMON=${binpath}/java
                fi
        done

        if [ "${JAVACOMMON}" != "NULL" ] ; then
        
                JAVAOUTPUT=`$JAVACOMMON -version 2>&1`
                if [ $? != 0 ]; then

                    echo ""
                    echo "Unable to execute Java in order to determine version.  Try installing"
                    echo " the package from /tmp"
                    exit 1
                fi
                JAVAVERS=`echo "$JAVAOUTPUT" | grep version | cut -d\" -f 2`
		        JAVABUILD=`echo "$JAVAOUTPUT" | head -n 2 | tail -1 | sed s/_.\*//g`
                
                # Consider only the major version #
                JAVAVERS=`echo $JAVAVERS | awk '{print substr($0,0,3)}'`
        
                JAVAVERCHECK=0
                for jv in ${OKJAVA} ; do
                        if [ "${jv}" = "${JAVAVERS}" ] ; then
                                JAVAVERCHECK=1
                                JAVACHECK=1
                        fi
                done
                
                if [ "$JAVAVERCHECK" = "0" ] ; then
                        echo ""
                        echo "The current version of Java, ${JAVAVERS}, is incompatible with CrashPlan."
                        echo "Please install one of the following version of Sun Java Runtime: ${OKJAVA}"
                        JAVACHECK=0
                fi      
        
                JAVABUILDER=`echo $JAVABUILD | cut -d\  -f 1`
                if [ "${JAVABUILDER}" != "Java(TM)" ] ; then
                        echo ""
                        echo "The current installed version of Java is not the Sun Java Runtime Environment."
                        echo "CrashPlan requires the Sun JRE."
                        JAVACHECK=0
                fi
        fi

        if [ "${JAVACHECK}" = "0" ] ; then
                echo ""
                echo "ERROR: Failed to find an acceptable Sun Java Runtime Environment"
                echo "These paths were searched: ${BINSLOC}"
                exit 1
        fi

done

exit 0
