

#-- Application Global Shared Script 

#-- We require all SCRIPT OUTPUT to be ENGLISH
#-- Remember the original locale.

APPLICATION_LANG=${LANG}

#-- Set locale to English for the remainder of this script.
LANG=C
LC_ALL=C
export LANG
export LC_ALL

#--------------------------------------------------------
#-- This function verifies the existence of all system 
#-- commands used by this application.  If a specfic  
#-- command is not found, feel free to modify the 
#-- following variables located int the System Command
#-- section using absolute paths.
#--
#-- NOTE:  ALWAYS USE ABSOLUTE PATHS
#--


initialize ()
{
   #-- user type ( admin | user )
   user_type="${1}"

   #-------------------------
   #-- BEGIN: System Commands
   #-------------------------
   CAT_CMD="/bin/cat"
   PRINT_CMD="/bin/echo"
   PRINTF_CMD="/usr/bin/printf"
   TAIL_CMD="/usr/bin/tail"
	MORE_CMD="/bin/more"

   SED_CMD="/bin/sed"
   ED_CMD="/bin/ed"
   TR_CMD="/usr/bin/tr"

   GREP_CMD="/bin/grep"
   EGREP_CMD="/bin/egrep"

   LS_CMD="/bin/ls"
   FIND_CMD="/usr/bin/find"
 
   CP_CMD="/bin/cp"
   RM_CMD="/bin/rm"
   MOVE_CMD="/bin/mv"
   LN_CMD="/bin/ln"
   MKDIR_CMD="/bin/mkdir"
   RMDIR_CMD="/bin/rmdir"
   
   CHMOD_CMD="/bin/chmod"
   CHOWN_CMD="/bin/chown"
   CHGRP_CMD="/bin/chgrp"

   if [ -x "/usr/bin/id" ];then   ID_CMD="/usr/bin/id"         ; else ID_CMD="/bin/id"                 ; fi
   if [ -x "/usr/bin/cut" ];then  CUT_CMD="/usr/bin/cut"       ; else CUT_CMD="/bin/cut"               ; fi
   if [ -x "/bin/awk" ];then      AWK_CMD="/bin/awk"           ; else AWK_CMD="/usr/bin/awk"           ; fi
   if [ -x "/bin/basename" ];then BASENAME_CMD="/bin/basename" ; else BASENAME_CMD="/usr/bin/basename" ; fi
   if [ -x "/bin/tar" ];then      TAR_CMD="/bin/tar"           ; else TAR_CMD="/usr/bin/tar"           ; fi
	if [ -x "/usr/bin/uncompress" ];then 
		UNCOMPRESS_CMD="/usr/bin/uncompress" ; 
	elif [ -x "/bin/uncompress" ];then 
		UNCOMPRESS_CMD="/bin/uncompress"; 
	elif [ -x "/usr/bin/gunzip" ];then 
		UNCOMPRESS_CMD="/usr/bin/gunzip"; 
	elif [ -x "/bin/gunzip" ];then 
		UNCOMPRESS_CMD="/bin/gunzip"; 
	else 
		UNCOMPRESS_CMD="/bin/uncompress"; 
	fi

   UNAME_CMD="/bin/uname"
   HOSTNAME_CMD="/bin/hostname"  	
   PS_CMD="/bin/ps"  			
   if [ -x "/usr/gnu/bin/whoami" ];then  
           WHOAMI_CMD="/usr/gnu/bin/whoami" ;     
   else
           if [ -x "/usr/bin/whoami" ];then
                      WHOAMI_CMD="/usr/bin/whoami";     
           else
                      WHOAMI_CMD="/usr/bin/whoami";
           fi
   fi


   PWD_CMD="/bin/pwd"
   DIRNAME_CMD="/usr/bin/dirname" 

   EXPR_CMD="/usr/bin/expr"
   WC_CMD="/usr/bin/wc"
   UNIQ_CMD="/usr/bin/uniq"

   DATE_CMD="/bin/date"
   SLEEP_CMD="/bin/sleep"
   TOUCH_CMD="/bin/touch"
   SWITCH_PRT_CMD="/usr/sbin/switch.prt"
   XDPYINFO_CMD="/usr/X11R6/bin/xdpyinfo" 		

   if [ ! -x "${XDPYINFO_CMD}" ] && [ -x "/usr/bin/xdpyinfo" ];then
   	XDPYINFO_CMD="/usr/bin/xdpyinfo"
   fi

   LPR_CMD=/usr/bin/lpr
   LPC_CMD=/usr/sbin/lpc

	if [ -f "/usr/sbin/cupsdisable" ];then
		DISABLE_CMD=/usr/sbin/cupsdisable
	else
		DISABLE_CMD=/usr/bin/disable
	fi
	if [ -f "/usr/sbin/cupsenable" ];then
		ENABLE_CMD=/usr/sbin/cupsenable
	else
		ENABLE_CMD=/usr/bin/enable
	fi

   ACCEPT_CMD="/usr/sbin/accept" 	        
   REJECT_CMD="/usr/sbin/reject"		

   LPADMIN_CMD="/usr/sbin/lpadmin"          
   CUPSD_CMD="/usr/sbin/cupsd"
   if [ -f /etc/init.d/cupsys ];then
      CUPSD_START_CMD="/etc/init.d/cupsys start"
   else
      CUPSD_START_CMD="/etc/init.d/cups start"
   fi

   BC_CMD="/usr/bin/bc"
   LPD_CMD="/usr/sbin/lpd"			
   #-------------------------
   #-- END: System Commands
   #-------------------------

   #--
   #-- DO NOT EDIT PAST THIS
   #--

   #-- program variables
   APPLICATION_NAME="Print Drivers"  	
   PRODUCT_NAME="genprint" 					
   PRODUCT_LINK="/usr/genprint"			
   PRODUCT_ABREV="GVPD"			
   PRINTER_DIR="${PRODUCT_LINK}/etc/printers"

   SYS_SPOOL_DIR="/var/spool"			
   ROOT_SPOOL_DIR=${SYS_SPOOL_DIR}/printer/unix_prt_drivers
   VIR_DEVICES_DIR=${ROOT_SPOOL_DIR}/vir_devices
   VIR_QUEUES_DIR=${ROOT_SPOOL_DIR}/vir_queues

   CLASSES_DIR=SYS_CLASSES_DIR			
   PRINTCAP_FILE="/etc/printcap"
   PRINTCAP_LOCAL_FILE="/etc/printcap.local"

   PRINT_GUI="genlp"

   BPP_PARALLEL_DEVS="UNAVAILABLE"	
   PARALLEL_DEVS="/dev/lp0:/dev/lp1:/dev/lp2"		
   SERIAL_DEVS="/dev/ttyS0:/dev/ttyS1"			
   USB_DEVS="/dev/usblp0:/dev/usblp1:/dev/usb/lp0:/dev/usb/lp1"				

   ERROR_LOG="/tmp/${PRODUCT_NAME}.errorlog"
   JAVA_CMD="${PRODUCT_LINK}/jre/bin/java"

   if [ -f "${EXTRA_DIR2}/.linpus" -a -f "/usr/bin/lp-cups" -a -f "/usr/bin/lpstat-cups" ];then
      LP_CMD=/usr/bin/lp-cups
      LPSTAT_CMD=/usr/bin/lpstat-cups
   else
      LP_CMD=/usr/bin/lp
      LPSTAT_CMD=/usr/bin/lpstat
   fi

   #-- Permission variables
	SPOOL_DIR_PERMS="0755"
	SPOOL_FILE_PERMS="0644"

   #-- Set Print Subsystem Type
   APP_PROPS_FILE="${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/props/app.properties"
   if [ -f "${APP_PROPS_FILE}" ];then
      ${CAT_CMD} ${APP_PROPS_FILE} | ${GREP_CMD} "app.prt_subsystem=bsd" >/dev/null 2>&1
      if [ ${?} -eq 0 ];then
         PRT_SUBSYSTEM="BSD"
      else
         PRT_SUBSYSTEM="SYSTEM_V"
      fi
   else
   	user_type="user"
      PRT_SUBSYSTEM="SYSTEM_V"
   fi

   #-- Add all system commands to SYS_COMMANDS that need to 
   #-- checked
   SYS_COMMANDS="${CAT_CMD} ${PRINT_CMD} ${PRINTF_CMD} ${TAIL_CMD} ${AWK_CMD} ${SED_CMD} ${CUT_CMD} ${TR_CMD} ${GREP_CMD} ${EGREP_CMD} ${LS_CMD} ${FIND_CMD} ${CP_CMD} ${RM_CMD} ${MOVE_CMD} ${LN_CMD} ${MKDIR_CMD} ${RMDIR_CMD} ${CHMOD_CMD} ${CHOWN_CMD} ${CHGRP_CMD} ${ID_CMD} ${UNAME_CMD} ${HOSTNAME_CMD} ${PS_CMD} ${PWD_CMD} ${DIRNAME_CMD} ${BASENAME_CMD} ${EXPR_CMD} ${DATE_CMD} ${SLEEP_CMD} ${TOUCH_CMD} ${WHOAMI_CMD}"

	#-- Lets skip checking for admin files until setup has been run.
	if [ ! -f "${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/props/gen.properties" ];then
		user_type="user"
	fi

   #-- Add privileged commands if user_type is admin. 
   if [ "${PRT_SUBSYSTEM}" = "BSD" ];then 
      if [ "${user_type}" = "admin" ];then
         SYS_COMMANDS="${SYS_COMMANDS} ${LPC_CMD} ${LPD_CMD} ${BC_CMD} ${ED_CMD}"
      else
         SYS_COMMANDS="${SYS_COMMANDS} ${LPC_CMD} ${BC_CMD} ${ED_CMD}"
      fi
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then 
      if [ "${user_type}" = "admin" ];then
         SYS_COMMANDS="${SYS_COMMANDS} ${LPADMIN_CMD} ${ACCEPT_CMD} ${REJECT_CMD} ${ENABLE_CMD} ${DISABLE_CMD}"
      fi
    fi
  
   FLAG="0"
   for i in ${SYS_COMMANDS}
   do
     if [ ! -x "${i}" ]; then
        if [ "${FLAG}" = "0" ]; then
           ${PRINT_CMD}
           ${PRINT_CMD} "This script uses a set of system commands which may be"
           ${PRINT_CMD} "in a different location on your system. The section on"
           ${PRINT_CMD} "\"System Commands\" specifies the location of these commands"
           ${PRINT_CMD} 
           ${PRINT_CMD} "The following system command(s) were not found:"
           ${PRINT_CMD} 
           FLAG="1"
        fi
        ${PRINT_CMD} "   File missing  --  $i"
     fi
   done

   if [ "${FLAG}" = "1" ]; then
      ${PRINT_CMD} 
      ${PRINT_CMD} "HOW TO FIX"
      ${PRINT_CMD} "----------"
      ${PRINT_CMD} "This problem can be easily corrected by manually editing the"
      ${PRINT_CMD} "script [ ${PRODUCT_LINK}/bin/.scripts/shared ] and modify."
      ${PRINT_CMD} "the System Commands section." 
      exit 1;
   fi
 
   #- All files found, continue

	#-- 
	: ${PDUSER=`${WHOAMI_CMD}`}}
	: ${PDHOME=${HOME}}
	: ${PDHOME=/tmp}

   return
}

