#!/bin/sh

########################################################################
#             (c) Copyright 2003 Lexmark International, Inc.           #
#                        All rights reserved                           #
########################################################################
#                                                                       
# This command prints test pages and files.
#
########################################################################
#
#	0 --- Success
#	1 --- Error
#
########################################################################

#-- set debug to false by default. To turn debug "on" set to "1"
DEBUG=0

#-- initialize variables
print_test_page="no"


PRODUCT_LINK="/usr/genprint"
SCRIPTS_DIR="${PRODUCT_LINK}/bin/.scripts"

#-- Load the shared script
. ${SCRIPTS_DIR}/shared
#-- Initialize
initialize

program_name=`${BASENAME_CMD} $0`

#-- Trap signal 2 and 15 and exit
trap "cleanup; exit 0" 2 15

#-- usage_short text
usage_short ()
{
${CAT_CMD} << EOT

USAGE:
    ${program_name} -f file -q queue [-o "opt1 opt2 ..." ] [-x]
    ${program_name} -t -q queue [-u username] [-x]
    ${program_name} -h
    ${program_name} -v

Try '${program_name} -h' for more information

EOT
}


#-- usage_long text
usage_long ()
{
${CAT_CMD} << EOT

${program_name}
    Prints test pages and regular files from a command line.
 
USAGE:
    ${program_name} -f file -q queue [-o "opt1 opt2 ..." ] [-x]
    ${program_name} -t -q queue [-u username] [-x]
    ${program_name} -h
    ${program_name} -v

OPTIONS:
    -f file
       Specify a file to print.

    -q queue
       Specify a printer queue.

    -o "opt1 opt2 ..."
       Specify a list of printer options seperated with space(s) in quotes. 

    -t
       Print a test page.

    -u username
       Specify a username.

    -x
       Run ${program_name} in debug mode.

    -h
       Display this help and exit.

    -v
       Display version information and exit.

EXAMPLES:
    Print the file myfile.txt on the queue myqueue:

       $ ${program_name} -f myfile.txt -q myqueue

    Print a test page on the queue myqueue:

       $ ${program_name} -t -q myqueue -u myname

    Print the file myfile.txt on the queue myqueue with options:

       $ ${program_name} -f myfile.txt -q myqueue \\
         -o "font=courier_bold banner_page=no"

       or

       $ ${program_name} -f myfile.txt -q myqueue \\
         -o font=courier_bold -o banner_page=no


EOT
}


verify_arg()
{
  if [ -z "${3}" ];then
      usage_short
      ${PRINT_CMD} "Missing [${1}] option, please specify a ${2}."
      exit 99
   fi
}

