#!/usr/bin/perl
#
# The main intention of this script is to make unchangable help files
# delivered with some of the integrated software compatible with
# the arb_hlp2xml help converter.

use strict;
use warnings;

my $inputFilename;
my $outputFilename;

use Carp;

sub die_input($) {
  my ($msg) = @_;
  confess("$inputFilename:$.: $msg");
}

sub clustalTopic2Section($) {
  my ($line) = @_;
  if ($line =~ /^\s*\>\>\s*([^<>]+)\s*\<\<\s*(.*)$/o) {
    # '>>topic<< moretext' -> 'SECTION topic moretext'
    $line = "SECTION $1 $2\n";
  }
  return $line;
}
sub fancyitemind2star($) {
  my ($line) = @_;
  $line =~ s/^(\s+)[\+\=]\s/$1\* /;
  return $line;
}
sub formatFormatHeadlines($) {
  my ($line) = @_;
  if ($line =~ /File\sFormat/o) {
    $line =~ s/^\s+/  /og;
  }
  elsif ($line =~ /Example\s.*\sfile/o) {
    $line =~ s/^\s+/   /og;
  }
  return $line;
}
sub readseq_promote_sections($) {
  my ($line) = @_;
  if ($line =~ /^\s+\|[\|]+\s+/o) {
    $line = "SECTION $'";
  }
  return $line;
}

sub linkToWeb($) {
  my ($line) = @_;
  if ($line =~ /LINK\{(http|ftp)[^\}]*\}/o) {
    die_input "illegal LINK{web}-element in generated help";
  }
  $line =~ s/((http|ftp):\/\/[^\s]*)(\s|$)/LINK{$1}/og;
  return $line;
}

sub elimEOLwhitespace($) {
  my ($line) = @_;
  $line =~ s/\s+$//o;
  return $line;
}

sub main() {
  my $args = scalar(@ARGV);
  if ($args!=2) {
    die "Usage: postprocess_genhelp.pl input-filename output-filename\n".
      "Filters stdin to stdout, filenames are only passed to trigger specific behavior\n".
      "OR to print proper error messages.\n ";
  }

  $inputFilename = $ARGV[0];
  $outputFilename = $ARGV[1];

  my $filename = $outputFilename;

  my @reg = ();
  my @fun = ();

  if ($filename =~ /clustalw/) {
    push @reg, qr/([^\s]\s)\s+([\(])/;
    push @reg, qr/([\.:])(\s)\s+/;
    push @fun, \&clustalTopic2Section;
  }
  elsif ($filename =~ /readseq/) {
    push @reg, qr/([\.])(\s)\s+/;
    push @reg, qr/(LINE\s)\s+([0-9]+)/;
    push @fun, \&fancyitemind2star;
    push @fun, \&readseq_promote_sections;
    if ($filename =~ /Formats/) {
      push @fun, \&formatFormatHeadlines;
    }
  }

  push @fun, \&linkToWeb;
  push @fun, \&elimEOLwhitespace;

  my $titled = 0;

  while (defined($_=<STDIN>)) {
    chomp;
    # convert all lines starting at column zero into SECTIONs
    if (s/^([^ \#\n])/SECTION $1/) {
      # convert first SECTION into TITLE
      if ($titled==0) {
        s/^SECTION/TITLE/;
        $titled=1;
      }
    }

    foreach my $re (@reg) {
      while ($_ =~ $re) {
        $_ = $`.$1.$2.$';
      }
    }
    foreach my $fn (@fun) {
      $_ = &$fn($_);
    }
    print $_."\n";
  }
}
main();
