#!/usr/local/bin/perl
#
# wget --output-dcoument=/dev/stdout ... | pot.pl
# 
# Procedure:
# http://www.intranet.ens.fr/Actualites.nsf?Opendatabase
#   Puis dernier "-Menu du pot" done qq choses comme
# http://www.intranet.ens.fr/Actualites.nsf/2bf3d43fcb97af80c12568d400408571/c00cdf43cb5ef80ec1256ccb00469ab5?OpenDocument
#   Puis traiter ceci.
# voir miam.info pour le detail des operations...
#


$baseurl="http://www.intranet.ens.fr/Actualites.nsf?Opendatabase";
$Menu="Menu du pot";
$MenuEnd="<table.*>";
$Menu2="<p align=\"center\">";
$Menu2End="</table>";

sub geturl1 {
    $i=0;
    open(FLUX, "wget -q --output-document=/dev/stdout http://www.intranet.ens.fr/Actualites.nsf?Opendatabase |");
    while (<FLUX>) {
        if ($_=~m/$Menu/) {
    print $_;
            while (<FLUX>) {
    print $_;
                if ($_=~m/$MenuEnd/i) {
                    last;
                } else {
                    $buffer.=$_;
                    $_=~m/((?<=HREF=\").*(?=\" TARGET))/;
                    if ($1 ne "") {
                        $i=$i+1;
                        @url[$i]=$1;
                        #$url=$1;
                    }
                    $_=~m/((?<=self\">).*(?=<\/A><\/FONT>))/;
                    if ($1 ne "") {
                        @date[$i]=$1;
                        #$date=$1;
                    }
                }
            }
        }
    }
    close(FLUX);

    return @url, @date;
}


sub geturl2 {
    my @argv=shift @_;

    open(FLUX, "wget -q --output-document=/dev/stdout http://www.intranet.ens.fr".$argv[0]." |");
    while (<FLUX>) {
        if ($_=~m/$Menu2/) {
            $_=~m/((?<=<p align=\"center\">)[a-zA-Z 0-9]*(?=<\/p>))/;
            $temp=$1;
            $temp=~s/(?<=\ ).*(?=$)//g;
            $a=substr($temp, 0, 1); $temp=substr($temp, 1);
            $temp=~tr/A-Z/a-z/;
            $temp=$a.$temp;
            print "\n".$temp."\n";
            $i=0;
            $menuA=""; $menuB="";
            while (<FLUX>) {
                if ($_=~m/$Menu2End/) {
                    last;
                } else {
                    if ($_=~m/\s<td.*>/) {
                        $i=$i+1;
                        if ($_=~m/<td.*>.*<\/td>/) {
                            $ligne=$_;
                        } else {
                            $ligne=<FLUX>;
                            $ligne=$_.$ligne;
                            $ligne=~s/\n//;
                        }
                        $ligne=~s/[ 	]{2,}/ /g;
                        $ligne=~s/&[a-z0-9]*;//;
                        #print "\\".$ligne;
                        #$ligne=~m/((?<=<td).*(?=<\/td>))/;
                        $ligne=~m/((?<=>)[^><]*(?=<\/))/;
                        $temp=$1;
                        $temp=~s/^ //;
                        if ($i%2==1) {
                            $menuA.=$temp."\n";
                        } else {
                            $menuB.=$temp."\n";
                        }
                        #print "plat: ".$1."\n";
                    }
                }
            }
            $menuA=~s/\n\n/\n/;
            $menuB=~s/\n\n/\n/;
            print $menuA;
            print "\n".$menuB;
        }
    }
    close(FLUX);
}

sub main {
    (@urls, @dates)=geturl1();
    print "+ +".@urls[0]."- -\n";
    print "+ +".$dates."- -\n";
}

main();
