#!/usr/bin/perl -w 
#
# $Id: benutzerantrag_senden.pl,v 1.1 1997/06/05 09:55:15 webadm Exp $
#
#
# DESCRIPTION: benutzerantrag_senden.pl - read the results of
#    benutzerantrag_print.pl unix_verlaengern.pl and novell_verlaengern.pl
#    and send it to the appropriate user
#
# URL: none yet
#
# AUTHOR: Cord Beermann (cord@Wunder-Nett.org)
#
# Thanks to: Ralf Begemann (begemann@cc.fh-lippe.de)
#
# 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 ###
#
# Where are the files?
$DIR='/var/infosystems/www/security/login';
#
### End of Configuration ###

# include perl-modules
require Mail::Send;

# open the directory and get the filenames
opendir(LOGINDIR, $DIR) or die "opendir $DIR: $!\n";
@files = readdir LOGINDIR;
closedir LOGINDIR;

# initialize the used variables 
$dostmp = '';
$dostmp2 = '';
$unixtmp = '';
$unixtmp2 = '';

# loop over all found directory-entries
foreach $file (@files) {
    # skip if the entry isn't a regular file
    next unless -f $DIR . '/' . $file;
    # if the filename doesn't contains a dot
    if ($file =~ /^[^\.]+$/) {
	# and if it is empty
	if (-z $DIR . '/' . $file) {
	    # remove it
	    unlink($DIR . '/' . $file) or
		die("remove $DIR/$file: $!\n");
	}
    # if it ends on '.u' store the filename in $unixtmp2
    } elsif ($file =~ /\.u$/) {
	$file =~ s/\.u$//;
	$unixtmp2 .= $file . "\n";
	# remove it
	unlink($DIR . '/' . $file . '.u') or
	    die("remove $DIR/$file.u: $!\n");
    # if it ends on '.n' store the filename in $dostmp2
    } elsif ($file =~ /\.n$/) {
	$file =~ s/\.n$//;
	$dostmp2 .= $file . "\n";
	# remove it
	unlink($DIR . '/' . $file . '.n') or
	    die("remove $DIR/$file.n: $!\n");
    # if it ends on '.0'
    } elsif ($file =~ /\.0$/) {
	# open it or skip it.
	open(FILE, $DIR . '/' . $file) or next;
	# read the file
	while (<FILE>) {
	    chomp;
	    # get the data that we need.
	    ($login,$passwd,$name,$status,$number,$oe) = split /:/, $_;
	    # example-format: #create muster;Heinrich Muster;08154711^
	    if ($status eq 'Studentin / Student') {
		# print data in the needed format to $dostmp
		$dostmp .= '#create ' . $login . ';' . $name . ';' . $number . '^' . "\n";
	    } else {
		# shorten data, so that it can be used on Novel/NT
		$noe = "FB 1" if ($oe eq "Fachbereich 1 Architektur und Innenarchitektur");
		$noe = "FB 3" if ($oe eq "Fachbereich 3 Bauingenieurwesen");
		$noe = "FB 4" if ($oe eq "Fachbereich 4 Lebensmitteltechnologie");
		$noe = "FB 5" if ($oe eq "Fachbereich 5 Elektrotechnik");
		$noe = "FB 6" if ($oe eq "Fachbereich 6 Maschinenbau");
		$noe = "FB 7" if ($oe eq "Fachbereich 7 Produktions- und Fertigungstechnik");
		# print data in the needed format to $dostmp
		$dostmp .= '#create ' . $login . ';' . $name . ';;' . '^' . $status . ';' . $noe . ';Tel:' . $number ."\n";
	    }

	    # example-format: muster:Passwort:"Heinrich Muster":gid:
	    # change organisation unit to the appropriate group id
	    $uoe = "104" if ($oe eq "Fachbereich 4 Lebensmitteltechnologie");
	    $uoe = "105" if ($oe eq "Fachbereich 5 Elektrotechnik");
	    $uoe = "106" if ($oe eq "Fachbereich 6 Maschinenbau");
	    $uoe = "107" if ($oe eq "Fachbereich 7 Produktions- und Fertigungstechnik");
	    $uoe = "110" if ($oe eq "Zentralverwaltung");
	    $uoe = "111" if ($oe eq "Hochschulbibliothek");
	    $uoe = "120" if ($oe =~ /^andere Hochschule/);
	    $uoe = "151" if ($oe eq "Fachbereich 3 Bauingenieurwesen");
#	    $uoe = "0815" if ($oe eq "Fachbereich 1 Architektur und Innenarchitektur");
#	    $uoe = "4711" if ($oe eq "Zentrale Einrichtungen");

	    # print data in the needed format to $unixtmp
	    $unixtmp .= $login . ':' . $passwd . ':"' . $name . '":' . $uoe . ':' . "\n";
	    # read only the first line. (We don't expect that there is a second)
	    last;
	}
	# close the data file
	close FILE;
	# rename the file from user.0 to user.1
	($file,$i) = split(/\./,$file);
	rename($DIR . '/' . $file . '.0' , $DIR . '/' . $file . '.1') or
	    die("rename $DIR/$file.0 $DIR/$file.1: $!\n");
    # if none of the above diretives matched and the file ends on 'dot number'
    } elsif ($file =~ /\.\d$/) {
	# rotate the files with *.1-*.8 to *.2-*.9 and delete *.9 
	($file,$i) = split(/\./,$file);
	if ($i < 9) {
	    rename($DIR . '/' . $file . '.' . $i, $DIR . '/' . $file . '.' . ($i + 1)) or
		die("rename $DIR/$file.$i $DIR/$file" . ($i+1) . ": $!\n");
	} else {
	    unlink($DIR . '/' . $file . '.' . $i) or
		die("remove $DIR/$file.$i: $!\n");
	}
    } else {
	# print a warning if we got somethin else.
	warn("doesn't match: $file");
    }
}

