#!/bin/sh

########################################################################
#             (c) Copyright 2009 Lexmark R&D Corporation               #
#                        All rights reserved                           #
########################################################################
#                                                                      #
#  This script will silently create the .OEM folder containing the     #
#  filtered applist.xml which will be read by the Device Monitor and   #
#  and the Application List Manager.				       #
#                                                                      #
########################################################################

DRIVER_PATH=$1
JAVA_CMD=java
OEM=lexmark
REQUIRED_VERSION=1.6

# Check if it's dell or lexmark
if [ ${DRIVER_PATH} = "/usr/local/dell/legacy" ]; then
    OEM=dell
fi

# Check possible location of JRE
if [ -f "/usr/local/$OEM/legacy/jre/bin/java" ]; then
	JAVA_CMD="/usr/local/$OEM/legacy/jre/bin/java"
elif [ -f "/usr/local/$OEM/fwu_legacy/jre/bin/java" ]; then
	JAVA_CMD="/usr/local/$OEM/fwu_legacy/jre/bin/java"
elif [ -f "/usr/local/$OEM/wsu_legacy/jre/bin/java" ]; then
	JAVA_CMD="/usr/local/$OEM/wsu_legacy/jre/bin/java"
elif [ -f "/usr/bin/java" ]; then
	JAVA_CMD="/usr/bin/java"
elif [ -f "/etc/alternatives/java" ]; then
	JAVA_CMD="/etc/alternatives/java"
else
    xmessage -buttons "OK" "There is no Java Runtime Environment (JRE) detected in your system.

Kindly download the driver with included JRE or use openJDK." -center
    exit 1
fi

CLASS_PATH="$DRIVER_PATH/jar/xstream-1.3.1.jar:\
$DRIVER_PATH/jar/xpp3_min-1.1.4c.jar:\
$DRIVER_PATH/jar/config.jar:\
$DRIVER_PATH/jar/tools.jar:\
$DRIVER_PATH/jar/devicecore.jar:\
$DRIVER_PATH/jar/localize.jar:\
$DRIVER_PATH/jar/uiframework.jar:\
$DRIVER_PATH/jar/appListManager.jar"

OPTIONS="-Xms4m -Xmx8m -Djava.library.path=$DRIVER_PATH/lib/"
TARGET="mdwapclss.apps.applistmanager.SilentAppListManager"

export LD_LIBRARY_PATH="$DRIVER_PATH/lib/"

$JAVA_CMD -version 2> /tmp/java_version
VERSION=`cat /tmp/java_version | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`
rm /tmp/java_version
VERSION=`echo $VERSION | awk '{ print substr($1, 1, 3); }' | sed -e 's;\.;0;g'`
REQUIRED_VERSION=`echo $REQUIRED_VERSION | sed -e 's;\.;0;g'`

if [ $VERSION ]
then
      if [ $VERSION -ge $REQUIRED_VERSION ]
      then
          if [ ! -f "/home/$USER/.$OEM/applist.xml" ]; then
              ${JAVA_CMD} -classpath ${CLASS_PATH} ${OPTIONS} ${TARGET}
          fi

      else
	xmessage -buttons "OK" "The version of Java Runtime Environment (JRE) detected in your system is below the required version.
The required version for Java Runtime Environment (JRE) should be 1.6 or higher." -center
	exit 1
      fi
else
	xmessage -buttons "OK" "The version of Java Runtime Environment (JRE) detected in your system is below the required version.
The required version for Java Runtime Environment (JRE) should be 1.6 or higher." -center
	exit 1
fi


