#!/usr/local/bin/perl

# OA95 <Olivier.Aubert@enst-bretagne.fr>
# Convertit le fichier /etc/aliases au format HTML

# This script is distributed under the GNU General Public License
# version 2.1 or later. See http://www.fsf.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.1, 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.

# A copy of the GNU General Public License can be obtained from this
# program's author (send electronic mail to the above address) or from
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# or from the Free Software Foundation website: http://www.fsf.org/


# Ligne a partir de laquelle on commence a traiter les listes (regexp)
$first_line = "Partie 2 : Listes de diffusion";

# Initialisation des listes
%fichier = ();
%items = ();
%cat = ();
@listes = ();
@categories = ();
$current_category = "";

# Parametres concernant les fichiers a creer
# Ils sont de la forme : 
# $filename = $basename.$counter.".html"
$basename = "alias";
$counter = 0;

# Chemin vers les scripts appeles
$formmailscript = "http://nebuleuse.enst-bretagne.fr/scripts-eleves/aubertBin/formmail.pl";

# Lignes affichees a chaque bas de page
$bottom_lines =<<"EOF";
<P><IMG SRC="http:/icones/degrade.gif"><P>
Retour &agrave; l\'<A HREF="index.html">index des listes</A>.<BR>
Retour &agrave; la <A HREF="http://nebuleuse.enst-bretagne.fr/">page des &eacute;l&egrave;ves</A>.<BR>
Retour &agrave; la <A HREF="http://www.enst-bretagne.fr/">page d'accueil</A> de T&eacute;l&eacute;com Bretagne.
<HR><H5>Fichier converti par <A HREF="http://nebuleuse.enst-bretagne.fr/~aubert/perl/">alias2html.pl</A><P>


EOF


#
# Debut du script
#

$fichier = $ARGV[0] || &usage;

open(F, "<$fichier") || die "Cannot open $fichier\n";

while (<F>)
{
    last if (/$first_line/o);
}

while (<F>)
{
    chop;

    if (/^\#\#?\s+(.+)$/)
    {
	$current_category = $1;
	push(@categories, $current_category);
	$fichier{$current_category} = $basename.$counter.".html";
	$counter++;
	next;
    }
    
    if (/^([\w\-\.]+)\:\s+(.+)$/)
    {
	$current_list = $1;
	$items{$current_list} = $2;
	$cat{$current_list} = $current_category;
	push(@listes, $current_list);
	next;
    }

    if (/^\s+(.+)$/)
    {
	$items{$current_list} .= $1;
	next;
    }
}
close(F);

$entete = "[ ";

for $c ( @categories )
{
    $entete .=<<"EOF";
<A HREF="$fichier{$c}">$c</A> |
EOF
}

chop($entete);			# Pour le dernier <CR>
chop($entete);			# Pour le dernier |

$entete .= "]";

$current_category = "";

open(F, ">/dev/null") || die "Cannot open /dev/null.\n";

for $l ( @listes )
{
    if ($cat{$l} eq $current_category)
    {
	&affiche_liste($l);
    }
    else
    {
	print F "</UL>\n";
	print F $bottom_lines;
	close(F);
	$current_category = $cat{$l};
	open(F, ">$fichier{$current_category}") || die "Cannot open $fichier{$current_category} for writing.\n";
	print F <<"EOF";
<TITLE>Liste de diffusion - $current_category</TITLE>
<H1>Liste de diffusion - $current_category</H1>
<P><IMG SRC="http:/icones/degrade.gif"><P>
$entete
<HR>
<UL>
EOF
    &affiche_liste($l);
    }
}

close(F);

# Construire l'index
open(F, ">index.html") || die "Cannot create index.html\n";

print F <<"EOF";
<TITLE>Listes de diffusion</TITLE>
<H1>Listes de diffusion</H1>
<IMG SRC="http:/icones/degrade.gif"><P>
Vous trouverez ci-dessous l\'ensemble des listes de diffusion d&eacute;finies &agrave; ce jour sur le serveur central de messagerie (melimelo ou tb).<p>

Cat&eacute;gories :<BR>
<UL>
EOF

for $c ( @categories )
{
    print F <<"EOF";
<LI><A HREF="$fichier{$c}">$c</A>
EOF
}

print F <<"EOF";
</UL>
$bottom_lines
EOF

close(F);

print "File ", $fichier, " converted successfully.\n";
print "Files created : ", $basename, "0.html to ", $basename, $counter, ".html\n";
print "and index.html\n\n";

exit(0);


sub affiche_liste
{
    local($nom) = shift;
    local(@elements);
    
    die "Erreur: alias(".$nom.") n\'est pas defini\n" if (!defined($items{$1}));

#    print "=" x 30, "> appel avec ", $k, "\n";
    @elements = split(/\s*,\s*/, $items{$nom});
    
    print F <<"EOF";
<DD><B><A NAME="$nom"><A HREF="$formmailscript?$nom\@enst-bretagne.fr">$nom</A> :</B>
<DL>
EOF

    for $adresse ( @elements )
    {
	if (defined($items{$adresse}))
	{
	    # C'est une liste imbriquee dans une autre
	    print F <<"EOF";
<DD><I><A HREF="$fichier{$cat{$adresse}}#$adresse">$adresse</A></I>
EOF
        }
	else
	{
	    $adresse .= "\@enst-bretagne.fr" if ($adresse !~ /\@/);
	    if ($adresse !~ /enst-bretagne.fr/)
	    {
		if ($adresse =~ /\@(enstb|melimelo)$/)
		{
		    $adresse .= ".enst-bretagne.fr";
		    print F <<"EOF";
<DD><A HREF="$formmailscript?$adresse">$adresse</A>
EOF
                }
		else
		{
		    print F <<"EOF";
<DD>$adresse
EOF
                }
	    }
	    else
	    {
		    print F <<"EOF";
<DD><A HREF="$formmailscript?$adresse">$adresse</A>
EOF
            }
	}
    }
    print F "</DL>\n";
}

sub usage
{
    print <<"EOF";
Usage:
    alias2html.pl <fichier alias>

Le programme cree plusieurs fichiers, nommes ${basename}0.html, ${basename}1.html,etc... dans le repertoire courant, ainsi que le fichier index.html.

EOF
    exit(0);
}
