#! /bin/sh -e
## 
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description. 

[ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
patch_opts="${patch_opts:--f --no-backup-if-mismatch}"

if [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi  
case "$1" in
       -patch) patch $patch_opts -p1 < $0;;
       -unpatch) patch $patch_opts -p1 -R < $0;;
        *)
                echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
		exit 1;;
esac            

exit 0
@DPATCH@
--- mgetty-1.1.30/fax/faxrunqd.in.orig	2003-09-13 11:04:12.000000000 +0000
+++ mgetty-1.1.30-5.1/fax/faxrunqd.in	2003-09-13 11:12:47.000000000 +0000
@@ -12,6 +12,10 @@
 my $rcs_id='RCS: $Id: faxrunqd.in,v 1.70 2005/02/17 13:52:33 gert Exp $';
 #
 # Change Log:
+#
+# Debian 1.1.30-5.1 2003/07/20  Andreas Barth <aba@not.so.argh.org>
+# Can specify group per commandline.
+#
 # $Log: faxrunqd.in,v $
 # Revision 1.70  2005/02/17 13:52:33  gert
 # do full "queue readdir()" only once per 180 seconds now, but use "stat(.)" to
@@ -73,6 +77,11 @@
 # a "low prio" job for phone number 123 could be attached to a high prio
 # job to 123, and thus be sent before a high prio job to 456)
 #
+# Debian 1.1.28-4 2002/11/27 18:32:23 Wolfgang Sourdeau <was@debian.org>
+# Quit quietly when received signal TERM or HUP since it is expected the
+# user knows what is happening already. The quit message is still written
+# into the logfile.
+#
 # Revision 1.56  2002/01/04 17:52:42  gert
 # pass sendfax exit code as 2nd argument to success/failure program
 #
@@ -83,6 +92,9 @@
 # Revision 1.54  2001/12/16 14:26:25  gert
 # stop queue handling if a file named 'stop' exists
 #
+# Add a -D option to allow for running in the background as a Daemon
+#       Philip Hands <phil@hands.com>
+#
 # Revision 1.53  2000/08/06 14:28:37  gert
 # go from using $fax_spool_out/.last_run to VARRUNDIR/faxqueue_done
 #
@@ -157,6 +169,8 @@
 #
 #
 require 5.004;
+
+use Fcntl;
 use strict;
 use POSIX;
 use IO::Handle;
@@ -174,10 +188,10 @@
 $mail='@MAILER@';
 
 $faxrunq_cf='@CONFDIR@/faxrunq.config';
-$fax_acct='@FAX_SPOOL@/acct.log';
-$faxrd_log='@FAX_SPOOL@/faxrunqd.log';
+$fax_acct='/var/log/mgetty/fax/acct.log';
+$faxrd_log='/var/log/mgetty/fax/faxrunqd.log';
 
-$faxrd_pid='@FAX_SPOOL_OUT@/faxrunqd.pid';
+$faxrd_pid='/var/run/mgetty-fax/faxrunqd.pid';
 $last_run='@FAX_SPOOL_OUT@/faxqueue_done';
 
 
@@ -218,14 +232,16 @@
 # command line options
 #
 my $saved_cli=join( " ", @ARGV );	# print command line to LOG later
-use vars qw / $opt_d $opt_v $opt_V $opt_l $opt_u /;
+use vars qw / $opt_d $opt_v $opt_V $opt_l $opt_u $opt_g $opt_D /;
 $opt_d = 0;					# debug
 $opt_v = 0;					# verbose
 $opt_V = 0;					# print version number
 $opt_l = '';					# ttys to use
 $opt_u = '';					# user id to setuid() to
-getopts( 'dvVl:u:' ) || 
-    die "Valid options: -d (debug), -v (verbose), -l tty<n>, -u uid, -V (version)\n";
+$opt_g = '';					# group id to setgid() to
+$opt_D = 0;					# daemon
+getopts( 'DdvVl:u:g:' ) || 
+    die "Valid options: -D (daemon), -d (debug), -v (verbose), -l tty<n>, -u uid, -g uid, -V (version)\n";
 
 if ( $opt_d ) { $opt_v=1; }
 
