#!/bin/sh

#--
#-- A Shell Script to start MarkVision Professional 
#-- Status Client.
#-- 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"
#--
#-- (c) 2005 Lexmark International, Inc.
#--

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

DEBUG="0"

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

usage_short()
{
	pn=`basename $0`
   printf "\nUSAGE:"
   printf "\n    ${pn} -d device -s server [-u user] [-p password] [-t]"
   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    Displays a status view of a specific printer."
   printf "\n\nUSAGE:"
   printf "\n    ${pn} -d device -s server [-u user] [-p password] [-a admin] [-t]"
   printf "\n    ${pn} -h"
   printf "\n    ${pn} -v"
	printf "\n\nOPTIONS:"
	printf "\n     -d device"
	printf "\n        Specify a Printer's hostname or IP Address.\n"
	printf "\n     -s server"
	printf "\n        Specify a MVP Server hostname or IP Address."
	printf "\n        This option defaults to localhost if not specified.\n"
	printf "\n     -u user"
	printf "\n        Specify a MVP Username.\n"
	printf "\n     -p password"
	printf "\n        Specify the MVP User's Password."
	printf "\n        Accounts without passwords do not need this option.\n"
	printf "\n     -h "
	printf "\n        Display this help and exit.\n" 
	printf "\n     -t "
	printf "\n        Run ${pn} in debug mode.\n"
	printf "\n     -v "
	printf "\n        Display version information and exit."
   printf "\n\n"
}

java_message()
{
	echo "Java Runtime was not detected in ${JAVA_HOME}."
	echo
	echo "The Java Environment must be setup before we can procede."
	echo "Run the following command as root."
   echo
   echo "  # ${MVP_HOME}/bin/findJava"
	echo
}

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

   -p) shift 1
            PASSWD="`echo "${1}" | cut -c 1`"
            if [ ! -z "${PASSWD}" ];then
               if [ "${PASSWD}" != "-" -a "${PASSWD}" != " " ]; then
			ARGS="${ARGS} -p ${1}"
               fi
            fi
	    ;;
				

   -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
             ;;

   -t) echo
       echo "MVP Status Client is running in Debug Mode."
       DEBUG="1"
       ;;

   -v|-V) 
       echo
       echo "MarkVision Professional Status Client for UNIX & LINUX Systems -- Licensed version"
       echo
       echo "Version ${VERSION} (licensed)"
       echo "Build ${BUILD_NO} `date '+%B %Y'`"
       echo
       exit 1;;

   -h) usage_long
       exit 2;;

	 -s) 	shift 1
			SERVER="${1}"			
			;;
	 -d) 	shift 1
			DEVICE="${1}"			
			;;
    *) 
			ARGS="${ARGS} ${1}"
       	;;
   esac
	if [ ! -z "${1}" ];then
   		shift 1
	fi
done

if [ -z "${DEVICE}" ];then
	usage_short
	exit 2
fi
if [ -z "${SERVER}" ];then
	SERVER="localhost"
fi

#--
#-- MarkVision Status Client Setup
#--

#-- Setting the system type
systype=`uname -a | (read p1 p2 p3 p4 p5 p6 p7 p8 p9; echo ${p1})`

#--
#-- Java Setup
#--

#-- Make sure links to the Java Runtime are installed
if [ ! -s "${JAVA_HOME}" ]; then
	java_message
   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
		CLASSPATH=${CLASSPATH}:${file}
	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

#-- Make sure the Java binary exists
if [ ! -x "${JAVA_BIN}" ]; then
	java_message
	exit 1;
fi

#--
#-- Launch MarkVision Status Client
#--

AWK_CMD="awk"
SED_CMD="sed"
PRINT_CMD="echo"
#-- VMDEFINES variable. 
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
      VMDEFINES="-Duser.language=${LANGUAGE} -Duser.region=${REGION}"
      LC_ALL=${LANG}
      export LC_ALL
   fi
fi

TARGET="com.lexmark.markvision.statusclient.StatusClient"

#-- Check to Make sure we can connect to the DISPLAY

if   [ "${systype}" = "SunOS" ]; then
   CHECK_DISPLAY="/usr/openwin/bin/xdpyinfo"
   EXTRA=""
elif [ "${systype}" = "Linux" ]; then
   CHECK_DISPLAY="/usr/X11R6/bin/xdpyinfo"
   EXTRA=""
elif [ "${systype}" = "HP-UX" ]; then
   CHECK_DISPLAY="/usr/contrib/bin/X11/xdpyinfo"
   EXTRA=""
elif [ "${systype}" = "AIX" ]; then
   CHECK_DISPLAY="/usr/bin/X11/xrdb"
   EXTRA="-query"
fi

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 Status Client 

cd ${MVP_HOME}
if [ "${DEBUG}" = "1" ]; then
   echo ""
   echo ""
   echo "${JAVA_BIN} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} -d ${DEVICE} -s ${SERVER} ${ARGS}"
   echo ""
   echo ""
   ${JAVA_BIN} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} -d ${DEVICE} -s ${SERVER} ${ARGS}
else
   ${JAVA_BIN} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} -d ${DEVICE} -s ${SERVER} ${ARGS} >/dev/null 2>&1
fi

exit 0
