#!/usr/bin/perl -w 
#
# $Id: benutzerantrag_print.pl,v 1.1 1997/05/29 11:05:15 webadm Exp $
#
#
# DESCRIPTION: benutzerantrag_print.pl - read CGI-Input from
#    benutzerantrag_check.pl, send data to a printer and save into a file for
#    further processing
#
# URL: none yet
#
# AUTHOR: Cord Beermann (cord@Wunder-Nett.org)
#
# Thanks to: Ralf Begemann (begemann@cc.fh-lippe.de)
#	     Christian Tacke (Elrond@Wunder-Nett.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# A Perl script is "correct" if it gets the job done before your boss fires
# you.
# ('Programming Perl Second Edition' by Larry Wall, Tom Christiansen
#   & Randal. L. Schwartz)
#
# Bugs and shortcomings
# none yet
#
# Todo
# * write instructions
#
### Configuration ###
#
#
### End of Configuration ###

# include perl-modules
use CGI::Carp;
use CGI qw(:standard);
use CGI qw(:shortcuts);

# initialize the CGI-Module and load the form-input
$query = new CGI;
$query->autoEscape(undef);

# Print out a content-type for HTTP/1.0 compatibility and the HTML-Head
print $query->header;
print $query->start_html(-"title"=>'Benutzerantrag wird gedruckt.',
                         -"author"=>'gnats@thalassa.cc.fh-lippe.de',
                         -"base"=>'true',
                         -"text"=>'#000000',
                         -"BGCOLOR"=>'#FFFFF7');
 
print "\n";

print(h1("Benutzerantrag f&uuml;r die zentralen Rechneranlagen der FH Lippe"),
      hr
      );

# check the input fields for correctness. If not correct add a specific
#    number to $error
$error = 0;
if (!defined($query->param('raum'))) {
    $error += 1;
} elsif ($query->param('raum') =~ /342/) {
    $printer='fs3600-342';
} elsif ($query->param('raum') =~ /343/) {
    $printer='fs1550-343';
} elsif ($query->param('raum') =~ /344/) {
    $printer='fs1600-344';
} elsif ($query->param('raum') =~ /364/) {
    $printer='fs1600-364';
} elsif ($query->param('raum') =~ /304/) {
    $printer='ps810-304';
} else {
    $error += 1;
}

$error += 2 unless ($query->param('name'));
$error += 4 unless ($query->param('status'));
$error += 8 unless ($query->param('oe'));
$error += 16 unless ($query->param('matrikel') || $query->param('telefond'));
$error += 32 unless ($query->param('login'));
$error += 64 unless ($query->param('password'));
$error += 128 unless ($query->param('plz'));
$error += 256 unless ($query->param('ort'));

if (defined($query->referer)) {
    # check if we were called from the expected URL
    unless ($query->referer eq
	    'http://www.fh-lippe.de/local-bin/benutzerantrag_check.pl') {
	$error += 1024;
    }
} else {
    $error += 512;
}

if (defined($query->param('login')) && !-e '/var/infosystems/www/security/login/' . $query->param('login')) {
    $error += 2048;
} elsif (defined($query->param('login')) && (stat('/var/infosystems/www/security/login/' . $query->param('login')))[1] eq $query->param('inode')) {
    # save the needed data into a file
    open(LOGIN, '>/var/infosystems/www/security/login/' .
	 $query->param('login')) or die('open
	/var/infosystems/www/security/login/' . "$query->param('login'): $!");
    print LOGIN $query->param('login') . ':' . $query->param('password') . ':' . $query->param('name') . ':';
    if ($query->param('status') eq 'Studentin / Student') {
	print LOGIN $query->param('status') . ':' . $query->param('matrikel'). ':';
    } else {
	print LOGIN $query->param('status') . ':' . $query->param('telefond'). ':';
    }
    print LOGIN $query->param('oe') . "\n";
    close(LOGIN);
} else {
    $error += 4096;
}

if ($error < 1) {
    # parse documents before printing
    for $i ('merkzettel.ps','benutzerantrag.ps') {
# debugging
#	open(PRINTER, ">/tmp/$i") or die("open lp -c -s -d $printer: $!");
	# open Printer
	open(PRINTER, "|lp -c -s -d $printer") or die("open lp -c -s -d $printer: $!");
	# open POSTSCRIPT
	open(POSTSCRIPT, "/var/infosystems/www/webroot/fhl/dvz/$i") or
	    die("open /var/infosystems/www/webroot/fhl/dvz/$i:  $!");
	while (<POSTSCRIPT>) {
	    # replace keywords with given values
	    s,\(NAME\),'(' . $query->param('name') . ')',eg;
	    s,\(LOGIN\),'(' . $query->param('login') . ')',eg;
	    s,\(EMAIL\),'(' . $query->param('login') . '@cc.fh-lippe.de)',eg;
	    if ($query->param('status') eq 'Studentin / Student') {
		s,\(STATUS\),'(' . $query->param('status') . ')',eg;
		s,\(MATEL1\),'(Matrikelnummer:)',eg;
		s,\(MATEL2\),'(' . $query->param('matrikel') . ')',eg;
	    } else {
		s,\(STATUS\),'(' . $query->param('status') . ')',eg;
		s,\(MATEL1\),'(Telefon (dienstlich):)',eg;
		s,\(MATEL2\),'(' . $query->param('telefond') . ')',eg;
	    }
	    s,\(OE\),'(' . $query->param('oe') . ')',eg;
	    s,\(STRASSE\),'(' . $query->param('strasse') . ')',eg;
	    s,\(PLZ,'(' . $query->param('plz'),eg;
	    s,ORT\),$query->param('ort') . ')',eg;
	    s,\(TELEFONP\),'(' . $query->param('telefonp') . ')',eg;
	    print PRINTER;
	}
	# close PRINTER. It will (hopefully) now start to print
	close(PRINTER);
	close(POSTSCRIPT);
    }

# All correct, give them the inputs back
# tell where he can find his prints
    print(p('Ihr Antrag und ein Merkblatt werden nun in ' .
	    $query->param('raum') . ' gedruckt. Bitte holen Sie Sich die
	    Ausdrucke aus dem dortigen Drucker und lesen Sie Sich das
	    Merkblatt sorgf&auml;ltig durch.'));
    print(h2('Probleme?'));
    print(p('Wenn Ihr Antrag und das Merkblatt nicht ausgedruckt werden,
	    suchen Sie bitte die',
	    a({href=>'/fhl/dvz/loesungen/misc/benutzerberatung.html'}, 
	      'Benutzerberatung' ) , 'auf.' ));
} else { 
    # something went wrong
    print(h2('Es ist ein Fehler aufgetreten'));
    # print obscure number for debugging
    print(p('Interner Fehler: ' . $error));
    print(p('Bitte wenden Sie Sich an die',
	  a({href=>'/fhl/dvz/loesungen/misc/benutzerberatung.html'},
              'Benutzerberatung'
	    ) . '.' ));
}

print(hr, address('Autor: ' . a({href=>'http://Cord.de/'}, 'Cord Beermann'
				)
		  )
      );