#---------------------------------------------------
#-- sets dynamic classpath
#---------------------------------------------------
set_classpath()
{
   #-- Set CLASSPATH
   JPRINT_JAR="${PRODUCT_LINK}/lib/main.jar"
   PRINTERS_JAR="${PRODUCT_LINK}/lib/printers.jar"
   JDOM_JAR="${PRODUCT_LINK}/lib/jdom.jar"
   XERCES_JAR="${PRODUCT_LINK}/lib/xerces.jar"
   EXTRA_DIR="${PRODUCT_LINK}/etc"
   EXTRA_DIR2="${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}"
	for jar in `${LS_CMD} ${EXTRA_DIR2}/lib/*.jar 2>/dev/null`
	do
		case "`${BASENAME_CMD} ${jar}`" in
			fLMACP_DRV.jar|fGNACP_DRV.jar ) continue ;;
			fLMACQ_DRV.jar|fGNACQ_DRV.jar ) continue ;;
			fLMACE_DRV.jar|fGNACE_DRV.jar ) continue ;;
			fLMACJ_DRV.jar|fGNACJ_DRV.jar ) continue ;;
			fLMACK_DRV.jar|fGNACK_DRV.jar ) continue ;;
			fLMACO_DRV.jar|fGNACO_DRV.jar ) continue ;;
			gLMACA_DRV.jar|gIIACA_DRV.jar ) continue ;;
			gLMACB_DRV.jar|gIIACB_DRV.jar ) continue ;;
			gLMACC_DRV.jar|gIIACC_DRV.jar ) continue ;;
			hLMABS_DRV.jar|hIIABS_DRV.jar ) continue ;;
			hLMABT_DRV.jar|hIIABT_DRV.jar ) continue ;;
			iLMABF_DRV.jar|iIIABF_DRV.jar ) continue ;;
			jLMABD_DRV.jar|jIIABD_DRV.jar ) continue ;;
			iLMABE_DRV.jar|iIIABE_DRV.jar ) continue ;;
			jLMABC_DRV.jar|jIIABC_DRV.jar ) continue ;;
			kLMABB_DRV.jar ) continue ;;
		esac
		if [ -z "${EXTRA_JARS}" ];then
			EXTRA_JARS="${jar}"
		else
			EXTRA_JARS="${EXTRA_JARS}:${jar}"
		fi
	done
   CLASSPATH=${EXTRA_DIR}:${EXTRA_DIR2}:${EXTRA_JARS}:${JPRINT_JAR}:${PRINTERS_JAR}:${JDOM_JAR}:${XERCES_JAR}
}


