#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Copyright: GPL, Author: Guido Socher
my $VERSION = "0.12";
#
use strict;
use vars qw($opt_p $opt_i $opt_h $opt_v);
use Getopt::Std;
use HTML::TagReader;
use IO::Handle;
#
sub help();
sub expandonefile($$);
sub dirname($);
sub abspath($);
#
getopts("hp")||die "ERROR: No such option. -h for help.\n";
help() if ($opt_h);
help() unless ($ARGV[0]);

my $changecount=0;
my $mode;
my $ofile;
my $renfile;
for my $f (@ARGV){
    if ( -r "$f" ){
        if ($opt_p){
            $ofile=$f;
            $renfile=$f;
            $changecount=expandonefile("$f","/tmp/none");
            print STDERR "$f: $changecount\n";
        }else{
            $mode=(stat(_))[2];

            $ofile=$f;
            $renfile="$f.lftr";
            rename($ofile,$renfile)||die "ERROR: can not rename $ofile to $renfile, check directory permissions.\n";
            $changecount=expandonefile($renfile,$ofile);
            if ($changecount){
                chmod($mode,$ofile)||die "ERROR: chmod %o $ofile failed\n";
                unlink($renfile)||die "ERROR: unlink $renfile failed\n";;
                print "$f: file modified to indicate translation status, $changecount\n";
            }else{
                # nothing changed restore the old file and do not change
                # modification time
                unlink("$ofile");
                rename($renfile,$ofile)||die "ERROR: can not rename $renfile to $ofile, check directory permissions.\n";
            }
        }
    }else{
        warn "ERROR: can not read $f\n";
    }
}
# 
# exit but cleanup renamed files
#
sub dieclean($){
    my $msg=shift;
    print STDERR $msg;
    if ($ofile ne $renfile){
            unlink("$ofile");
            rename($renfile,$ofile)||die "ERROR: can not rename $renfile to $ofile, check directory permissions.\n";
    }
    exit 1;
}
# 
# expand exactly one file 
#
sub expandonefile($$){
    my $infile=shift;
    my $outfile=shift; 
    my $changecount=0;
    my @tag;
    my $lftransstatus=0;
    my ($imgtag,$buffer,$waitanchortag,$articlehref,$s,$issuedir,$origtag,$a);
    my $cc_tr=0;
    my $cc_ntr=0;

    my $dir=dirname($infile);
    my $absdir=abspath($infile);
    if ($absdir=~m/\/([A-Z]\w+\d\d+)$/){
        $issuedir=$1;
    }else{
        print STDERR "ERROR: can not determine the issue name (e.g something like May2002), is the file $infile really in the right directory?\n";
        return(0);
    }
    #
    my $p=new HTML::TagReader "$infile";
    my $fd_out=new IO::Handle;
    $waitanchortag=0; # wait for <a href after img tag.
    unless($opt_p){
        open(OUT,">$outfile")||dieclean("ERROR: can not write $outfile\n");
        $fd_out->fdopen(fileno(OUT),"w")||die;
    }else{
        $fd_out->fdopen(fileno(STDOUT),"w")||die "ERROR: can not write to stdout\n";
    }
    while(@tag = $p->getbytoken(1)){
        # read out the tags, and the text
        # $tag[0]=tag
        # $tag[1]=tagtype
        # $tag[2]=line in file
        $origtag=$tag[0];
        if ($lftransstatus==0 && $tag[1] eq "!--"){
            $tag[0]=~s/\s+//g; # kill newline
            if ($tag[0]=~/lftransstatus=1/){
                $lftransstatus=1;
                $changecount++;
                $fd_out->print($origtag);
                next;
            }
        }
        if($lftransstatus==0){
            $fd_out->print($origtag);
            next;
        } 
        # we search for tagtype=img followed by tagtype=a not
        # more than 2 tags later.
        if ($tag[1] eq 'img' && $waitanchortag==0){
            if ($tag[0]=~/images\/frame/){
                # ok this is the right image tag
                $imgtag=$origtag;
                $waitanchortag=1;
                $buffer="";
                next;
            }
        }
        unless($waitanchortag){
            $fd_out->print($origtag);
            next;
        }
        $waitanchortag++ if ($waitanchortag);
        if ($tag[1] eq 'a'){
            $tag[0]=~s/\s+/ /g; # kill newline
            if ($tag[0]=~/href ?=.+(article\d+\.\w+ml)/i){
                # we found an article entry
                $articlehref=$1;
                $a=0;
                if (-f "$dir/$articlehref"){
                    # file exists
                    $s=(stat("$dir/$articlehref"))[7];
                    if ($s > 2100){
                        $a=1;
                    }
                }
                if ($a){
                    # article is there:
                    $cc_tr++;
                    $fd_out->print("<img src=\"../../common/images/frame_tux.gif\" alt=\"[translated]\"\n align=\"middle\">");
                    $fd_out->print($buffer);
                    $fd_out->print("<a href=\"$articlehref\">");
                }else{
                    # article is not there:
                    $cc_ntr++;
                    $fd_out->print("<img src=\"../../common/images/frame_tuxgrey.gif\" alt=\"[not translated]\"\n align=\"middle\">");
                    $fd_out->print($buffer);
                    $fd_out->print("<a href=\"../../English/$issuedir/$articlehref\">");
                }
                $waitanchortag=0;
                next;
            }else{
                $buffer.=$origtag;
                next;
            }
        }else{
            $buffer.=$origtag;
            if ($waitanchortag > 4 ){
                $waitanchortag=0;
                $fd_out->print($imgtag);
                $fd_out->print($buffer);
            }
            next;
        }
    }
    $fd_out->close;
    close OUT;
    if ($changecount){
        return("$cc_tr translated, $cc_ntr linked to English");
    }else{
        return(0);
    }
}
#----------------------------------
# construct out of the pwd and the filename
# the absolute path to the directory where the file is
# It can look like this: /a/b/../c/../d
sub abspath($){
    my $f=shift;
    my $pwd=`pwd`;
    chomp($pwd);
    my $dir=dirname($f);
    $dir="" if ($dir eq ".");
    my $outdir=$pwd ."/".$dir;
    $outdir=~s/\/$//;
    return($outdir);
}
#----------------------------------
# get the directory name from file name
sub dirname($){
    my $f=shift;
    if ($f=~m=/=){
        $f=~s=/[^/]*$==;
        return("$f");
    }else{
        return(".");
    }
}
#----------------------------------
sub help(){
print "lftransstatus -- modify the index page of an issue and
indicate which articles are translated and which are not.

USAGE: lftransstatus [-hp] index.shtml

lftransstatus reads the file index.shtml and searches for
<!-- lftransstatus=1 -->. If it finds this then it will
modify the page otherwise do nothing.
The program searches in the html code for a pattern that 
looks as follows:
<img src=\"../../common/images/frame_...\" ....> 
<a href=\"...articleXXX.shtml\">
If it finds this pattern then it will edit it and change the 
image and the link.
For articles that are translated (file is there and bigger than 2K) 
it will create the link to that article otherwise it will link to 
the English pages.

This is to indicate on the index page of an issue whether the
article is translated or not.

File access permissions are preserved.

The program is also executed automatically when files are uploaded
to the server.

OPTIONS: 
     -h this help

     -p print to stdout and do not modify any files.

EXAMPLE:
lftransstatus Deutsch/September2002/index.shtml

version $VERSION
         \n";
exit(0);
}
__END__ 

=head1 NAME

lftransstatus -- modify the index page of an issue and
indicate which articles are translated and which are not.

=head1 SYNOPSIS

 lftransstatus [-hp] index.shtml

=head1 DESCRIPTION


lftransstatus reads the file index.shtml and searches for
<!-- lftransstatus=1 -->. If it finds this then it will
modify the page otherwise do nothing.
The program searches in the html code for a pattern that 
looks as follows:
<img src="../../common/images/frame_..." ....> 
<a href="...articleXXX.shtml">
If it finds this pattern then it will edit it and change the 
image and the link.
For articles that are translated (file is there and bigger than 2K) 
it will create the link to that article otherwise it will link to 
the English pages.

This is to indicate on the index page of an issue whether the
article is translated or not.

File access permissions are preserved.

=head1 OPTIONS

-h this help

-p print to stdout and do not modify any files.

=head1 EXAMPLE

lftransstatus Deutsch/September2002/index.shtml

=head1 AUTHOR

lftransstatus  was written by Guido Socher [guido(at)linuxfocus.org]

=cut
