#! /bin/sh

############################################################################
#  irlptd - Bourne Shell Skript to write incoming (!) IR printing requests #
#           into files                                                     #
#                                                                          #
#  Copyright (C) 1999 Peter J. Weyers                                      #
#                                                                          #
#  This program is free software; you can redistribute it and/or modify    #
#  it under the terms of the GNU General Public License version 2 as       #
#  published by the Free Software Foundation.                              #
#                                                                          #
#  This program is distributed in the hope that it will be useful,         #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#  GNU General Public License for more details.                            #
############################################################################


SPOOLDIR=/var/spool/irlptd
DEV=/dev/irlptd
name=`basename $0`
pidfile=/var/run/${name}.pid

for arg in "$@"
do
  case "${arg}" in
    -k) if [ ! -f "${pidfile}" ]
        then
          echo "${pidfile} does not exist ..." >&2
          exit 42
        else
          kill `cat ${pidfile}` && exit 0
          exit 42
        fi
  esac
done

if [ -f ${pidfile} ]
then
  if ps -p `cat ${pidfile}` > /dev/null
  then
    echo "$name" "already running with pid `cat ${pidfile}` ?" >&2
    exit 42
  else
    logger -t "${name} ($$)" "removing stale ${pidfile}"
    rm -f ${pidfile} || exit 42
  fi
fi

if fuser ${DEV} > /dev/null
then
  echo "$name: ${DEV} busy" >&2
  exit 42
fi

#if tty >/dev/null 2>&1
if [ "$PPID" != 1 ]
then
  exec $0 "$@" </dev/null >/dev/null 2>&1 &
  disown $!
  exit
fi

function on_exit
{
logger -t "${name} ($$)" "terminating"
rm -f ${pidfile} 
}

function on_term
{
logger -t "${name} ($$)" "recieved SIGTERM"
rm -f ${pidfile}
exit 
}

exec > /tmp/irlptd.$$.log

echo "-------- starting $$ ----`date`"
set
export


trap on_exit EXIT
trap on_term SIGTERM

echo $$ > ${pidfile}

logger -t "${name} ($$)" "starting"

sleep 1

seq_nr=`cat ${SPOOLDIR}/.sequence` || exit 42

while [ "true" ]
do
  logger -p daemon.debug -t "${name} ($$)" "starting loop at (${seq_nr})"
  read line1 < ${DEV}
  logger -p daemon.debug -t "${name} ($$)" "line '${line2}' (${seq_nr}) read"
  if [ -n "${line1}" ]
  then
    echo "$line1" > ${SPOOLDIR}/job.${seq_nr}
    logger -p daemon.debug -t "${name} ($$)" "line '$line1' written"
  else
    logger -p daemon.debug -t "${name} ($$)" "empty line not written"
  fi
  logger -p daemon.debug -t "${name} ($$)" "doing cat (${seq_nr})"
  cat ${DEV} >> ${SPOOLDIR}/job.${seq_nr}
  logger -p daemon.debug -t "${name} ($$)" "cat (${seq_nr}) done"
  format="`file - < ${SPOOLDIR}/job.${seq_nr}|sed -e 's;standard input: *;;g'`"
  logger -p daemon.debug -t "${name} ($$)" \
                              "File Format: '${format}'(${seq_nr})"
  case "${format}" in
   "PostScript "*) mv ${SPOOLDIR}/job.${seq_nr} ${SPOOLDIR}/job.${seq_nr}.ps  ;;
   "International language text"|\
   "ASCII "*)      mv ${SPOOLDIR}/job.${seq_nr} ${SPOOLDIR}/job.${seq_nr}.txt ;;
  esac
  let seq_nr=seq_nr+1
  echo "${seq_nr}" > ${SPOOLDIR}/.sequence
done
