#!/bin/sh
#########################################################################
#
# chkconfig: 2345 59 61
# description: "mtink" is a low-level driver for Epson's printers and \
#              multi-function devices. It allows printing and status \
#              monitoring to be done at the same time.
#
# Start / stop script for mtinkd
#
# In order to be distibution independant, the server known a few
# extra commands:
#   start
#   stop
#   status
#
# other typical commands as restart are implemented as
# call from start and restart
#
# Authors: Jean-Jacques Sarton
#           jj.sarton@ t-online.de
#           http://xwtools.automatix.de
#         Till Kamppeter
#           http://www.linuxprinting.org/till
#
# Version 0.4-Mdk 30-08-2002
#         0.5     26-11-2002 line for f in /dev/usb/lp*... corrected
#         0.6     07-12-2002 added start of required modules (ppdev/printer)
#
##########################################################################


# define the device file, adapt this
#DEV_FILE=/dev/usb/lp0

# Special options, adapt this
NAME=
SPEC="-name $NAME -usbbase /dev/usb/lp"

# where the program is located

PATH=/sbin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:$PATH
PROG=/usr/sbin/mtinkd

ppdevStarted=n
printerStarted=n
if lsmod|grep ppdev|grep -v  grep >/dev/null 2>&1 
then
   ppdevStarted=y
fi
if lsmod|grep printer|grep -v  grep >/dev/null 2>&1 
then
   printerStarted=y
fi

startModule() {
   case $1 in
   /dev/lp*)
      if  [ $ppdevStarted = n ]
      then
          modprobe ppdev >/dev/null 2>&1
          ppdevStarted=y
      fi;;
   /dev/usb*)
      if  [ printerStarted = n ]
      then
          modprobe printer >/dev/null 2>&1
          printerStarted=y
      fi;;
   esac
}

case $1 in
   start|stop|status)
	   names=""
      if [ -f /etc/mtinkd.conf ]
      then
         while read opt1 name opt2 dev
         do
            startModule $dev
            if [ $opt1 = -name ]
            then
               echo -n "$1 `basename $0` for $name "
               if $PROG $1 $opt1 $name $opt2 $dev
               then
                  if [ $1 = stop ]
                  then
                     echo OK
                  else
                     echo RUN
                  fi
               else
                  echo STOP
               fi
            fi
         done < /etc/mtinkd.conf
      else
         modprobe printer >/dev/null 2>&1
         modprobe ppdev >/dev/null 2>&1
         for f in /dev/usb/lp* /dev/lp*
         do
	         name=`askPrinter $f | grep '[:,]D4[,;]' | sed -n '/Stylus/s/.*:EPSON Stylus \(.*\);/\1/pg' | tr ' ' '_'`
	         if [ ! -z $name ]
            then
	            echo -n "$1 `basename $0` for $name "
               if echo $f | grep usb > /dev/null
               then
	               $PROG $1 -name $name -usbbase /dev/usb/lp
               else
                  $PROG $1 -name $name -dev $f
               fi
               if [ $? -eq 0 ]
               then
                  if [ $1 = stop ]
                  then
                     echo OK
                  else
                     echo RUN
                  fi
               else
                  echo STOP
               fi
	         fi
         done
      fi
	   RETVAL=$?;;
   restart)
	   $0 stop
	   sleep 2
	   $0 start
	   RETVAL=$?;;
   *)
	   echo "Syntax `basename $0` start|stop|status|restart"
	   RETVAL=1;;
esac

RETVAL=$?
exit $RETVAL
