#!/bin/bash

. /usr/lib/lprfax/init

LPUSER=$(Get_Lpd_Var user)
LPC=$(Get_Conf_Path)

if ! su $LPUSER -c "test -w /var/lock" ; then
    echo "LPRng is currently configured to run printing scripts"
    echo "as user $LPUSER, and /var/lock is not currently writeable"
    echo "by this user.  Print-faxing on any device will not"
    echo "work until you make the necessary changes in $LPC"
    echo "and/or /etc/group and/or /var/lock"
    echo
    echo "I recommend 'chmod 1777 /var/lock', in keeping with latest"
    echo "Debian policy (potato).  Shall I do that (y or n)?"
    read f
    if [ "$f" = "y" ] ; then
	chmod 1777 /var/lock
    else
	echo OK, you can fix this later yourself.
    fi
fi


j=0
SQ=$(Server_Queues)
if [ "$SQ" != "" ] ; then
    echo "You already have a configured $MPC with $SQ as server queues"
    echo "Do you want to change it?"
    echo -n "(This will clear your existing spool directories) (y,n) >"
    read r
    if [ "$r" != "y" ] ; then
	echo "Leaving $MPC alone.  "
	echo "You can run this later with $0"
    else
	j=1
	Clear_Serverqueues
	Clear_Sendfax
	SQ=""
    fi
else
    j=1
fi

if [ $j -eq 1 ] ; then

    SER="none"
    while [ "$SER" != "done" ] ; do

	echo "Please input serial device for fax-printing (e.g. ttyS0), 'done' to end"
	echo -n ">"
	read SER
	if [ -c /dev/$SER ] ; then
	    if ! su $LPUSER -c "test -r /dev/$SER && test -w /dev/$SER" ; then
		echo "LPRng is currently configured to run printing scripts"
		echo "as user $LPUSER, and /dev/$SER is not currently accessible"
		echo "by this user.  Print-faxing on this device will not"
		echo "work until you make the necessary changes in $LPC"
		echo "and/or /etc/group and/or /dev/$SER"
		echo
		echo "I recommend 'chmod 666 /dev/$SER', as lprng and bash together"
		echo "seem to have problems maintaining group membership information over"
		echo "time.  Shall I do that?"
		read f
		if [ "$f" = "y" ] ; then
		    chmod 666 /dev/$SER
		else
		    echo OK, you can fix this later yourself.
		fi
	    fi
	    ID=$(Get_Faxid $SER)
	    if [ "$ID" = "" ] ; then
		echo "Please input the fax-id for this device (usually the phone number)"
		echo -n ">"
		read ID
		Add_Sendfax $SER $ID
	    fi
	    if [ "$SQ" = "" ] ; then
		SQ=$SER
	    else
		SQ="$SQ,$SER"
	    fi
	else
	    if [ "$SER" != "done" ] ; then
		echo /dev/$SER is not a character device
	    fi
	fi
    done
    if [ "$SQ" = "" ] ; then
	echo "You must supply at least one serial device to use this program"
	exit 1
    fi

    Add_Serverqueues $SQ

    echo "Please review the 'port' settings for $SQ in $SFC"
    echo "as well as the fax header description in $SFH"
    echo ""

fi


Add_Printcappath

Check_Server_Queues

Reload_Printer

Check_Sendfax

HP2PBM=$(dpkg -s hp2pbm 2>/dev/null | grep ^Status: | awk '{print $2}')
k=$(tempfile -m 755)
if [ "$HP2PBM" = "install" ] ; then
    cat /usr/lib/lprfax/fax-filter | sed -e 's,^#\(.*filter.*hp2hig3.*\),\1,1' \
	-e 's,^\([^#].*reject.*PCL files.*\),#\1,1' >$k
    mv $k /usr/lib/lprfax/fax-filter
else	
    cat /usr/lib/lprfax/fax-filter | sed -e 's,^\([^#].*filter.*hp2hig3.*\),#\1,1' \
	-e 's,^#\(.*reject.*PCL files.*\),\1,1' >$k
    mv $k /usr/lib/lprfax/fax-filter
fi

if [ -r /etc/samba/smb.conf ] &&
    ! grep '; begin lprfax' /etc/samba/smb.conf >/dev/null ; then
    echo "I see you are using samba.  I can setup this fax-printer for use under"
    echo "Windows if you would like."
    echo -n "(y or n)"
    read a
    if [ "$a" = "y" ] ; then
	k=$(tempfile -m 644)
	cat /etc/samba/smb.conf | awk '/^\[global\]/ {print; 
	    print "; begin lprfax"; 
	    print "message command = echo %m $(cat %s |tr -d '\''\\000'\'') >>/tmp/smbmess; rm %s";
	    print "; end lprfax"; 
	    next;} {print}' >$k
    	cat >>$k <<EOF    
; begin lprfax
[fax]
   comment = Fax printer
   path = /tmp
   printing = lprng
   browseable = yes
   printable = yes
;   public = yes
   printer = fax
   print command = /usr/lib/lprfax/wlpr %s %p %m %u &
   lpq command = /usr/lib/lprfax/wlpq %p
   lprm command = /usr/lib/lprfax/wlprm %p %j
   writable = no
   invalid users = root
; end lprfax
EOF
	mv $k /etc/samba/smb.conf
	for i in $(pidof smbd); do kill -HUP $i ; done
	if [ "$HP2PBM" = "install" ] ; then
	    echo 
	    echo "Please setup the fax printer on the Windows side as an"
	    echo "HP Laserjet II (since you have hp2pbm) for optimal performance."
	    echo "Otherwise, set it up as an Apple Laserwriter."
	    echo
	else
	    echo 
	    echo "Please setup the fax printer on the Windows side as an"
	    echo "Apple Laserwriter."
	    echo
	fi
    fi
fi

exit 0



