#------------------------------------------------------------------------------
# /etc/rc.d/rc445.lpd - start lpd                                  3.1.4
# was file /etc/rc.d/rc550.lpd
#
# Creation   : 27.12.2000  fm
# Last Update: $Id: rc445.lpd 14139 2007-12-27 12:47:26Z gernotm $
#------------------------------------------------------------------------------
if [ "$OPT_LPD" = "yes" ]
then
    begin_script LPD "configuring lpd..."
    echo "lp:x:7:" >>/etc/group
    echo "printer 515/tcp" >>/etc/services

    #--------------------------------------------------------------------------
    # create ramdisk and directories for spooling:
    #--------------------------------------------------------------------------
    port=''
    io=''
    irq=''
    interrupt=''
    parport=''
    dma=''
    direct=''
    spooldir_error='true'
    path=''
    lpdir='/var/spool/lp'
    lpddir='/var/spool/lpd'
    do_printing=''
    printcapfile='/etc/printcap'
    hostslpdfile='/etc/hosts.lpd'
    colechobin='/usr/local/bin/colecho'
    loggerbin='/usr/bin/logger'
    data_mount=`cat /proc/mounts | grep ' /data ' | grep ' rw'`

    if [ -f "$loggerbin" ]
    then
        msgcmd="$loggerbin -t samba_lpd -p local0"
        msgcmdfail="$msgcmd.error "
        msgcmdinfo="$msgcmd.info "
    else
        msgcmd='echo'
        msgcmdfail='echo'
        msgcmdinfo='echo'
    fi

    mkdir -p "$lpddir"
    /bin/chmod 775 "$lpddir"
    /bin/chown root "$lpddir"
    /bin/chgrp root "$lpddir"

    if [ -n "$data_mount" ]
    then
        for i in minix ext2 ext3
        do
          if [ -n "`echo $data_mount | grep $i`" ]
          then
              spooldir_error='false'
              $msgcmdinfo "mounted writeable partition /data with file system $i found"
              path='/data'
          fi
        done

        if [ "$spooldir_error" = "false" ]
        then
            path="$path/lp_spooldir"

            if [ -d "$path" ]
            then
                rm -r "$path"
            fi

            mkdir "$path"

            if [ $? -eq 0 ]
            then
                $msgcmdinfo "spool directory $path on $HOSTNAME created"   
                ln -s "$path" "$lpdir"
                do_printing='yes'
            else
                $colechobin "*** ERROR: cannot create spool directory $path on $HOSTNAME! ***" br x br
                $colechobin "*** ERROR: check /proc/mounts! ***" br x br
                $colechobin "*** ERROR: printing function on hdd not available! ***" br x br
                sleep 5
                $msgcmdfail "*** ERROR: cannot create spool directory $path on $HOSTNAME! ***"
                $msgcmdfail "*** ERROR: check /proc/mounts! ***"
                $msgcmdfail "*** ERROR: printing function on hdd not available! ***"
                spooldir_error='true'
            fi
        fi
    else
        $msgcmdinfo "no mounted writeable minix/ext2/ext3 data partition found"
        sleep 5
    fi

    if [ "$spooldir_error" = "true" ] 
    then
        mkdir "$lpdir"
        do_printing='yes'
        $colechobin "*** Warning! ***" br x br
        $colechobin "*** spooling into rootfs ramdisk! ***" br x br
        $colechobin "*** big jobs can break router functionality! ***" br x br
        sleep 5
        $msgcmdinfo "*** Warning! ***"
        $msgcmdinfo "*** spooling into rootfs ramdisk! ***"
        $msgcmdinfo "*** big jobs can break router functionality! ***"
    fi

    #--------------------------------------------------------------------------
    # begin writing /etc/printcap, if not exists:
    #--------------------------------------------------------------------------
    overwrite='false'

    if [ "$do_printing" = "yes" ]
    then
        if [ ! -f "$printcapfile" ]
        then
            overwrite='true'
           {
            echo "#----------------------------------------------------------------------------"
            echo "# $printcapfile"
            echo "# automatically created by rc445.lpd"
            echo "#----------------------------------------------------------------------------"
           } >"$printcapfile"
        fi

        #----------------------------------------------------------------------
        # configuration for local parallel printers 
        #----------------------------------------------------------------------
        if [ "$OPT_LPD_PARPORT" = "yes" ]
        then
            idx='1'
            idy='0'

            while [ "$idx" -le "$LPD_PARPORT_N" ]
            do
              mkdir "$lpdir/pr$idx"
              /bin/chmod 777 "$lpdir/pr$idx"

              eval port='$LPD_PARPORT_'$idx'_IO'
              eval interrupt='$LPD_PARPORT_'$idx'_IRQ'
              eval direct='$LPD_PARPORT_'$idx'_DMA'
              port=`echo $port | tr 'A-FX' 'a-fx'`

              if [ "$interrupt" = "yes" ]
              then
                  interrupt='auto'
              else
                  interrupt='none'
              fi

              if [ "$direct" = "yes" ]
              then
                  direct='auto'
              else
                  direct='none'
              fi

              if [ -z "$parport" ]
              then
                  parport="$idy"
              else
                  parport="$parport,$idy"
              fi

              if [ -z "$io" ]
              then
                  io="$port"
              else
                  io="$io,$port"
              fi

              if [ -z "$irq" ]
              then
                  irq="$interrupt"
              else
                  irq="$irq,$interrupt"
              fi

              if [ -z "$dma" ]
              then
                  dma="$direct"
              else
                  dma="$dma,$direct"
              fi

              if [ "$overwrite" = true ]
              then
                 {
                  echo "pr$idx:\\"
                  echo " :rw:\\"
                  echo " :sh:\\"
                  echo " :lp=/dev/lp$idy:\\"
                  echo " :sd=$lpdir/pr$idx:\\"
                  echo " :fx=flp:\\"
                  echo " :sf:\\"
                  echo " :mx#0:"
                  echo
                 } >>"$printcapfile"
              fi

              idx=`expr $idx + 1`
              idy=`expr $idy + 1`
            done
        fi

        #----------------------------------------------------------------------
        # configuration for local usb printers 
        #----------------------------------------------------------------------
        if [ "$OPT_LPD_USBPORT" = "yes" ]
        then
            idx='1'
            idy='0'

            while [ "$idx" -le "$LPD_USBPORT_N" ]
            do
              mkdir "$lpdir/usbpr$idx"
              /bin/chmod 777 "$lpdir/usbpr$idx"
              /bin/mknod "/dev/usblp$idy" c 180 "$idy"

              if [ "$overwrite" = "true" ]
              then
                 {
                  echo "usbpr$idx:\\"
                  echo " :rw:\\"
                  echo " :sh:\\"
                  echo " :lp=/dev/usblp$idy:\\"
                  echo " :sd=$lpdir/usbpr$idx:\\"
                  echo " :fx=flp:\\"
                  echo " :sf:\\"
                  echo " :mx#0:"
                  echo
                 } >>"$printcapfile"
              fi

              idx=`expr $idx + 1`
              idy=`expr $idy + 1`
            done
        fi

        #----------------------------------------------------------------------
        # configuration for remote printers
        #----------------------------------------------------------------------
        if [ "$OPT_LPD_REMOTE" = "yes" ]
        then
            idx='1'

            while [ "$idx" -le "$LPD_REMOTE_N" ]
            do
              mkdir "$lpdir/repr$idx"
              /bin/chmod 777 "$lpdir/repr$idx"

              eval remoteip='$LPD_REMOTE_'$idx'_IP'
              eval remoteport='$LPD_REMOTE_'$idx'_PORT'
              eval remotequeuename='$LPD_REMOTE_'$idx'_QUEUENAME'

              if [ "$overwrite" = "true" ]
              then
                 {
                  echo "repr$idx:\\"
                  echo " :rw:\\"
                  echo " :sh:\\"
                  echo " :sd=$lpdir/repr$idx:\\"
                  echo " :fx=flp:\\"
                  echo " :sf:\\"

                  if [ -n "$remoteport" ]
                  then
                      echo " :lp=$remoteport@$remoteip:\\"
                  else
                      echo " :lp=:\\"
                      echo " :rm=$remoteip:\\"
                      echo " :rp=$remotequeuename:\\"
                  fi

                  echo " :mx#0:"
                  echo
                 } >>$printcapfile
              fi

              idx=`expr $idx + 1`
            done
        fi

        #----------------------------------------------------------------------
        # configuration for remote smb printers
        #----------------------------------------------------------------------
        if [ "$OPT_LPD_SMBREMOTE" = "yes" ]
        then
            eval debuglevel='$LPD_SMBREMOTE_DEBUGLEVEL'

            idx='1'

            while [ "$idx" -le "$LPD_SMBREMOTE_N" ]
            do
              mkdir "$lpdir/smbrepr$idx"
              mkdir "$lpdir/smbrepr$idx/acct"
              /bin/chmod 777 "$lpdir/smbrepr$idx"
              >"/dev/smbrepr$idx"

              eval server='$LPD_SMBREMOTE_'$idx'_SERVER'
              eval service='$LPD_SMBREMOTE_'$idx'_SERVICE'
              eval user='$LPD_SMBREMOTE_'$idx'_USER'
              eval password='$LPD_SMBREMOTE_'$idx'_PASSWORD'
              eval ip='$LPD_SMBREMOTE_'$idx'_IP'

              if [ "$overwrite" = "true" ]
              then
                 {
                  echo "server=$server"
                  echo "service=$service"
                  echo "username=\"$user\""
                  echo "password=\"$password\""
                  echo "ip=$ip"
                  echo "debuglevel=$debuglevel"
                 } >>"$lpdir/smbrepr$idx/acct/.config"

                 {
                  echo "smbrepr$idx:\\"
                  echo " :rw:\\"
                  echo " :sh:\\"
                  echo " :lp=/dev/smbrepr$idx:\\"
                  echo " :sd=$lpdir/smbrepr$idx:\\"
                  echo " :if=/bin/smbprint:\\"
                  echo " :af=$lpdir/smbrepr$idx/acct:\\"
                  echo " :fx=flp:\\"
                  echo " :sf:\\"
                  echo " :mx#0:"
                  echo
                 } >>"$printcapfile"
              fi

              idx=`expr $idx + 1`
            done
        fi
    fi

    #--------------------------------------------------------------------------
    # write /etc/hosts.lpd to allow remote printing via lpr
    #--------------------------------------------------------------------------
    if [ ! -f "$hostslpdfile" ]
    then
       {
        echo "#----------------------------------------------------------------------------"
        echo "# $hostslpdfile"
        echo "# automatically created by rc445.lpd"
        echo "#----------------------------------------------------------------------------"
       } >"$hostslpdfile"

        idx=1

        while [ "$idx" -le "$HOSTS_N" ]
        do
          eval name='$HOST_'$idx'_NAME'

          if [ -n "$name" ]
          then
              echo "$name" >>"$hostslpdfile"
          fi

          idx=`expr $idx + 1`
        done
    fi

    #--------------------------------------------------------------------------
    # loading drivers
    #--------------------------------------------------------------------------
    if [ "$OPT_LPD_PARPORT" = "yes" -a "$LPD_PARPORT_N" != "0" ]
    then
        $colechobin "loading parport on io $io, irq $irq, dma $dma ..." gn
        $msgcmdinfo "loading parport on io $io, irq $irq, dma $dma ..."
        do_modprobe parport_pc io=$io irq=$irq dma=$dma
        do_modprobe lp parport=$parport
    fi

    if [ "$OPT_LPD_USBPORT" = "yes" -a "$LPD_USBPORT_N" != "0" ]
    then
        case $kernel_major
	in
            2.6) do_modprobe usblp ;;
            *)   do_modprobe printer  ;;
        esac
    fi

    #----------------------------------------------------------------------------
    # start server
    #----------------------------------------------------------------------------
    $colechobin "starting lpd..." gn
    $msgcmdinfo "starting lpd..."
    /usr/sbin/lpd -l

    end_script
fi
