#! /bin/bash
# Copyright (c) 2004 SUSE LINUX Products GmbH, Nuernberg, Germany
#
# Author: Johannes Meixner <http://www.suse.de/feedback>
#
# System startup script for setting scanner permissions in /dev/
#
### BEGIN INIT INFO
# Provides:       sane-dev
# Required-Start: $remote_fs $syslog
# Should-Start:   hal
# Required-Stop:  $remote_fs $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Setting scanner permissions in /dev/.
### END INIT INFO

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

store_dir=/var/lib/sane
store_file=$store_dir/devices
maximum_wait=30

if [ "$1" != "status" -a "$( id -u )" != "0" ]
then echo "You must be root (except for \"$0 status\")."
     rc_failed 4
     rc_status
     rc_exit
fi

case "$1" in
    start)
        if [ -x /usr/bin/scanimage ]
        then echo -n "Searching for configured scanners in SANE."
             [ -d $store_dir ] || mkdir -m 755 $store_dir
             [ -f $store_file ] && mv $store_file ${store_file}.old
             /usr/bin/scanimage -f '%d,' 1>${store_file}.raw 2>/dev/null &
             scanimagePID=$!
             for i in $( seq $maximum_wait )
             do grep scanimage /proc/$scanimagePID/cmdline &>/dev/null || break
                sleep 1
                echo -n "."
             done
             if grep scanimage /proc/$scanimagePID/cmdline &>/dev/null
             then echo -n " Aborted."
                  kill -9 $scanimagePID &>/dev/null
                  rc_failed 1 
                  rc_status -v
                  rc_exit
             fi
             cat ${store_file}.raw | tr ',' '\n' | grep -v 'net:' | tr ':' '\n' | grep '^/dev/' | sort -u >$store_file
             if [ -s $store_file ]
             then echo -e "\nSetting scanner access mode to rw-rw-rw- for"
                  # to be safe only character device files are changed (e.g. harddisks are not changed)
                  for d in $( cat $store_file )
                  do [ -c $d ] && chmod 666 $d && echo -n "$d "
                  done
             else echo -en "\nNo scanner device files found."
             fi
        else echo -n "Cannot execute /usr/bin/scanimage"
             rc_failed 5
             rc_status -v
             rc_exit
        fi
        # Remember status and be verbose
        rc_status -v
        ;;
    stop)
        if [ -s $store_file ]
        then echo "Resetting scanner access mode to rw-r----- for"
             # to be safe only character device files are changed (e.g. harddisks are not changed)
             for d in $( cat $store_file )
             do [ -c $d ] && chmod 640 $d && echo -n "$d "
             done
        else echo -n "There are no scanner device files in $store_file"
        fi
        # Remember status and be verbose
        rc_status -v
        ;;
    try-restart)
        ## Stop the service and if this succeeds (i.e. the 
        ## service was running before), start it again.
        ## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
        $0 status >/dev/null &&  $0 restart
        # Remember status and be quiet
        rc_status
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start
        # Remember status and be quiet
        rc_status
        ;;
    force-reload)
        ## Signal the daemon to reload its config. Most daemons
        ## do this on signal 1 (SIGHUP).
        ## If it does not support it, restart.
        rc_failed 3 
        rc_status -v
        ;;
    reload)
        ## Like force-reload, but if daemon does not support
        ## signalling, do nothing (!)
        # If it supports signalling:
        rc_failed 3
        ;;
    status)
        if [ -s $store_file ]
        then echo "Scanner access mode is"
             # only character device files are of interest
             for d in $( cat $store_file )
             do [ -c $d ] && ls -l $d | cut -b 2-10 | tr -d '\n' && echo " for $d"
             done
        else echo "There are no scanner device files in $store_file"
        fi
        rc_status
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 1
        ;;
esac
rc_exit
