#!/bin/bash

. /usr/lib/lprfax/init

LPUSER=$(Get_Lpd_Var user)
GROUP=$(Get_Lpd_Var group)
PP=$(Get_Lpd_Var printcap_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 /etc/lpd.conf"
    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 /etc/lprfax/printcap with $SQ as server queues"
    echo -n "Do you want to change it? (This will clear your existing spool directories) (y,n) >"
    read r
    if [ "$r" != "y" ] ; then
	echo "Leaving /etc/lprfax/printcap alone.  "
	echo "You can run this later with /usr/lib/lprfax/lprfaxconfig"
    else
	j=1
	for i in $(echo $SQ | tr ',' ' '); do
	    rm -rf /var/spool/lpd/$i
	done
	SQ=""
	k=$(tempfile -m 644)
	cat /etc/lprfax/printcap | awk '/faxint/ {i=1} /:sv=/ {if (i) j=1} {if (!j) print}' >$k
	mv $k /etc/lprfax/printcap
    fi
else
    j=1
fi

if [ $j -eq 1 ] ; then

    SER=""
    while [ "$SER" = "" ] || [ "$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 /etc/lpd.conf"
		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
		k=$(tempfile -m 644)
		cat /etc/mgetty/sendfax.config | awk '{
		    print
		    if ($1 == "port" && $2 == "'$SER'") {
			i=1;
			printf("# Added by lprfax\n\tfax-id %s\n","'$id'");
		    }
		    } END {if (!i) printf("# Added by lprfax\nport %s\n# Added by lprfax\n\tfax-id %s\n\n","'$SER'","'$id'")}' >$k
		mv $k /etc/mgetty/sendfax.config
	    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
    echo -e "\t:sv=$SQ" >>/etc/lprfax/printcap
    echo >>/etc/lprfax/printcap
    for i in $(echo $SQ | tr ',' ' '); do
	echo $i:tc=.commonttyS >>/etc/lprfax/printcap
	echo >>/etc/lprfax/printcap
    done
    chmod 644 /etc/lprfax/printcap

    echo "Please review the 'port' settings for $SQ in /etc/mgetty/sendfax.config"
    echo "as well as the fax header description in /etc/mgetty/faxheader"

fi



j=0
for i in $(echo $PP | tr ':' ' '); do
    if [ "$i" = "/etc/lprfax/printcap" ] ; then
	j=1;
    fi
done

if [ "$j" = "0" ] ; then
    SRCH="^[^#]*\<printcap_path[:space:]*=[:space:]*"
    if grep "$SRCH" /etc/lpd.conf >/dev/null; then
	k=$(tempfile -m 644)
	cat /etc/lpd.conf | sed "s,\($SRCH\)\([[:alnum:][:punct:]]*\),\1/etc/lprfax/printcap:\2,1" >$k
	mv $k /etc/lpd.conf
	chmod 644 /etc/lpd.conf
    else
	if [ "$PP" != "" ] ; then
	    PP=":$PP"
	fi
	echo printcap_path=/etc/lprfax/printcap$PP >>/etc/lpd.conf
    fi
fi

checkpc -f 

if [ -r /var/run/lprng/lpd.printer ] ; then
    kill -HUP $(cat /var/run/lprng/lpd.printer)
fi

exit 0