is_true ()
{
    #-- return 0 if true otherwise return 1
    [ "$1" = "y" -o "$1" = "Y" -o "$1" = "yes" -o "$1" = "YES" ]
}  


#---------------------------------------------------
#-- is_cmd_success
#--
#--  This function is called to check if a command completed successfully
#--  and processes the results accordingly.
#--
#--  Syntax:
#--  is_cmd_success <exit_code> <success> <should_exit> <failed_msg> <command> <exit_with>
#--
#--  NOTE: The <exit_with> argument only matters if the <should_exit> = "yes",
#--        otherwise it can be set to anything.
#--
is_cmd_success ()
{
   exit_code="${1}"
   success="${2}"
   should_exit="${3}"
   failed_msg="${4}"
   command="${5}"
   exit_with="${6}"

   if [ "${exit_code}" != "${success}" ]; then
      ${PRINTF_CMD} " COMMAND FAILED ( ${command} )\n"
      if [ ! -z "${failed_msg}" ]; then
         ${PRINTF_CMD} "${failed_msg}\n"
      fi
      if [ "${should_exit}" = "yes" ]; then
         exit ${exit_with};
      fi
   fi
}


#---------------------------------------------------
#-- check_for_a_file
#--
#-- This function checks for the existance of a file.
#--
#-- Syntax:
#-- check_for_a_file <file_name>
#--
#--  Return 0  -  File Found
#--  Return 1  -  File Not Found
#--
check_for_a_file ()
{
   filename="${1}"
   return_code=1

   pdebug "Searching for (${filename}) ..."
   if [ -f "${filename}" ];then
      pdebug " found it.\n"
      return_code=0
   else  
      pdebug " not found.\n"
      return_code=1
   fi    

   return ${return_code}
}


#---------------------------------------------------
#-- bsd_check_queue_status
#--
#-- This function checks to see if a specific queue 
#-- is a system queue.  We define a system queue as
#-- any print queue that is known by the print
#-- subsystem.
#--  
#-- If the specific queue is a known system queue,
#-- we will send the current status to standard out.
#--
#-- Syntax:
#-- check_queue_status <queue_name>
#--
#--  Return 0  --   print queue
#--            --   Status is sent to standard out
#--  Return 1  --   unknown print queue
#--
bsd_check_queue_status()
{
   lqName="${1}"
   return_code=1

   lp_status_output=`${LPC_CMD} status ${lqName} 2>&1` 
   if [ "${?}" != "0" ];then
      ${PRINT_CMD} "ERROR: ${LPC_CMD} Failed" 
      ${PRINT_CMD} "[ ${LPC_CMD} status ${lqName} ]"
      return ${return_code}
   fi

   #-- Check to See queue exists.
   ${PRINTF_CMD} "${lp_status_output}\n" | \
      ${EGREP_CMD} -e "non-existent|unknown printer|not in printcap|does not exist|no spool directory|Invalid destination name in list" 1>/dev/null 2>&1
   if [ "$?" = "0" ]; then
      pdebug "${lp_status_output}\n"
      pdebug "System Queue [ ${lqName} ] does not exist.\n"
      return_code=1
   else
      pdebug "${lp_status_output}\n"
      pdebug "System Queue [ ${lqName} ] found..\n"
   
      # LPRng and LPR lpc status commands return different output.
      # Lets check to see which one we caught.
      ${PRINTF_CMD} "${lp_status_output}\n" | ${GREP_CMD} "Printing Spooling" 1>/dev/null 2>&1
      if [ "$?" = "0" ]; then
         pdebug "LPRng style LPC command.\n"
         ${PRINTF_CMD} "${lp_status_output}\n" | \
     		${AWK_CMD} ' $1 ~ /'"$lqName"'/ { printf $2 ":"
     					   if ( $3 == "enabled" ) print "accepting"
     					   else printf "rejecting\n"  } '
      else
         pdebug "LPR style LPC command.\n"
         ${PRINTF_CMD} "${lp_status_output}\n" | \
     		${AWK_CMD} ' $1 ~ /printing/ && $2 ~ /is/ { pState=$3 }
                                  $1 ~ /queuing/ && $2 ~ /is/ { if ( $3 == "enabled" ) qState="accepting" 
     					   else qState="rejecting"  }
                                  END { printf("%s:%s",pState,qState) }' 
      fi
      pdebug "lp_status_output:\n${lp_status_output}\n"
      return_code=0
   fi
   return ${return_code}
}

#---------------------------------------------------
#-- sysv_check_queue_status
#--
#-- This function checks to see if a specific queue
#-- is a system queue.  We define a system queue as
#-- any print queue that is known by the print
#-- subsystem.
#--
#-- If the specific queue is a known system queue,
#-- we will send the current status to standard out.
#--
#-- Syntax:
#-- check_queue_status <queue_name>
#--
#--  Return 0  --   print queue
#--            --   Status is sent to standard out
#--  Return 1  --   unknown print queue
#--
sysv_check_queue_status()
{
   lqName="${1}"
   return_code=1
   UNKNOWN_STRINGS="not-found|non-existent|unknown printer|does not exist|Unknown destination|Invalid destination name in list"

   # Check to See queue exists.
   lp_request_output=`${LPSTAT_CMD} -a${lqName} 2>&1` 
   ${PRINTF_CMD} "${lp_request_output}\n" | ${EGREP_CMD} -e "${UNKNOWN_STRINGS}" 1>/dev/null 2>&1
   if [ "${?}" = "0" ] || [ -z "${lp_request_output}" ];then
      pdebug "${lp_request_output}\n"
      pdebug "System Queue [ ${lqName} ] does not exist.\n" 
      return_code=1
   else 
      lp_status_output=`${LPSTAT_CMD} -p${lqName} 2>&1` 
      ${PRINTF_CMD} "${lp_status_output}\n" | ${EGREP_CMD} -e "${UNKNOWN_STRINGS}" 1>/dev/null 2>&1
      if [ "${?}" = "0" ] || [ -z "${lp_request_output}" ];then
         pdebug "${lp_status_output}\n" 
         pdebug "System Class [ ${lqName} ] found..\n"
         return_code=2
      else
         pdebug "${lp_status_output}\n" 
         pdebug "System Queue [ ${lqName} ] found..\n"
         ${PRINTF_CMD} "${lp_status_output}\n" | ${GREP_CMD} "disabled" 1>/dev/null 2>&1
         if [ "${?}" = "0" ]; then
            pStatus="disabled"
         else 
            pStatus="enabled"
         fi
         pdebug "${lp_request_output}\n" 
         ${PRINTF_CMD} "${lp_request_output}\n" | ${GREP_CMD} "not accepting requests" >/dev/null 2>&1
         if [ "${?}" = "0" ]; then
            qState="rejecting"
         else 
            qState="accepting"
         fi
         ${PRINT_CMD} "${pStatus}:${qState}"
         return_code=0
      fi
   fi
   return ${return_code}
}