# if we loaded somethin in $unixtmp do this:
if ($unixtmp) {
    # open a file for writing
    open(UNIX, ">>/var/infosystems/www/security/unix.antrag") ||die("open
	/var/infosystems/www/security/unix.antrag");
    # open a mail for sending
    $stefan = new Mail::Send Subject=>'Benutzerantrag',To=>'hegger@cc.fh-lippe.de';
    $stefan->cc('cord@cc.fh-lippe.de');
    $unix = $stefan->open;
    # print the data we've got to file and to mail
    print UNIX $unixtmp;
    print $unix $unixtmp;
    # close mail and file (means sending the mail)
    $unix->close;
    close(UNIX);
}

# if we loaded somethin in $unixtmp2 do this:
if ($unixtmp2) {
    # open a file for writing
    open(UNIX, ">>/var/infosystems/www/security/unix.verlaenger") ||die("open
	/var/infosystems/www/security/unix.verlaenger");
    # open a mail for sending
    $stefan = new Mail::Send Subject=>'Zugangsverlaengerung',To=>'hegger@cc.fh-lippe.de';
    $stefan->cc('cord@cc.fh-lippe.de');
    $unix = $stefan->open;
    # print the data we've got to file and to mail
    print UNIX $unixtmp2;
    print $unix $unixtmp2;
    # close mail and file (means sending the mail)
    $unix->close;
    close(UNIX);
}

# if we loaded somethin in $dostmp do this:
if ($dostmp) {
    # open a mail for sending
    $michael = new Mail::Send Subject=>'Benutzerantrag', To=>'michael@cc.fh-lippe.de';
    $michael->cc('cord@cc.fh-lippe.de');
    $dos = $michael->open;
    # print the data we've got to mail
    print $dos $dostmp;
    # close mail (means sending the mail)
    $dos->close;
}

# if we loaded somethin in $dostmp2 do this:
if ($dostmp2) {
    # open a mail for sending
    $michael = new Mail::Send Subject=>'Zugangsverlaengerung', To=>'michael@cc.fh-lippe.de';
    $michael->cc('cord@cc.fh-lippe.de');
    $dos = $michael->open;
    # print the data we've got to mail
    print $dos $dostmp2;
    # close mail (means sending the mail)
    $dos->close;
}