@@ -236,32 +252,63 @@
 mgetty+sendfax by Gert Doering
 $rcs_id
 
-config file read from '$faxrunq_cf'
+config read from '$faxrunq_cf'
 
 EOF
     exit 0;
 }
 
+my $uid=$>;
+my $gid=$);
+
 if ( $opt_u ne '' )	# set user ID to $opt_u
 {
-    my ( $uid, $gid ) = ( getpwnam( $opt_u ) )[2,3];
+    ( $uid, $gid ) = ( ( $opt_u =~ /^[0-9]+$/ )
+			    ? getpwuid( $opt_u )
+			    : getpwnam( $opt_u ) )[2,3];
 
     if ( !defined($uid) || !defined($gid) )
 		{ die "$0: no such user: '$opt_u'\n"; }
 
     print "change user ID to '$opt_u' (numeric uid: $uid, gid: $gid)\n"
 								if $opt_d;
-    $( = $) = $gid;
-    $< = $> = $uid;
-
-    if ( $> != $uid || $) != $gid )
-		{ die "$0: can't set uid to $uid / gid to $gid: $!\n"; }
 }
+
+if ( $opt_g ne '' )
+    {
+	$gid = ( ( $opt_g =~ /^[0-9]+$/ )
+		   ? getgrgid( $opt_g )
+		   : getgrnam( $opt_g ) )[2];
+	die "$0: no such group: '$opt_g'\n" unless defined ($gid);
+    }
+
+$( = $) = $gid;
+$< = $> = $uid;
+
+if ( $> != $uid || $) != $gid )
+	{ die "$0: can't set uid to $uid / gid to $gid: $!\n"; }
+
 if ( $> == 0 )		# root check
 {
     print STDERR "$0: running with root privileges is not recommended\n";
 }
 
+# fork as a daemon if invoked with -D
+if ( $opt_D ) {
+  my $pid = fork ;
+  die "Cannot fork: $!" unless defined $pid ;
+  if (0 == $pid) {
+    # Child process: become leader of a new session, lose STDIN, log STDOUT/ERR
+    POSIX::setsid();
+    umask(022);
+    open(STDIN, "/dev/null") ;
+    open(STDOUT, ">&LOG") ;
+    open(STDERR, ">&LOG") ;
+  } else {
+    exit 0;     # parent process
+  }
+}
+
 #
 # startup... write PID file, make sure no other faxrunqd runs
 #
@@ -296,7 +343,7 @@
 
 $SIG{USR1} = \&signal_handler_USR1;		# roll log file
 my $roll_log_file_requested = 0;
-my $roll_level=3;				# keep 3 old files around
+my $roll_level=7;				# keep 7 old files around
 
 $SIG{HUP}  = \&signal_handler_HUP;		# graceful exit
 my $graceful_exit_requested = 0;
@@ -396,7 +443,7 @@
 #
 # open log file
 #
-open( LOG, ">>$faxrd_log" ) ||
+sysopen( LOG, "$faxrd_log", O_CREAT | O_APPEND | O_WRONLY, 0640 ) ||
 	    die "can't write log file '$faxrd_log'";
 LOG->autoflush(1);
 print LOG "\n" . localtime() .": faxrunqd starting, pid=$$\n";
@@ -604,7 +651,7 @@
 	$roll_log_file_requested=0;
 
 	# start new
-	open( LOG, ">$faxrd_log" ) ||
+	sysopen( LOG, "$faxrd_log", O_CREAT | O_APPEND | O_WRONLY, 0640 ) ||
 		    die "can't re-open log file '$faxrd_log'";
 	LOG->autoflush(1);
 	print LOG localtime() .": -- new log file started --\n";
@@ -1736,7 +1783,8 @@
 {
 my $sig = shift;
 
-    print "\nfaxrunqd: signal handler: got signal $sig, goodbye...\n";
+    print "\nfaxrunqd: signal handler: got signal $sig, goodbye...\n"
+	if ($sig ne 'TERM' && $sig ne 'HUP' && $sig ne 'USR2');
     print LOG "\nfaxrunqd: signal handler: got signal $sig, goodbye...\n";
 
     # save tty statistics