print_test()
{
   device_name=`${AWK_CMD} -F= '$1 ~ /device_name/ {print $2;exit}' ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name}`
   emulation=`${AWK_CMD} -F= '$1 ~ /ds_list/ {print $2;exit}' ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name}`
   printer_name=`${AWK_CMD} -F= '$1 ~ /printer_type/ {print $2;exit}' ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name}`
   pdd_name=`${AWK_CMD} -F= '$1 ~ /pdd/ {print $2;exit}' ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name}`


   if [ "${emulation}" != "ppds" ]; then
        paper_size=`${AWK_CMD} -F= '$1 ~ /paper_size/ {print $2;exit}' ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name}`

           if [ "${paper_size}" = "A4" ];then
              paper_size="A4"
           else
              paper_size="letter"
           fi

   else
        paper_size=`${AWK_CMD} -F= '$1 ~ /page_length_inches/ {print $2;exit}' ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name}`
   fi   



   if [ "${user_name}" = "" ];then
    user_name=`${WHOAMI_CMD}`
   fi

   if [ "${user_name}" = "" ];then
   user_name="Unknown"
   fi

   if [ "${device_name}" = "" ];then
      device_name="Unknown"
   fi

   if [ "${emulation}" = "" ];then
      emulation="Unknown"
   fi

   if [ "${printer_name}" = "" ];then
      printer_name="Unknown"
   fi

   if [ "${pdd_name}" = "" ]; then
      pdd_name="Unknown"
   fi


    get_queue_settings ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name} >/dev/null 2>&1
    if [ "${?}" != "0" ];then
       ${PRINT_CMD} "Printer queue [ ${queue_name} ] is not a ${APPLICATION_NAME} queue."
       exit 1
    fi
    check_queue_status ${queue_name} >/dev/null 2>&1
    if [ "${?}" != "0" ];then
       ${PRINT_CMD} "Printer queue [ ${queue_name} ] is not a ${APPLICATION_NAME} queue."
       exit 1
    fi

    if [ "${emulation}" = "automatic" -o "${emulation}" = "pcl" ];then
       test_program="${PRODUCT_LINK}/etc/banners/test_page_pcl5"
    else
	if [ "${emulation}" = "ppds" ]; then
		test_program="${PRODUCT_LINK}/etc/banners/test_page_ppds"
	else
		test_program="${PRODUCT_LINK}/etc/banners/test_page_pcl5"
	fi
    fi

    hostname="`${HOSTNAME_CMD}`"

                   case ${APPLICATION_LANG}
                     in
                        en*|EN*) 
                           	APPLICATION_LANG=en; break
                              ;;
                        fr*|FR*) 
                              APPLICATION_LANG=fr; break
                              ;;
                        it*|IT*)
                              APPLICATION_LANG=it; break
                              ;;
                        de*|DE*)
                              APPLICATION_LANG=de; break
                              ;;
                        es*|ES*)
                              APPLICATION_LANG=es; break
                              ;;
                        pt*|PT*)
                              APPLICATION_LANG=pt; break
                              ;;
                        ja*|JA*)
                              APPLICATION_LANG=ja; break
                              ;;
                        ko*|KO*)
                              APPLICATION_LANG=ko; break
                              ;;
                        zh_TW*|zh_HK*|ZH_TW*)
                              APPLICATION_LANG=tc; break
                              ;;
                        zh*|ZH*)
                              APPLICATION_LANG=sc; break
                              ;;
                        *)
                              APPLICATION_LANG=en; break
                              ;;
                   esac

    if   [ "${PRT_SUBSYSTEM}" = "BSD" ];then
         ${test_program} ${user_name} ${hostname} ${queue_name} "${printer_name}" ${device_name} ${emulation} ${paper_size} ${APPLICATION_LANG} ${pdd_name} | ${LPR_CMD} -P ${queue_name} -C "lexopts:banner_page=no"
    elif [ "${PRT_SUBSYSTEM}" = "SYSTEM_V" ];then 
         ${test_program} ${user_name} ${hostname} ${queue_name} "${printer_name}" ${device_name} ${emulation} ${paper_size} ${APPLICATION_LANG} ${pdd_name} | ${LP_CMD} -d ${queue_name} -o banner_page=no 
    fi
    return ${?}
}

print_the_file()
{
    pdebug "Print options=${queue_options}\n"
    print_command="`build_print_command ${queue_name} ${queue_options}`"
    pdebug "Print command=${print_command} ${file_name}\n"
    eval ${print_command} \"${file_name}\"
    return ${?}
}

#-- MAIN --#
#-- parse command line arguments --#
while getopts f:o:q:tu:xvh? arg
do
  case "${arg}" in
  x) DEBUG=1 ;;
  f) file_name="${OPTARG}" ;;
  h) usage_long; exit 99 ;;
  o) if [ -z "${queue_options}" ];then 
         queue_options="${OPTARG}"
     else 
         queue_options="${queue_options} ${OPTARG}"
     fi
     ;;
  q) queue_name="${OPTARG}";;
  t) print_test_page="yes" ;;
  u) user_name="${OPTARG}" ;;
  v) display_version; exit 99 ;;
  *) usage_short; exit 99 ;;
  esac 
done

file_name=`echo ${file_name} | sed -e 's/ /\ /g'`

if [ "${print_test_page}" = "yes" ] && [ ! -z "${file_name}" ];then
   usage_short
   ${PRINT_CMD} "Invalid options, specify either the -f or -t flags."
   exit 99
fi

if [ -z "${queue_name}" ];then
   usage_short
   ${PRINT_CMD} "Missing [-q] option, please specify a queue name."
   exit 99
else
   get_queue_settings ${VIR_QUEUES_DIR}/${queue_name}/.${queue_name} >/dev/null 2>&1
   if [ "${?}" != "0" ];then
      ${PRINT_CMD} "Printer queue [ ${queue_name} ] is not a ${APPLICATION_NAME} queue."
      exit 1
   fi
   check_queue_status ${queue_name} >/dev/null 2>&1
   if [ "${?}" != "0" ];then
      ${PRINT_CMD} "Printer queue [ ${queue_name} ] is not a ${APPLICATION_NAME} queue."
      exit 1
   fi
fi

if [ "${print_test_page}" = "yes" ];then
   print_test
   exit $?
else
   if [ ! -z "${user_name}" ];then
      usage_short
      ${PRINT_CMD} "One or more options entered are invalid."
      exit 99
   fi

   verify_arg -f "filename" "${file_name}"
   if [ ! -f "${file_name}" ];then
      ${PRINT_CMD} "File not found."
      exit 1
   fi

   print_the_file
   exit ${?} 
fi

exit 0

