#!/bin/sh

#--
#-- A Shell Script to start MarkVision Professional Client 
#-- application.
#--
#-- Copyright (c) 1999,2005 Lexmark International, Inc.
#-- All Rights Reserved.
VERSION=":11.2.1"
#-- Java VM system defines can be passed to the Java VM by specifying
#-- the defines in the VMDEFINES environment variable. To specify
#-- multiple defines, enclose the define statements in quotes. For
#-- example:
#--
#-- % export VMDEFINES="-Duser.language=de -Duser.region=DE"
#--

BUILD_NO=`echo ${VERSION} | sed -e 's/:.*//g'`
VERSION=`echo ${VERSION} | sed -e 's/.*://g'`

DEBUG="0"
LOGFILE=/dev/null
JAVALOGFILE=/dev/null
FJLOGFILE=/dev/null
DEBUGARG=""

MVP_HOME=/usr/mvp
JAVA_HOME=/usr/mvp_java
JAVA_BIN=${JAVA_HOME}/bin/java

#-- Setting the system type
systype=`uname -a | (read p1 p2 p3 p4 p5 p6 p7 p8 p9; echo ${p1})`
if [ "${systype}" = "SunOS" ]; then
   CHECK_DISPLAY="/usr/openwin/bin/xdpyinfo"
   XTERM_CMD="/usr/openwin/bin/xterm"
   EXTRA=""
elif [ "${systype}" = "Linux" ]; then
	if [ -x "/usr/bin/xdpyinfo" ];then
		CHECK_DISPLAY="/usr/bin/xdpyinfo"
	else
		CHECK_DISPLAY="/usr/X11R6/bin/xdpyinfo"
	fi
	if [ -x "/usr/bin/xterm" ];then
		XTERM_CMD="/usr/bin/xterm"
	else
		XTERM_CMD="/usr/X11R6/bin/xterm"
	fi
   EXTRA=""
elif [ "${systype}" = "HP-UX" ]; then
   CHECK_DISPLAY="/usr/contrib/bin/X11/xdpyinfo"
   XTERM_CMD="/usr/bin/X11/xterm"
   EXTRA=""
elif [ "${systype}" = "AIX" ]; then
   CHECK_DISPLAY="/usr/bin/X11/xrdb"
   XTERM_CMD="/usr/bin/X11/xterm"
   EXTRA="-query"
fi

usage_short()
{
	pn=`basename $0`
   printf "\nUSAGE:"
   printf "\n    ${pn} [-display hostname] [-l] [-t] [mvp_server]"
   printf "\n    ${pn} -h"
   printf "\n    ${pn} -v"
	printf "\n\nTry '${pn} -h' for more information"
   printf "\n\n"
}

usage_long()
{
	pn=`basename $0`
   printf "\n${pn}"
   printf "\n    Starts the MarkVision Professional Client."
   printf "\n\nUSAGE:"
   printf "\n    ${pn} [-display hostname] [-l] [-t] [mvp_server]"
   printf "\n    ${pn} -h"
   printf "\n    ${pn} -v"
	printf "\n\nOPTIONS:"
	printf "\n     -display hostname"
	printf "\n        This option specifies the X server to contact.\n"
	printf "\n     -h "
	printf "\n        Display this help and exit.\n" 
	printf "\n     -l "
	printf "\n        Run ${pn} in debug mode. Debug information is logged to a file.\n"
	printf "\n     -t "
	printf "\n        Run ${pn} in debug mode. Debug information is sent to screen.\n"
	printf "\n     -v "
	printf "\n        Display version information and exit."
   printf "\n\n"
}

java_message()
{
	echo
 	echo "##############################################################"
	echo " Java Runtime (`pwd`/jre/bin/java) was not detected."
 	echo "##############################################################"
	echo " MarkVision Professional requires Java VM 1.5 or higher."
	echo
	echo " The Java Environment must be set up before we can proceed."
	echo " Run the following command as root."
   echo
   echo "  # ${MVP_HOME}/bin/findJava"
	echo
}

xterm_msg()
{
	printf '\n MVP Setup Incomplete.\n\n'
	printf ' To complete the MarkVision Professional Client Installation,\n'
	printf ' run the following command to setup the Java Environment.\n'
	printf '\n  # /usr/mvp/bin/findJava\n'
	printf '\n'
	printf '\n Note:  You will need to an administrator to run this command.\n\n'
	read p
	exit
}

while [ ! -z "${1}" ]
do
   case "${1}" in

   -display) shift 1
             OptHost="`echo "${1}" | cut -c 1`"
             if [ -z "${OptHost}" ] || [ "${OptHost}" = "-" ]; then
					 usage_short
                exit 2
             else
                echo ${1} | grep ":" >/dev/null 2>&1
                if [ "${?}" = 1 ];then
                    DISPLAY=${1}:0.0
                    export DISPLAY
                else
                    DISPLAY=${1}
                    export DISPLAY
                fi
             fi
             ;;

   -h) usage_long
       exit 1;;

   -G1) xterm_msg
		  exit
		 ;;

   -G) SHOW_XTERM="yes"
		 ;;

   -l) DEBUGARG="${DEBUGARG} ${1}"
       LOGFILE=/tmp/${LOGNAME}-mvp.log
       JAVALOGFILE=/tmp/${LOGNAME}-mvp-java.log
       FJLOGFILE=/tmp/${LOGNAME}-findJava.log
       ;;

   -t) echo
       echo "MVP is running in Debug Mode"
       DEBUG="1"
       DEBUGARG="${DEBUGARG} $1"
       ;;

   -v|-V) 
       echo
       echo "MarkVision Professional for UNIX & LINUX Systems -- Licensed version"
       echo
       echo "Version ${VERSION} (licensed)"
       echo "Revision ${BUILD_NO} 30-October-2009"
       echo
       exit 1;;

   -?) usage_short
       exit 2;;

    *) ARGS="${ARGS} ${1}"
       ;;

   esac
   shift 1
