#!/usr/bin/perl
# filter of help2man output
use strict;
use warnings;

while ( <STDIN> ) {
    #multiline regex
    undef $/;
    # remove colon from the name
    $_ =~ s/(^\.SH\sDESCRIPTION\n.*\.IP\n)(cat)/$2/gmxs;
    $_ =~ s/(^\.PP\n)(Input\soptions.*)/.SH OPTIONS\n.SS $2/gmxs;
    # $_ =~ s/(^\.PP\n)(Computation\soptions.*)/.SS $2/gmxs;
    $_ =~ s/(^Batch\smode\s\(default\s=\s2\)\n)(Mode.*)/$1\n         $2/gmxs;
    $_ =~ s/(^\.TP\n)(0)(\n)(see\snote.*)/           $2      $4/gmxs;
    $_ =~ s/(^\.TP\n)(1)(\n)(see\snote.*)/           $2      $4/gmxs;
    $_ =~ s/(^\.TP\n)(\(default\))(\n)(2\s{6}see\snote.*)/ $2 $4/gmxs;
    $_ =~ s/(^3\s{6}see\snote.*)/           $1/gmxs;
    $_ =~ s/(^4\s{6}see\snote.*)/           $1/gmxs;
    $_ =~ s/(^5\s{6}expand\s.*)/           $1/gmxs;
    # lineup "-f format" option of gmap
    $_ =~ s/(under\sOutput\stypes\)\:\n)(.*)\.PP\n/
                my $q = $1;
                my $p = $2;
                $p =~ s"(\,\n)"$1 "gmxs;
                $p = $q.' '.$p;
                /egmsx;
    # $_ =~ s/(^\.TP\n)(Note:?\s.*)/\n$2/gmxs;
#gsnap aligning
    #$_ =~ s/(^\.SH\sDESCRIPTION\n)GSNAP\sversion.*help\\fR\n\.IP/$1/gmsx;
    $_ =~ s/(^\.TP\n)(\(default\))(\n\s+)(4\s{6}see\snote.*)/ $2 $4/gmxs;
    $_ =~ s/(^\.TP\n)(2)(\n)(see\snote.*)/           $2      $4/gmxs;
    $_ =~ s/(^\.TP\n)(3)(\n)(see\snote.*)/           $2      $4/gmxs;
    $_ =~ s/(^5\s{6}see\snote\s.*)/           $1/gmxs;
    $_ =~ s/(^Examples:\n\.TP)/\.TP\n$1/gmsx;
    $_ =~ s/(.*be\schanged)\n\.TP\n(by\sproviding.*)/$1 $2/gmsx;
    #fix gmap_build header
    $_ =~ s/(^\.SH\sDESCRIPTION\n)Unknown.*\.PP/$1/gmsx;
    print $_;
}
print "\n\.TP\nOther tools of GMAP suite are located in \/usr\/lib\/gmap";
