#!/bin/sh
#
# Script to create a XPP printer
# Syntax: MAKEXPP printerName [URI] [XPP path]
#
if [ $# -lt 1 ] ; then
  echo "Usage: $0 printerName [URI] [XPP path]"
  echo "where:"
  echo "printerName = name of the queue to create"
  echo "URI  = /dev/null, or URI of Canon device"
  echo "XPP Path = full path of the xpp program"
  exit 0
fi
qName=$1
uri="file: /dev/null"
xppPath=null
if [ $# -gt 1 ] ; then
  uri=$2
  if [ $# -gt 2 ] ; then
    xppPath=$3
  fi
else
  uri=/dev/null
fi
#
# Search for xpp path
#
searchXPP()
{
  if [ -x /usr/bin/xpp ] ; then
    xppPath=/usr/bin/xpp
    return
  fi
  if [ -x /usr/local/bin/xpp ] ; then
    xppPath=/usr/local/bin/xpp
    return
  fi
  if [ -x /opt/bin/xpp ] ; then
    xppPath=/opt/bin/xpp
    return
  fi
  if [ -x /usr/sbin/xpp ] ; then
    xppPath=/usr/sbin/xpp
    return
  fi
  if [ -x /bin/xpp ] ; then
    xppPath=/bin/xpp
    return
  fi
  echo "No XPP program found."
  echo "Sorry!"
  exit 0
}
#
# Create .sh script
#
createShScript()
{
  TMPFNAME=/tmp/SIC$$
  echo "#!/bin/sh" > $TMPFNAME
  echo "DISPLAY=:0.0" >> $TMPFNAME
  echo "export DISPLAY" >> $TMPFNAME
  echo "/bin/cat \$1 | ${xppPath}" >> $TMPFNAME
  /bin/mv -f $TMPFNAME /opt/cel/bin/${qName}.sh
  /bin/chmod 755 /opt/cel/bin/${qName}.sh
  echo "Created shell script at: /opt/cel/bin/${qName}.sh"
  return 1
}
#
# Create model script
#
createModelScript()
{
  TMPFNAME=/tmp/CIS$$
  echo "#!/bin/sh" > $TMPFNAME
  echo "#" >> $TMPFNAME
  echo "# lp interface for CQue 1.0-4 XPP Queue." >> $TMPFNAME
  echo "# Created at `date`" >> $TMPFNAME
  echo "#" >> $TMPFNAME
  echo "# Uncomment the following line for debugging info in the logfile" >> $TMPFNAME
  echo "# set -x" >> $TMPFNAME
  echo "#" >> $TMPFNAME
  echo "NAME=${qName}" >> $TMPFNAME
  echo "TYPE=Dumb" >> $TMPFNAME
  echo "WDIR=/opt/cel/bin" >> $TMPFNAME
  echo "DISPLAY=\":0.0\"" >> $TMPFNAME
  echo "export DISPLAY" >> $TMPFNAME
  echo "#" >> $TMPFNAME
  echo "# You normally don't have to modify beyond this line." >> $TMPFNAME
  echo "#" >> $TMPFNAME
  echo "# Get proper user name" >> $TMPFNAME
  echo "getTrueUserName()" >> $TMPFNAME
  echo "{" >> $TMPFNAME
  echo "  echo \$1 | grep @ >/dev/null" >> $TMPFNAME
  echo "  ret=\$?" >> $TMPFNAME
  echo "  if [ \$ret -eq 0 ] ; then" >> $TMPFNAME
  echo "    theName=\`echo \$1 | tr \"@\" \"\\\\012\" | head -1\` >/dev/null" >> $TMPFNAME
  echo "  else" >> $TMPFNAME
  echo "    echo \$1 | grep ! >/dev/null" >> $TMPFNAME
  echo "    ret=\$?" >> $TMPFNAME
  echo "    if [ \$ret -eq 0 ] ; then" >> $TMPFNAME
  echo "      theName=\`echo \$1 | tr \"!\" \"\\\\012\" | tail -1\` >/dev/null" >> $TMPFNAME
  echo "    else" >> $TMPFNAME
  echo "      theName=\$1" >> $TMPFNAME
  echo "    fi" >> $TMPFNAME
  echo "  fi" >> $TMPFNAME
  echo "  echo \$theName" >> $TMPFNAME
  echo "}" >> $TMPFNAME
  echo "#" >> $TMPFNAME
  echo "printer=\`basename \$0\`                   # the printer\'s name" >> $TMPFNAME
  echo "seqid=\$1                                # job sequence id number" >> $TMPFNAME
  echo "username=\`getTrueUserName \$2\`" >> $TMPFNAME
  echo "title=\`echo \"\$3\" | tr \"\\040\" \"\\055\"\`    # job title" >> $TMPFNAME
  echo "copies=\$4                               # number of copies" >> $TMPFNAME
  echo "options=\"\$5\"                            # -o options" >> $TMPFNAME
  echo "shift 5                                 # discard args up to filenames" >> $TMPFNAME
  echo "files=\"\$*\"                              # input file(s)" >> $TMPFNAME
  echo "LOGFILE=/tmp/\${printer}_\${username}.log" >> $TMPFNAME
  echo "" >> $TMPFNAME
  echo "for file in \$files" >> $TMPFNAME
  echo "do" >> $TMPFNAME
  echo "  \$WDIR/${qName}.sh  \"\$file\" 2>\$LOGFILE" >> $TMPFNAME
  echo "  stat=\$?" >> $TMPFNAME
  echo "done" >> $TMPFNAME
  echo "exit \$stat" >> $TMPFNAME
  /bin/mv -f $TMPFNAME /opt/cel/etc/${qName}.model
  echo "Created model script at: /opt/cel/etc/${qName}.model"
  return 1
}
#
# Start of execution
#
echo "Start of creation for queue: ${qName}"
if [ "$xppPath" = "null" ] ; then
  searchXPP
fi
echo "Found xpp at: ${xppPath}"
createShScript
createModelScript
/usr/sbin/lpadmin -p ${qName} -i /opt/cel/etc/${qName}.model -v "$uri"
stat=$?
if [ $stat -ne 0 ] ; then
  echo "lpadmin returned status code: $stat"
fi
/usr/sbin/accept ${qName}
stat=$?
if [ $stat -ne 0 ] ; then
  echo "accept returned status code: $stat"
fi
/usr/sbin/cupsenable ${qName}
stat=$?
if [ $stat -ne 0 ] ; then
  echo "cupsenable returned status code: $stat"
fi
echo "Created queue: ${qName}."
