#!/usr/bin/perl -w
#------------------------------------------------------------------------------
# makehelp.pl - generate eisfair xml help from tex
# 
#------------------------------------------------------------------------------

use strict;

my $file    = shift @ARGV;
my $package = $file;
$package =~ s/.*\///g;
$package =~ s/\..*//g;
$package =~ s/_.*//g;
my ($string, $inhelp)  = ("", "");

my ($s, $m, $h, $day, $month, $year, $wd, $nday, $isdst) = localtime(time);

$year  += 1900;
$month += 1;
if ($month < 10) { $month = "0$month"; }
if ($day < 10)   { $day = "0$day"; }

my $tag_help_on  = '^[ \t]*\\\\config{(.*?)}{.*}{.*}[ \t]*$';

my $bracket_on   = '^[ \t]*{[ \t]*$';
my $bracket_off  = '^[ \t]*}[ \t]*$';

my $empty_line   = '^[ \t]*$';

sub tex2xml
{
    $string = shift;
    return if (!$string);

    $string =~ s/\\_/_/g;
    $string =~ s/\\%/%/g;
    $string =~ s/\\emph{(.*?)}/$1/g;
    $string =~ s/{\\tt (.*?)}/$1/g;
    $string =~ s/{\\bf (.*?)}/$1/g;
    $string =~ s/``/"/g;
    $string =~ s/ДД/"/g;
    $string =~ s/"`/"/g;
    $string =~ s/"'/"/g;
    $string =~ s/$empty_line/<br\/><br\/>/;

    print "$string";
}

tex2xml("<!-- \n");
tex2xml("#-----------------------------------------------------------------------------\n");
tex2xml("# /var/install/help/$package - helptexts for package $package\n");
tex2xml("#\n");
tex2xml("# Creation   : $day.$month.$year  $ENV{'LOGNAME'}\n");
tex2xml("# Last Update: $day.$month.$year  $ENV{'LOGNAME'}\n");
tex2xml("#\n");
tex2xml("# Copyright (c) 2001-$year Frank Meyer, frank(at)eisfair.org\n");
tex2xml("#\n");
tex2xml("# This program is free software; you can redistribute it and/or modify\n");
tex2xml("# it under the terms of the GNU General Public License as published by\n");
tex2xml("# the Free Software Foundation; either version 2 of the License, or\n");
tex2xml("# (at your option) any later version.\n");
tex2xml("#-----------------------------------------------------------------------------\n");
tex2xml("-->\n\n");
 
open(INPUT, "<$file") || die("Could not open $file, reason: $!\n");
while (<INPUT>)
{

    if ($inhelp && /$bracket_off/)
    {
        tex2xml("</help>\n\n");
        $inhelp = "";
    }
    elsif (/$bracket_on/)
    {
        # skip
    }
    elsif (/$tag_help_on/)
    {
        tex2xml("<help name=\"");
        $string = $1;
        $string =~ s/x/%/g;
        tex2xml($string);
        tex2xml("\">\n");

        $inhelp = "true";
    }
    elsif ($inhelp)
    {
        tex2xml($_);
    }
}