done

printf "`date`\n\n" >${LOGFILE}

#--
#-- Java Setup
#-- Make sure the Java binary exists
if [ ! -x "${JAVA_BIN}" ]; then
	java_message
	if [ "${SHOW_XTERM}" = "yes" ];then
		${XTERM_CMD} -T "MVP Setup" -n "MVP Setup" -e ${MVP_HOME}/bin/mvp -G1
	fi
	exit 1;
fi

#--
#-- Application Setup
#--
#-- This will set the CLASSPATH for mvp.
	
CLASSPATH=$MVP_HOME/mvp/lib:

if [ -n "`ls $MVP_HOME/mvp/lib/*.jar 2>/dev/null`" ]; then
	for file in ${MVP_HOME}/mvp/lib/*.jar
	do
		if [ "${file}" != "${MVP_HOME}/mvp/lib/markvision.jar" -a "${file}" != "${MVP_HOME}/mvp/lib/graniteclient.jar" ];then
			CLASSPATH=${CLASSPATH}:${file}
		fi
	done
fi
if [ -n "`ls $MVP_HOME/mvp/lib/*.zip 2>/dev/null`" ]; then
	for file in ${MVP_HOME}/mvp/lib/*.zip
	do
		CLASSPATH=${CLASSPATH}:${file}
	done
fi

#-- Now append the standard java classes to the CLASSPATH
CLASSPATH=${CLASSPATH}:${JAVA_HOME}/lib/rt.jar
CLASSPATH=${CLASSPATH}:${JAVA_HOME}/lib/classes.zip
CLASSPATH=${CLASSPATH}:${JAVA_HOME}/lib/javaplugin.jar


#-- Set any Java VM flags (Can be overriden using JVM_ARGS env var)
if [ "${JVM_ARGS}" = "" ]; then
   #-- Set minimum VM size to 64MB set maximum to 128MB
   JVM_ARGS="-ms64m -mx128m"
fi

if [ ${DEBUG} = "1" ]; then
   echo "CLASSPATH"
   echo "---------"
   echo "${CLASSPATH}"
   echo " "
   echo "Java Args"
   echo "---------"
   echo "${JVM_ARGS}"
fi


#--
#-- Launch MarkVision
#--

AWK_CMD="awk"
SED_CMD="sed"
PRINT_CMD="echo"
if [ -z "${VMDEFINES}" ];then
   if [ ! -z "${LANG}" ];then
      LANGUAGE="`${PRINT_CMD} ${LANG} | ${AWK_CMD} -F'_' '{print $1}' | ${SED_CMD} -e 's/\..*$//'`"
      REGION="`${PRINT_CMD} ${LANG} | ${AWK_CMD} -F'_' '{print $2}' | ${SED_CMD} -e 's/\..*$//'`"
      if [ "${REGION}" = "" ];then
         REGION="${LANGUAGE}"
      fi
      if [ "${systype}" = "SunOS" ]; then
         if [ "${LANGUGE}" = "c" ] || [ "${LANGUAGE}" = "C" ]; then
           LANGUAGE="en"
           REGION="US"
           COUNTRY="US"
           LANG="en_US"
           VMDEFINES="-Duser.language=${LANGUAGE} -Duser.country=${COUNTRY} -Duser.region=${REGION}"
           LC_ALL=${LANG}
           export LC_ALL
           export LANG
         else
           VMDEFINES="-Duser.language=${LANGUAGE} -Duser.region=${REGION}"
           LC_ALL=${LANG}
           export LC_ALL
         fi
      else
        VMDEFINES="-Duser.language=${LANGUAGE} -Duser.region=${REGION}"
        LC_ALL=${LANG}
        export LC_ALL
      fi
   fi
fi

TARGET="com.lexmark.markvision.MarkVision"

# Check to Make sure we can connect to the DISPLAY
if [ ! -z "${CHECK_DISPLAY}" -a -x "${CHECK_DISPLAY}" ];then
   ${CHECK_DISPLAY} ${EXTRA} </dev/null >/dev/null 2>&1
   if [ ${?} -ne 0 ]; then
      echo "MarkVision Professional can not connect to the current DISPLAY ( $DISPLAY )."
      echo "Check your DISPLAY Environment Variable and make sure you have "
      echo "permissions to connect to that DISPLAY and run mvp again."
      exit 0
   fi
fi

#
#-- Run the Java program

cd ${MVP_HOME}
if [ "${DEBUG}" = "1" ]; then
   echo ""
   echo ""
   echo "${JAVA_BIN} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} ${DEBUGARG} ${ARGS}"
   echo ""
   echo ""
   ${JAVA_BIN} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} ${DEBUGARG} ${ARGS}
else
   ${JAVA_BIN} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} ${DEBUGARG} ${ARGS} >$FJLOGFILE 2>&1
fi

exit 0