check_queue_status()
{
   if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
        bsd_check_queue_status ${*}
        return ${?}
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
        sysv_check_queue_status ${*}
        return ${?}
   fi
}

#-----------------------------------------------
#--  check_for_system_queue
#--
#--  This function checks for a system queue.
#--
#--  Syntax:
#--  check_for_system_queue <queue_name>
#--
#--  RETURN 0  --  Queue Found
#--  RETURN 1  --  Queue Not Found
#--
check_for_system_queue()
{
   lqName=${1}

   check_queue_status ${lqName} >/dev/null 2>&1
   if [ "${?}" != "0" ];then
      pdebug "System Queue [ ${lqName} ] was not found.\nReturn 1\n"
      return 1
   else
      pdebug "System Queue [ ${lqName} ] was found.\nReturn 0\n"
      return 0
   fi
}


#---------------------------------------------------
#-- get_queue_setting 
#--  
#-- This function checks to see if a specific queue 
#-- is a virtual queue.  We define a virtual queue
#-- as any print queue that is known by the print
#-- subsystem and was created by this application.
#--  
#-- If the specific queue is a known by this application,
#-- we will send the current settings to standard out.
#--
#-- Syntax:
#-- get_queue_setting <queue_name>
#--
#--
#-- Return 0  --  Application Queue
#--           --  Setting are sent to standard out
#-- Return 1  --  Not an application queue
#-- Exit 1    --  status command fails 
get_queue_settings()
{
   lqName_file="${1}"
   return_code=1

   if [ ! -f ${lqName_file} ]; then
      pdebug "Queue [ ${lqName_file} ] does not exist.\n"
   else
      pdebug "Queue [ ${lqName_file} ] found.\n"
      ${AWK_CMD} -F"=" '{
                        if      ( $1 == "printer_desc" ) desc=$2
                        else if ( $1 == "printer_type" ) ptype=$2
                        else if ( $1 == "ds_list" ) lang=$2
                        else if ( $1 == "device_name" ) dname=$2
                        else if ( $1 == "pdd" ) fname=$2
                        else if ( $1 == "queue_name" ) queue=$2
			if ( desc  != "" && ptype != "" && lang  != "" && dname != "" && fname != "" && queue != "" ) exit 
                  }
                  END {
                      printf("%s:%s:%s:%s:%s:%s",queue,ptype,desc,dname,lang,fname)
                      }' ${lqName_file}
      return_code=0
   fi
   pdebug "Return Code = $return_code\n"
   return ${return_code}
}


#---------------------------------------------------
#-- get_device_setting 
#--
#-- This function checks to see if the virtual
#-- device exists.
#--  
#-- If the specific device is a known virtual device,
#-- we will send the current settings to standard out.
#--
#-- Syntax:
#-- get_device_setting <device_names>
#--
#-- Return 0  --  Known virtual device
#--           --  Setting are sent to standard out
#-- Return 1  --  Not a virtual device
#--
get_device_settings()
{
     devices="$*" 
     return_code="1"

     for device in ${devices}
     do
         #-- Check to make sure we can read the file
         if [ -r "${VIR_DEVICES_DIR}/${device}" ];then
            pdebug "[ Reading ${device} ].\n"
            ${AWK_CMD} -F"=" '{
                         if ( $1 == "device_name"      ) dname=$2
                    else if ( $1 == "device_desc"      ) desc=$2
                    else if ( $1 == "transport_type"   ) dtype=$2
                    else if ( $1 == "network_host"     ) dextra=$2
                    else if ( $1 == "parallel_device"  ) dextra=$2
                    else if ( $1 == "other_parallel_device" ) dextra_other=$2
                    else if ( $1 == "usb_device"       ) dextra=$2
                    else if ( $1 == "other_usb_device" ) dextra_other=$2
                    else if ( $1 == "serial_device"    ) dextra=$2
                    else if ( $1 == "other_serial_device" ) dextra_other=$2
                }
                END {
                    if ( dname != "" && desc != "" && dtype != "" && dextra != "" )
                    {
                       if ( dextra == "other" ) dextra=dextra_other
                       if ( "true" ~ /'"$long_listing"'/ ) {
                          printf("Device Name : %s\nDescription : %s\nDevice Type : %s\nConnection  : %s\n\n",dname,desc,dtype,dextra)
                       }
                       else 
                       {
                          printf("%s#%s#%s#%s\n",dname,dtype,desc,dextra)
                       }
                    }
                    else
                    {
                       if ( dname == "" )  dname=ldevice
                       if ( desc  == ""  ) desc="unknown"
                       if ( dtype == "" )  dtype="unknown"
                       if ( dextra == "" ) dextra="unknown"
                       if ( ldebug == 1  ) {
                          printf("ERROR: Virutal device [ %s ] is corrupt!\n",ldevice) | "cat 1>&2"
                          printf("%s:%s:%s:%s\n",dname,dtype,desc,dextra) | "cat 1>&2"
                       }
                       exit 2
                    }
               }' ldevice=${device} ldebug=${DEBUG} ${VIR_DEVICES_DIR}/${device}
               return_code="${?}"
         else
            pdebug "ERROR: Virtual Device [ ${device} ] is not readable.\n"
         fi
     done
     pdebug "RETURN CODE = $return_code\n"
     return ${return_code} 
}


#---------------------------------------------------
#-- reject_queue 
#-- 
#-- This function changes a print queue to rejecting
#-- print requests
#--  
#-- Syntax:
#-- reject_queue <queue_name>
#--  
#-- Return 0  --  SUCCESS
#-- Return 1  --  FAILED
#--
reject_queue()
{
   lqName="${1}"

   if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
        lcommand="${LPC_CMD} disable"
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
        lcommand="${REJECT_CMD}"
   fi

   pdebug "\n${lcommand} ${lqName}\n"
   cmd_out=`${lcommand} ${lqName} 2>&1`

   pdebug "$cmd_out\n"

   #-- Check the status after running command
   status_out=`check_queue_status ${lqName} 2>&1`
   pdebug "\nQueue Status: $status_out\n"
   ${PRINT_CMD} ${status_out} | ${GREP_CMD} ":rejecting" >/dev/null 2>&1
   if [ "$?" != "0" ];then
      ${PRINT_CMD} ${cmd_out}
      ${PRINT_CMD} "Rejecting print requests for printer queue ${lqName} has failed."
      return 1
   else
      ${PRINT_CMD} "Printer queue ${lqName} is now rejecting print requests."
      return 0
   fi
}


