#!/bin/sh

#--
#-- A Shell Script to start MarkVision Professional as
#-- an application.
#--
#-- Copyright (c) 1999,2007 Lexmark International, Inc.
#-- All Rights Reserved.
#--
#-- 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"
#--
#-- Requires Java 1.5 Runtime or later 
#-- Uses .markvision/java as JAVA_HOME.
#--

DEBUG="0"
LOGFILE=/dev/null
JAVALOGFILE=/dev/null
FJLOGFILE=/dev/null
SERVERNAME=""
DEBUGARG=""
pn=`basename $0`

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

usage_long()
{
   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\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\n"
}

java_message()
{
	echo
 	echo "##############################################################"
	echo " Java Runtime (`pwd`/jre/bin/java) was not detected."
 	echo "##############################################################"
	echo " MarkVision Professional requires Java 1.5 or higher."
	echo
	echo " If you believe a suitable Java version is on this system, add"
	echo " it to your PATH environment variable and run ${pn} again."
	echo
	echo " This script creates a symbolic link from `pwd`/jre"
	echo " to the Java Home Directory."
	echo 
	echo " For Example:  If Java 1.5 was installed in /usr/local/j2re1.5.0,"
	echo " create the link as shown below."
	echo 
	echo "   # ln -s /usr/local/j2re1.5.0 `pwd`/jre"
	echo 
	echo "##############################################################"

}

show_install_error()
{
	echo
	echo "Application Error!"
	echo
	echo "MarkVision Professional program files are missing."
   echo
	echo "This script (${pn}) should be located in the directory"
	echo "were you untarred the web deployment package.  You may"
	echo "create a symlink to (${pn}) from anywhere on your system."
	echo
}

pdebug()
{
	message="${1}"
	if   [ "${DEBUG}" = "1" ];then
		printf "${message}" >> ${LOGFILE}
	elif [ "${DEBUG}" = "2" ];then
      printf "${message}" >&2
	fi
}

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

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

   -t) DEBUG="2"
       DEBUGARG="${DEBUGARG} ${1}"
       ;;

   -?) usage_short
       exit 2;;

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

   esac
   shift 1
done

pdebug "#------------------------------------\n"
pdebug " MVP START\n"
pdebug " MVP DEBUG MODE = YES\n"
pdebug " DATE           = `date`\n" 

#--
#-- MarkVision Professional Setup
#--

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

#-- Verify Installation
MVP_FILE="${0}"

#-- Check To see if mvp is a symlink.
if [ -h "${MVP_FILE}" ];then
	REAL_MVP_FILE="`ls -l ${MVP_FILE} 2>/dev/null | awk '{ print $NF }'`"
	pdebug " MVP SYMLINK    = YES\n"
	pdebug " MVP FILE       = ${MVP_FILE}\n"
	pdebug " REAL MVP FILE  = ${REAL_MVP_FILE}\n"
	pdebug " LS_LINK        = `ls -l  ${MVP_FILE}`\n"
else
	REAL_MVP_FILE="${MVP_FILE}"
	pdebug " MVP SYMLINK    = NO\n"
	pdebug " REAL MVP FILE  = ${REAL_MVP_FILE}\n\n"
fi
pdebug "#------------------------------------\n"

MVP_ROOT=".markvision"
MVP_JAVA_HOME="jre"
MVP_JAVA_LIB="${MVP_JAVA_HOME}/lib"
MVP_JAVA="${MVP_JAVA_HOME}/bin/java"

pdebug " MVP SEARCH FOR INSTALL DIRECTORY\n" 
CMD_MVP_DIR="`dirname ${REAL_MVP_FILE}`"
if [ -d "${CMD_MVP_DIR}/${MVP_ROOT}/bin" ];then
	pdebug " FOUND ${CMD_MVP_DIR}/${MVP_ROOT}/bin\n" 
	pdebug " CD TO ${CMD_MVP_DIR}/${MVP_ROOT}\n"
	cd ${CMD_MVP_DIR}/${MVP_ROOT}
else
	if [ ! -f "${CMD_MVP_DIR}/findJava" ];then
		pdebug " ERROR: NOT FOUND: ${CMD_MVP_DIR}/${MVP_ROOT}/bin\n"
		pdebug " ERROR: NOT FOUND: ${CMD_MVP_DIR}/findJava\n\n"
		show_install_error
		exit 1
	else
		pdebug " FOUND ${CMD_MVP_DIR}/findJava\n" 
		pdebug " CD TO ${CMD_MVP_DIR}\n"
		pdebug " CD TO ..\n"
		cd ${CMD_MVP_DIR}
		cd ..
	fi
