#!/usr/bin/perl -w
# Pretigi TTT-ejon.

my $SIST = "utf";

my $APOS = 0;
my $CIRK = 0;
my $HTML = 0;
my $LAT  = 0;
my $UTF  = 0;
my $X    = 1;
my $ZAM  = 0;

my $snomo = $SIST . "2";

my $anomo = 'apos';
my $cnomo = 'cirk';
my $hnomo = 'html';
my $lnomo = 'lat';
my $unomo = 'utf';
my $xnomo = 'x';
my $znomo = 'zam';

print "komencas...\n";
sub cxefa {
    my $dujo = $_[0];
    chdir($dujo);
    while (<*.html>) {
	my $dos = $_;
	next if (m@\..\.html@);
	print "$_\n";
	&horstamp($_);
	$_ = $dos;
	s@\.html@@;
	if ($APOS) {
	    $at = `$snomo$anomo $_.html`;
	    $at =~ s@a href=\"(.{4}[^:].*?).html\"@a href=\"$1.a.html\"@g;
	    open(ADOS,">$_.a.html");
	    print ADOS $at;
	    close ADOS;
	}
	if ($CIRK) {
	    $ct = `$snomo$cnomo $_.html`;
	    $ct =~ s@a href=\"(.{4}[^:].*?).html\"@a href=\"$1.c.html\"@g;
	    open(CDOS,">$_.c.html");
	    print CDOS $ct;
	    close CDOS;
	}
	if ($HTML) {
	    $ht = `$snomo$hnomo $_.html`;
	    $ht =~ s@a href=\"(.{4}[^:].*?).html\"@a href=\"$1.h.html\"@g;
	    open(HDOS,">$_.h.html");
	    print HDOS $ht;
	    close HDOS;
	}
	if ($LAT) {
	    $lt = `$snomo$lnomo $_.html`;
	    $lt =~ s@\"Content-Type\" content=\"(.*?);.*?\"@\"Content-Type\" content=\"$1; charset=iso-8859-3\"@;
	    $lt =~ s@a href=\"((?!http).*?)\.html\"@a href=\"$1.l.html\"@g;
	    open(LDOS,">$_.l.html");
	    print LDOS $lt;
	    close LDOS;
	}
	if ($UTF) {
	    $ut = `$snomo$unomo $_.html`;
	    $ut =~ s@a href=\"(.{4}[^:].*?).html\"@a href=\"$1.u.html\"@g;
	    open(UDOS,">$_.u.html");
	    print UDOS $ut;
	    close UDOS;
	}
	if ($X) {
	    $xt = `$snomo$xnomo $_.html`;
	    $xt =~ s@a href=\"((?!http).*?)\.html\"@a href=\"$1.x.html\"@g;
	    open(XDOS,">$_.x.html");
	    print XDOS $xt;
	    close XDOS;
	}
	if ($ZAM) {
	    $zt = `$snomo$znomo $_.html`;
	    $zt =~ s@a href=\"(.{4}[^:].*?).html\"@a href=\"$1.z.html\"@g;
	    open(ZDOS,">$_.z.html");
	    print ZDOS $zt;
	    close ZDOS;
	}
    }
}


&recurse(".");

sub recurse {
    my @dujoj;
    my $dujo = $_[0];
    my $nunadujo = `pwd`;
    chomp($nunadujo);
    $nunadujo .= "/";
    &cxefa($dujo);
    while (<*>) {
	push(@dujoj,"$nunadujo$_") if (-d $_);
    }
    foreach $d (@dujoj) {
	print "$d\n";
	chdir($d);
	&recurse("$d");
    }
}

sub horstamp {
    my $dos = $_[0];
    my ($ahor,$mod) = (stat("$dos"))[8,9];
    my $tag = ("dim","lun","mar","mer","ĵaŭ","ven","sab")[(localtime($mod))[6]];
    my $mon = ("jan","feb","mar","apr","maj","jun","jul","aŭg","sep","okt","nov","dec")[(localtime($mod))[4]];
    my $tn  = (localtime($mod))[3];
    $tn = 0 . $tn unless (length($tn) eq 2);
    my $hor = (localtime($mod))[2];
    $hor = 0 . $hor unless (length($hor) eq 2);
    my $min = (localtime($mod))[1];
    $min = 0 . $min unless (length($min) eq 2);
    my $sek = (localtime($mod))[0];
    $sek = 0 . $sek unless (length($sek) eq 2);
    my $jar = (localtime($mod))[5] + 1900;
    my $tuthoro = "$tag $mon $tn $hor:$min:$sek PST $jar";

    rename($dos,"$dos.malnova");
    open(EDOS,"$dos.malnova") or die "ne povis skribi al $dos.malnova : $!\n";
    open(ADOS,">$dos") or die "ne povis krei $dos : $!\n";
    while(<EDOS>) {
	s@<!-- hor --> .*? <!-- /hor -->@<!-- hor --> $tuthoro <!-- /hor -->@;
	print ADOS $_;
    }
    close(EDOS);
    close(ADOS);
    utime($ahor,$mod,"$dos") or die "'utime' ne funkciis\n";
    unlink("$dos.malnova");
}