#---------------------------------------------------
#-- accept_queue 
#--  
#-- This function changes a print queue to accepting
#-- print requests
#--
#-- Syntax:
#-- accept_queue <queue_name>
#--  
#-- Return 0  --  SUCCESS
#-- Return 1  --  FAILED
#--
accept_queue()
{
   lqName="${1}"

   if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
        lcommand="${LPC_CMD} enable"
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
        lcommand="${ACCEPT_CMD}"
   fi

   pdebug "\n${lcommand} ${lqName}\n"
   cmd_out=`${lcommand} ${lqName} 2>&1`

   pdebug "$cmd_out\n"


   #-- Check the status after running command
   #-- This verifies the command completed successfully
   status_out=`check_queue_status ${lqName} 2>&1`
   pdebug "\nQueue Status: $status_out\n"
   ${PRINT_CMD} ${status_out} | ${GREP_CMD} ":accepting" >/dev/null 2>&1
   if [ "$?" != "0" ];then
      ${PRINT_CMD} ${cmd_out}
      ${PRINT_CMD} "Accepting print requests for printer queue ${lqName} has failed."
      return 1
   else
      ${PRINT_CMD} "Printer queue ${lqName} is now accepting print requests."
      return 0
   fi
}


#---------------------------------------------------
#-- enable_queue
#--  
#-- This function enables a print queue
#--
#--  Exit 0  : SUCCESS
#--  Exit 1  : FAILED
#--
#-- Syntax:
#-- enable_queue <queue_name>
#--  
#-- Return 0  --  SUCCESS
#-- Return 1  --  FAILED
#--
enable_queue()
{
   lqName="${1}"

   if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
        lcommand="${LPC_CMD} start"
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
        lcommand="${ENABLE_CMD}"
   fi

   pdebug "\n${lcommand} ${lqName}\n"
   cmd_out=`${lcommand} ${lqName} 2>&1`

   pdebug "$cmd_out\n"

   #-- Check the status after running command
   #-- This verifies the command completed successfully
   status_out=`check_queue_status ${lqName} 2>&1`
   pdebug "\nQueue Status: $status_out\n"
   ${PRINT_CMD} ${status_out} | ${GREP_CMD} "enabled:" >/dev/null 2>&1
   if [ "$?" != "0" ];then
      ${PRINT_CMD} ${cmd_out}
      return 1
   else
	  ${PRINT_CMD} "Printer queue ${lqName} is now enabled."
      return 0
   fi
}


#---------------------------------------------------
#-- disable_queue
#--  
#-- This function disables a print queue
#--
#-- Syntax:
#-- disable_queue <queue_name>
#--  
#-- Return 0  --  SUCCESS
#-- Return 1  --  FAILED
#--
disable_queue()
{
   lqName="${1}"

   if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
        lcommand="${LPC_CMD} stop"
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
        lcommand="${DISABLE_CMD}"
   fi

   pdebug "\n${lcommand} ${lqName}\n"
   cmd_out=`${lcommand} ${lqName} 2>&1`

   pdebug "$cmd_out\n"

   #-- Check the status after running command
   #-- This verifies the command completed successfully
   status_out=`check_queue_status ${lqName} 2>&1`
   pdebug "\nQueue Status: $status_out\n"
   ${PRINT_CMD} ${status_out} | ${GREP_CMD} "disabled:" >/dev/null 2>&1
   if [ "$?" != "0" ];then
      ${PRINT_CMD} ${cmd_out}
      return 1
   else
	  ${PRINT_CMD} "Printer queue ${lqName} is now disabled."
      return 0
   fi
}


#---------------------------------------------------
#--  build_print_command
#--
#--  This function builds an appropriate lp print 
#--  command.
#--
#--  Syntax:
#--  build_print_command queue args
#--
#--  Example:
#--  build_print_command queue ps_duplex=on ps_paper_tray=tray2
#--
#--  Return 0  --  Success
#--            --  Print the lp command to standard out
#--  Return 1  --  Cancel
#--
build_print_command()
{
   lqueue_name="${1}"
   shift 1
   largs="${*}"

   if [ -z "${lqueue_name}" ];then
      ${PRINTF_CMD} "Queue Name missing.\n"
      pdebug "RETURN CODE = 1\n"
      return 1
   fi

   #-- Check to see if we should cancel the command
   ${PRINTF_CMD} "${largs}" | ${GREP_CMD} -i "CANCEL" >/dev/null 2>&1 
   if [ "${?}" = "0" ];then
      ${PRINTF_CMD} "\nPrinting cancelled.\n"
      pdebug "RETURN CODE = 0\n"
      return 1
   fi

   if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
        if [ ! -z "${largs}" ];then
           command_args="`${PRINT_CMD} ${largs} | ${SED_CMD} -e 's/pcl_//g' -e 's/ps_//g' -e 's/forms_//g' -e 's/iq_//g' -e 's/ppds_//g'`"
           ${PRINT_CMD} "${LPR_CMD} -P ${lqueue_name} -C \"lexopt:${command_args}\""
        else
           ${PRINT_CMD} "${LPR_CMD} -P ${lqueue_name}"
        fi
   elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
        if [ ! -z "${largs}" ];then
           command_args="`${PRINT_CMD} ${largs} | ${SED_CMD} -e 's/pcl_//g' -e 's/ps_//g' -e 's/forms_//g' -e 's/iq_//g' -e 's/ppds_//g'`"
           ${PRINT_CMD} "${LP_CMD} -d ${lqueue_name} -o \"${command_args}\""
        else
           ${PRINT_CMD} "${LP_CMD} -d ${lqueue_name}"
        fi
   fi

   return 0
}


#---------------------------------------------------
#-- process_display
#--
#-- This function process the -display argument
#--
#-- Syntax:
#-- process_display <display-hostname>
#--
#--  Return   --   Success
#--  Exit 1   --   Invalid usage
#--
process_display ()
{
   display="${1}"

   if [ -z "${display}" ] || [ "${display}" = "-" ]; then
      ${PRINT_CMD} "Usage: $0 [-d display-hostname]"
      exit 1;
   else
      ${PRINT_CMD} ${display} | ${GREP_CMD} ":" >/dev/null 2>&1
      if [ "$?" = "1" ];then
         DISPLAY=${display}:0.0
         export DISPLAY
      else
         DISPLAY=${display}
         export DISPLAY
      fi
   fi
   return
}


#---------------------------------------------------
#--  check_display
#--
#--  This function checks to see if the current X
#--  environment accepts connections.
#--
#--  Syntax:
#--  check_display
#--
#--  RETURN 0  --  X server connection refused, show message.
#--
check_display ()
{
   #-- Check for the xdpyinfo commmand.  If administrators
   #-- do not install X, this file will not exist. So,
   #-- skip the test.
   if [ -x ${XDPYINFO_CMD} ]; then
      ${XDPYINFO_CMD} </dev/null >/dev/null 2>&1
      if [ "${?}" != "0" ]; then
         ${PRINT_CMD}
         ${PRINT_CMD} "${PRODUCT_LINK}/bin/`${BASENAME_CMD} ${0} .sh` error: Can't open display $DISPLAY"
         ${PRINT_CMD} "Check your DISPLAY Environment Variable and make sure you have permissions"
         ${PRINT_CMD} "to connect to that DISPLAY ( xhost + hostname )."
         ${PRINT_CMD}
      fi
   else
      pdebug "${XDPYINFO_CMD} missing, skip checking display environment.\n"
   fi
   return 0
}