fi

if [ ! -d "lib" ];then
	pdebug " ERROR: DIRECTORY lib NOT FOUND\n\n"
	show_install_error
	exit 1
else
	pdebug " FOUND lib\n\n" 
fi
	
#-- Make sure necessary scripts are executable
pdebug "#------------------------------------\n"
pdebug " MVP SET PERMISSIONS\n"
for file in `echo ../mvp bin/findJava bin/mvp`
do
	if [ ! -x "${file}" ];then
		pdebug "  CHMOD a+x ${file}\n"
		chmod a+x ${file}
	else
		pdebug "  FILE ${file} is already exec.\n"
	fi
done

#--
#-- Java Setup
#--

#-- Make sure links to the Java Runtime are installed
pdebug "\n#------------------------------------\n"
pdebug " MVP SEARCH FOR JAVA\n"
if [ ! -x "${MVP_JAVA}" ];then
	echo "Setting Up Java Environment"
	./bin/findJava -l `pwd`/jre
	if [ ! -x "${MVP_JAVA}" ];then
		pdebug " ERROR FILE ${MVP_JAVA} NOT FOUND.\n\n"
		java_message
		exit 1
	else
		echo
		echo "Starting MarkVision Client .."
	fi
else
	pdebug " FOUND ${MVP_JAVA}\n\n"
fi

#--
#-- Application Setup
#--
pdebug "#------------------------------------\n"
pdebug " MVP SET CLASSPATH\n"
if [ -n "`ls lib/*.jar 2>/dev/null`" ] ; then
   for file in lib/*.jar
   do
		pdebug "   ADD ${file}\n"
      CLASSPATH=${CLASSPATH}:${file}
   done
fi

if [ -n "`ls lib/*.zip 2>/dev/null`" ] ; then
   for file in lib/*.zip
   do
		pdebug "   ADD ${file}\n"
      CLASSPATH=${CLASSPATH}:${file}
   done
fi

CLASSPATH=${CLASSPATH}:${MVP_JAVA_LIB}/rt.jar:${MVP_JAVA_LIB}/lib/classes.zip:${MVP_JAVA_LIB}/javaplugin.jar
pdebug "   ADD ${MVP_JAVA_LIB}/lib/rt.jar\n"
pdebug "   ADD ${MVP_JAVA_LIB}/lib/classes.zip\n"
pdebug "   ADD ${MVP_JAVA_LIB}/lib/javaplugin.jar\n\n"

#-- Set any JAVA VM flags
JVM_ARGS="-ms64m -mx128m"
TARGET="com.lexmark.markvision.MarkVision"

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

#-- Check to Make sure we can connect to the DISPLAY
if   [ $systype = "SunOS" ]; then
   CHECK_DISPLAY="/usr/openwin/bin/xdpyinfo"
   EXTRA=""
elif [ $systype = "UnixWare" ]; then
   CHECK_DISPLAY="/usr/X/bin/xdpyinfo"
   EXTRA=""
elif [ $systype = "Linux" ]; then
	if [ -x "/usr/bin/xdpyinfo" ];then
   	CHECK_DISPLAY="/usr/bin/xdpyinfo"
	else
   	CHECK_DISPLAY="/usr/X11R6/bin/xdpyinfo"
	fi
   EXTRA=""
elif [ $systype = "HP-UX" ]; then
   CHECK_DISPLAY="/usr/contrib/bin/X11/xdpyinfo"
   EXTRA=""
elif [ $systype = "IRIX" ] || [ $systype = "OSF1" ] || [ $systype = "SCO_SV" ] || [ $systype = "UNIX_SV" ]; then
   CHECK_DISPLAY="/usr/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 Java program

pdebug "#------------------------------------\n"
pdebug " MVP CLIENT START\n"
pdebug " ${MVP_JAVA} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} ${DEBUGARG} ${ARGS}\n\n"
pdebug " LAUNCHING .. Please Wait\n"
if [ "${DEBUG}" = "2" ]; then
   ${MVP_JAVA} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} ${DEBUGARG} ${ARGS}
else
   ${MVP_JAVA} ${JVM_ARGS} -classpath ${CLASSPATH} ${VMDEFINES} ${TARGET} ${DEBUGARG} ${ARGS} 2>$FJLOGFILE 1>>$FJLOGFILE
fi