#---------------------------------------------------
#-- option_mutually_exclusive
#--
#-- This function prints the error that the supplied
#-- command line arguments are mutually exclusive.
#--
#-- Syntax:
#-- option_mutually_exclusive <mutually_exclusive_arguments>
#--
option_mutually_exclusive ()
{
  ${PRINT_CMD}
  ${PRINT_CMD} "Options [ $* ] are mutually exclusive. use one option only."
}


#---------------------------------------------------
#-- pdebug
#--
#-- This function takes one "string" argument and
#-- prints it to standard error for debuging purposes
#-- DEBUG variable must be set to 0 otherwise do nothing.
#--
#-- Syntax:
#-- pdebug <output_string>
#--
pdebug ()
{
   if [ "${DEBUG}" = "1" ]; then
      ${PRINTF_CMD} "$1" >&2
   fi
}


#---------------------------------------------------
#-- get_os_type
#--
#-- This function checks to see the System supports
#-- Aliases or Classes.  
#--
#-- If it supports Classes, return the class list.
#--
#-- Syntax:
#-- get_os_type
#--
#-- Return 0  --  Unknown
#-- Return 1  --  Classes
#-- Return 2  --  Classes plus prompt
#-- Return 3  --  Aliases
#--
get_os_type()
{
   if [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
      OS_TYPE="CLASS"
      OS_TYPE_EXIT="1"
   else
      OS_TYPE="ALIAS"
      OS_TYPE_EXIT="3"
   fi
   if [ "${OS_TYPE}" = "ALIAS" ];then
      ${PRINT_CMD} "BSD Print Subsystem - Printer Aliases are supported." >&2
   elif [ "${OS_TYPE}" = "CLASS" -o "${OS_TYPE}" = "CLASS PLUS PROMPT" ];then
      ${PRINT_CMD} "SystemV Print Subsystem - Printer Classes are supported." >&2
   fi
   return ${OS_TYPE_EXIT}
}


#---------------------------------------------------
#--  display_version
#--
#--  This function displays the version of this app
#--
#--  Syntax:
#--  dipslay_version
#--
display_version()
{
   ${PRINT_CMD}
   ${CAT_CMD} ${PRODUCT_LINK}/docs/version.txt 
   if [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then
      lps="CUPS"
   else
      lps="LPD"
   fi
   ${PRINT_CMD} "Print SubSystem (${lps})"
   ${PRINT_CMD}
}


#---------------------------------------------------
#--  is_privileged_user
#--
#--  This function is called to check if the current user is
#--  privileged.
#--
#--  Syntax:
#--  is_privileged_user
#--
#--  Return 0  -  User is priviledged
#--  Return 1  -  User is not priviledged
#--
is_privileged_user ()
{
   id="`${ID_CMD}`"
   ${PRINT_CMD} "${id}" | ${GREP_CMD} "uid=0">/dev/null 2>&1
   if [ "${?}" = "0" ]; then
      pdebug "User ID [ ${id} ], priviledged user.\n"
      return 0
   else
      pdebug "User ID [ ${id} ], non priviledged user.\n"
      return 1
   fi
}


#---------------------------------------------------
#--  check_userid
#--
#--  This function verifies a userid
#--
#--  Syntax:
#--  check_userid user
#--
#--  Return 0  --  Valid User ID
#--  Return 5  --  Invalid User ID
#--
check_userid()
{
    luser="${1}"
    ${TOUCH_CMD} /tmp/testfile.users$$
    ${CHOWN_CMD} ${luser} /tmp/testfile.users$$ >>/dev/null 2>&1
    if [ $? -ne 0 ]; then
       return_code=5;
       pdebug "\nUser [ ${luser} ] is not a valid user!\n"
    else
       return_code=0;
       pdebug "\nUser [ ${luser} ] is a valid user!\n"
    fi
    ${RM_CMD} -f /tmp/testfile.users$$
    return ${return_code}
}


#---------------------------------------------------
#--  check_characters
#--
#--  This function verifies a device/queue/alias name
#--
#--  Syntax:
#--  check_characters <queue_name> <name_type>
#--
#--  Example:
#--  check_characters queue1 Queue
#--
#--  Return 0  --  Valid Name
#--  Return 1  --  Invalid Name
#--
check_characters()
{
   lname="${1}"
   lshowtype="${2}"
   max_length=15

   case ${lshowtype} in
        "Queue") max_length=15 
				lshowtype="Printer"
				;;

        "Alias") max_length=15 ;;
       "Device") max_length=26 ;;
   esac

   ${PRINT_CMD} "${lname}" | ${EGREP_CMD} -e "^[A-Za-z][-_A-Za-z0-9]*$" >/dev/null 2>&1
   char_return=${?}

   queue_length="`${PRINT_CMD} ${lname} | ${WC_CMD} -c`"
   length_return="`${EXPR_CMD} ${queue_length} \<= ${max_length}`"
   
   if [ "${char_return}" != "0" ] || [ "${length_return}" != "1" ];then
      if [ ${length_return} = "0" ];then
         cutchar="`${EXPR_CMD} ${queue_length} - ${max_length} - 1`"
         lnametmp=`${PRINT_CMD} ${lname} | ${SED_CMD} -e "s/.\{$cutchar,$cutchar\}$//"`
         ${PRINT_CMD} "Invalid ${lshowtype} Name [ ${lnametmp}.... ]."
      else
         ${PRINT_CMD} "Invalid ${lshowtype} Name [ ${lname} ]."
      fi
      
      ${PRINT_CMD}
      ${PRINT_CMD} "Valid names must begin with a letter and can contain the"
      ${PRINT_CMD} "characters 'A-Z', 'a-z', '0-9',  '-' and '_'."
      ${PRINT_CMD}
      ${PRINT_CMD} "${lshowtype} Names may be no longer than `${EXPR_CMD} ${max_length} - 1` characters."
      ${PRINT_CMD}
      return_code=1
   else
      return_code=0
   fi
   return ${return_code}
}


#---------------------------------------------------
#--  verify_printer_type
#--
#--  This function verifies the printer type.
#--
#--  Syntax:
#--  verify_printer_type printer_type
#--
#--  Example:
#--  verify_printer_type 10LC750
#--
#--  Return 0  --  Valid type
#--  Return 1  --  Invalid type
#--
verify_printer_type()
{
   lprinter_type="${1}"

   if [ "${DEBUG}" = "1" ];then
      ${PRODUCT_LINK}/bin/supported_printers | ${GREP_CMD} " ${lprinter_type} "
   else
      ${PRODUCT_LINK}/bin/supported_printers | ${GREP_CMD} " ${lprinter_type} " >/dev/null 2>&1
   fi
   if [ "${?}" = "0" ];then
      return_code=0;
   else
      return_code=1;
   fi
   return ${return_code};
}



#---------------------------------------------------
#--  verify_printer_emulation
#--
#--  This function verifies the printer emulation type 
#--  is available for a specific printer type.
#--
#--  Syntax:
#--  verify_printer_emulation printer_type printer_emulation
#--
#--  Example:
#--  verify_printer_emulation 10LC750 pcl
#--
#--  Return 0  --  Valid Type
#--  Return 1  --  Invalid Type
#--
verify_printer_emulation()
{
   lprinter_type="${1}"

   #-- Make sure emulation is all lower case
   lprinter_emulation="`${PRINT_CMD} ${2} | ${TR_CMD} '[:upper:]' '[:lower:]'`"

   #-- Verify valid type
   case "${lprinter_emulation}" in
        automatic) ;;
   automatic_plus) ;;
   automatic_pdf) ;;
            forms) ;;
             ppds) ;;
              pcl) lprinter_emulation="automatic|automatic_plus|pcl|automatic_pdf" ;;  
               ps) lprinter_emulation="automatic|automatic_plus|ps|automatic_pdf" ;;
               iq) lprinter_emulation="automatic_plus|iq" ;;
              pdf) lprinter_emulation="automatic_pdf|pdf" ;;
              raw) lprinter_emulation="automatic|automatic_plus|forms|pcl|ppds|ps|iq|raw|automatic_pdf|pdf" ;;
                *) pdebug "Unknown emulation ( ${lprinter_emulation} )\nRETURN CODE = 1\n" 
                return 1 ;;  
   esac

   #-- Check to see if emulation is available for the specific printer type
   if [ "${DEBUG}" = "1" ];then
      ${PRODUCT_LINK}/bin/supported_printers | ${EGREP_CMD} -E " ${lprinter_type}  *(${lprinter_emulation})"
   else
      ${PRODUCT_LINK}/bin/supported_printers | ${EGREP_CMD} -E " ${lprinter_type}  *(${lprinter_emulation})" >/dev/null 2>&1
   fi
   if [ "${?}" = "0" ];then
      return 0
   else
      return 1
   fi
}


#---------------------------------------------------
#--  verify_printer_options
#--
#--  This function verifies the printer type and  
#--  printer language for print queue creation.
#--
#--  Syntax:
#--  verify_printer_options <printer_type> <printer_language> 
#--
#--  Example:
#--  verify_printer_options 10LC750 automatic
#--
#--  RETURN 0  --  Success -- No Output
#--  RETURN 1  --  Error   -- Invalid Printer Type
#--  RETURN 2  --  Error   -- Invalid Printer Language 
#--
#--
verify_printer_options()
{
   lpType="${1}" 
   lpLanguage="`${PRINT_CMD} ${2} | ${TR_CMD} '[:upper:]' '[:lower:]'`"
   lang_match="false"      
  
   #-- Catch auto for automatic and ps
   if   [ "${lpLanguage}" = "auto" ];then
        lpLanguage="automatic"
   elif [ "${lpLanguage}" = "postscript" ];then
        lpLanguage="ps"
   fi
  
   #-- Verify printer type
   pLine=`${PRODUCT_LINK}/bin/supported_printers | ${GREP_CMD} -v "^ *#" | ${GREP_CMD} " ${lpType} "`
   if [ "${?}" != "0" ];then
      pdebug "Printer Type Not Found : [ ${pLine} ]\n"
      ${PRINT_CMD}
      ${PRINT_CMD} "Printer Type [ ${lpType} ] is not a valid printer type."
      ${PRINT_CMD} "Run the following command for valid printer types."
      ${PRINT_CMD}
      ${PRINT_CMD} "${PRODUCT_LINK}/bin/supported_printers"
      ${PRINT_CMD}
      return 1
   fi
   pdebug "Found Printer Type: [ ${pLine} ]\n"
  
   #-- Verify printer language
   sLang=`${PRINT_CMD} ${pLine} | ${SED_CMD} 1q | ${SED_CMD} -e 's/.* //g' `
   if   [ "${lpLanguage}" = "raw" ];then
        lang_match="true"      
   elif [ "${sLang}" = "automatic" ];then
        if [ "${lpLanguage}" = "pcl" -o "${lpLanguage}" = "ps" -o "${lpLanguage}" = "automatic" ];then
           lang_match="true"      
        fi
   elif [ "${sLang}" = "automatic_plus" ];then
        if [ "${lpLanguage}" = "pcl" -o "${lpLanguage}" = "ps" -o "${lpLanguage}" = "iq" -o "${lpLanguage}" = "automatic_plus" ];then
           lang_match="true"      
        fi
   elif [ "${sLang}" = "automatic_pdf" ];then
        if [ "${lpLanguage}" = "pcl" -o "${lpLanguage}" = "ps" -o "${lpLanguage}" = "pdf" -o "${lpLanguage}" = "automatic_pdf" ];then
           lang_match="true"      
        fi
   elif [ "${sLang}" = "${lpLanguage}" ];then
        lang_match="true"      
   fi
  
   if [ "${lang_match}" = "false" ];then
      ${PRINT_CMD} "Printer Language (Emulation) [ ${lpLanguage} ] does not match the printer type."
      if   [ "${sLang}" = "automatic" ];then
           ${PRINT_CMD} "Please specify automatic, pcl, ps or raw."
      elif [ "${sLang}" = "automatic_plus" ];then
           ${PRINT_CMD} "Please specify automatic_plus, pcl, ps, iq or raw."
      elif [ "${sLang}" = "automatic_pdf" ];then
           ${PRINT_CMD} "Please specify automatic_pdf, pdf or raw."
      else
         ${PRINT_CMD} "Please specify ${sLang} or raw."
      fi
      return 2
   fi
  
   #-- ALL is Well
   return 0
} 

#---------------------------------------------------
#--  get_queue_emulation
#--
#--  This function returns the queue's printer language.
#--
#--  Syntax:
#--  get_queue_emulation queue_name
#--
#--  RETURN  --  Prints the printer language found.
#--
get_queue_emulation()
{
   lqName="${1}" 
   lqFile="${VIR_QUEUES_DIR}/${lqName}/.${lqName}"

   if [ ! -f "${lqFile}" ];then
      lpType="unknown" 
   else
      lpType=`${AWK_CMD} -F"=" '$1 ~ /ds_list/ { print $2 ; exit }' ${lqFile}`
   fi

   pdebug "Supported Printer Language: [ ${lpType} ]\n"
   ${PRINT_CMD} "${lpType}"
} 


#---------------------------------------------------
#--  check_spool_dir_permissions
#--
#--  This function returns the spool directory read status
#--
#--  Syntax:
#--  check_spool_dir_permissions
#--
#--  RETURN 0  --  Spool directories are readable.
#--  RETURN 1  --  Spool directories are not readable.
#--
check_spool_dir_permissions()
{
   if [ "${PRT_SUBSYSTEM}" != "BSD" ];then
   return 0
   fi
   ${LS_CMD} ${VIR_QUEUES_DIR}/* 2>&1 | ${GREP_CMD} -i "Permission denied" >/dev/null 2>&1
   if [ "${?}" = "0" ];then
      ${PRINT_CMD}
      ${PRINT_CMD} "This application has detected that your print subsystem"
      ${PRINT_CMD} "does not allow regular users access to the printer queue"
      ${PRINT_CMD} "settings.  Therefore, it can not run in user mode."
      ${PRINT_CMD}
      ${PRINT_CMD} "To run this application in user mode, you will need to"
      ${PRINT_CMD} "change the permissions of the spool directories.  To do this,"
      ${PRINT_CMD} "add the following two lines to the /etc/lpd.conf file and"
      ${PRINT_CMD} "restart your print subsystem."
      ${PRINT_CMD}
      ${PRINT_CMD} "  spool_dir_perms=000755"
      ${PRINT_CMD} "  spool_file_perms=000644"
      ${PRINT_CMD}
      ${PRINT_CMD} "Contact your System Administrator for more information."
      ${PRINT_CMD}
      return 1
   else 
      return 0
   fi
}

#---------------------------------------------------
#--  verify_print_subsystem
#--
#--  This function verifies the running print subsystem
#--  Matches the configured print subsystem
#--
#--  Syntax:
#--  verify_print_subsystem 
#--
#--  RETURN 0  --  OK
#--  RETURN 6  --  MisMatch
#--
verify_print_subsystem()
{
   PRINT_DAEMON="${1}"
   PROP_FILE="${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/props/app.properties" 
   if [ -f "${PROP_FILE}" ];then
       ${CAT_CMD} ${PROP_FILE} | ${GREP_CMD} "app.prt_subsystem=cups" >/dev/null 2>&1
       if [ ${?} -eq 0 ];then
          if [ "${PRINT_DAEMON}" = "1" -o "${PRINT_DAEMON}" = "2" -o "${PRINT_DAEMON}" = "5" ];then
             return 0
          else
             return 6
          fi
       else
          if [ "${PRINT_DAEMON}" = "0" -o "${PRINT_DAEMON}" = "3" -o "${PRINT_DAEMON}" = "5" ];then
             return 0
          else
             return 6
          fi
       fi
   else
       return 6
   fi
}





#---------------------------------------------------
#-- license_agreement
#--
#-- This function manages license agreements
#--
#-- Syntax:
#-- license_agreement <is_admin> <show_message_type>
#--
#-- is_admin ( true or false ) 
#-- show_message_type ( noshow, show_text, show_gui ) 
#--
#-- Return 0  --  SUCCESS ( Accepted license )
#-- Return 1  --  FAILED  ( Did not accept license )
#--
license_agreement()
{
	luser_type="${1}"
	show_mtype="${2}"
	laccept="false"

	ADMIN_DIR="${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/license_accepted"
	ADMIN_FILE="${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/license_accepted/${PDUSER}"

	GENFILE="${ROOT_SPOOL_DIR}/.${PRODUCT_NAME}/props/gen.properties"
	if [ -f "${GENFILE}" ];then
		UNIQ_DIR=`${AWK_CMD} -F= ' /gen.unique.user.dir/ { print $2 } ' ${GENFILE}` 
	else
		UNIQ_DIR="000111"
	fi
	USER_DIR="${PDHOME}/.${PRODUCT_NAME}/${UNIQ_DIR}"
	USER_FILE="${PDHOME}/.${PRODUCT_NAME}/${UNIQ_DIR}/LicenseAccepted"

	pdebug "\nLicense Agreement\n-----------------\nChecking for License Agreement\n"
	pdebug "USER TYPE = ${luser_type}\nADMIN_FILE : ${ADMIN_FILE}\nUSER_FILE  : ${USER_FILE}\n\n"

	#-- See if license has been accepted.
	if   [ -f "${ADMIN_FILE}" ];then
		  pdebug "License Agreement has already been accepted as admin. [ ADMIN_FILE ]\n"
		  return 0
	elif [ -f "${USER_FILE}" ];then
		  pdebug "License Agreement has already been accepted as user. [ USER_FILE ]\n"
		  return 0
	fi

	#-- 
 	if		[ "${show_mtype}" = "noshow" ];then
			laccept="true"
	elif	[ "${show_mtype}" = "show_text" ];then
			${MORE_CMD} ${PRODUCT_LINK}/docs/license.txt 
			${PRINTF_CMD} "\n--------------------------\n"
			${PRINT_CMD} "${APPLICATION_NAME}"
			${PRINT_CMD} "End-User License Agreement"
			${PRINT_CMD} "--------------------------"
			${PRINTF_CMD} "You must agree to the terms of the End-User License Agreement.\n\n" 
			${PRINTF_CMD} "To accept the license agreement, type 'agree' and press enter. > "
			read answer
			if [ "${answer}" = "agree" ];then
				laccept="true" 
			fi
	elif	[ "${show_mtype}" = "show_gui" ];then
			#-- LAUNCH GUI
			LICENSE_TARGET="com.lexmark.unix.drivers.jprint.jprint.LicenseWindow"
			${JAVA_CMD} ${JVM_ARGS} ${VMDEFINES} -cp ${CLASSPATH} ${LICENSE_TARGET} true >> ${DEBUG_LOG} 2>&1
			JAVA_RETURN="$?"
			case "${JAVA_RETURN}" in
				0)	laccept="true" 
					pdebug "JAVA RETURN = ${JAVA_RETURN}\n"
					;;
				*) laccept="false" 
					pdebug "JAVA RETURN = ${JAVA_RETURN}\n"
					;;
			esac		
	fi

	if 	[ "${laccept}" = "true" -a "${luser_type}" = "admin" ];then
			pdebug "Creating License Agreement file under admin directory.\n"
			[ ! -d "${ADMIN_DIR}" ] && ${MKDIR_CMD} -p ${ADMIN_DIR}
			${TOUCH_CMD} ${ADMIN_FILE}	
			return 0
	elif 	[ "${laccept}" = "true" -a "${luser_type}" = "user" ];then
			pdebug "Creating License Agreement file under user home directory.\n"
			[ ! -d "${USER_DIR}" ] && ${MKDIR_CMD} -p ${USER_DIR}
			${TOUCH_CMD} ${USER_FILE}	
			return 0
	else
			${PRINT_CMD} "License Agreement was not accepted."
			exit 1
	fi
}

