#!/usr/local/bin/perl
$BACK="FFFFFF";
$copy=pop(@ARGV);
$PSINDEX="psion.html";
$FIELD=15;
$PSIREF="psi-prog.html";

open(INDEX,">$PSINDEX") || die "Couldn't open : $!\n";
open(PSI,">$PSIREF") || die "Couldn't open : $!\n";

print INDEX <<EndOfCommands;
<HEAD>
<TITLE>PSION Programming Reference</TITLE>
</HEAD>
<IMG ALT = "" SRC=psion.gif>
<H1>PSION Programming Reference</H1>
<P><HR>This is a first attempt at putting <A HREF="#copy">Psion's</A>
Programmer's Reference onto the Web. If, like me, you bought your 
3a after they stopped shipping the manual, or you just keep 
forgetting it, you should find this useful. It is a first attempt,
so please bear with me.<IMG ALT = "" SRC = "smiley.gif"><P>

By the way, rather than edit the <B>oplman</B> text files by hand
I've used a PERL program to do the majority of the work. This is
convenient for me, but it may mean a few blips here and there. Please
email me with any cock-ups you see, and I'll fix them, and make
the whole thing available via FTP.<P>
<A HREF="mailto:dave\@bccsun1.estec.esa.nl"><I>Dave Stafford</I></A>

<HR><P>
<UL>
EndOfCommands

print PSI <<EndOfRef;
<HEAD>
<TITLE>PSION Function Reference</TITLE>
</HEAD>

<IMG ALT= "" SRC=psion.gif>
<H1>PSION Function Quick Reference</H1>
<P><PRE>

EndOfRef

foreach $file (@ARGV) {

  $x=$file;
  $chap=substr($file,1,2);
  if ($chap =~ /_/ ) {chop $chap;$chap =~ tr/a-z/A-Z/;}
  elsif ($chap < 10) {$chap=substr($chap,1,1);}
  chop $x;chop $x;chop $x;chop $x;
  $html=$x;
  open(OUTPUT,">$html.html") || die "Couldn't open : $!\n";
  open(DATA,$file) || die "Couldn't open $file: $!\n";

# First line is H1 Header (except for Appendix A), the
# and the file name is the title

  $line=&getline('DATA');
  while ( $line eq "" ) {$line=&getline('DATA');}    

  if ( $chap eq "A" ) {
     print INDEX "</UL><HR><H2>$line</H2>\n";
     print INDEX "<UL>\n";
     $line=&getline('DATA');
     while ( $line eq "" ) {$line=&getline('DATA')};
  }
   
print OUTPUT <<EndPsion;

<TITLE>PSION Programming Ref.: Chapter $chap. $line</TITLE>
<BODY BGCOLOR="#$BACK">
Return to the Index<BR>
<A HREF="$PSINDEX"><IMG ALT = "<==" SRC ="psi-logo.gif"></A> 
<H1><CENTER>$line</CENTER></H1>
EndPsion

  print INDEX "<H3><A HREF=\"$x.html\"><B>$chap. $line</B></A></H3>\n";
  $sub1=1;
  print INDEX "<UL>\n";
  $COU=0; 
  while (<DATA>) {
     chop;
     if ( /^$/ ) {                  #if a blank line
        print OUTPUT "<P>\n";
         next;
     }

     elsif ( substr($_,0,6) eq "------" ) {next;}

     elsif ( substr($_,0,6) eq "******" ) { # Sub Section header
        $_ =~ tr/*//d;
        print OUTPUT "<HR SIZE=4 NOSHADE ><A NAME = \"$chap.$sub1\"><H2>$_</H2></A>\n";
        print INDEX  "<A HREF=\"$html.html\#$chap.$sub1\">$chap.$sub1. $_</A><BR>\n";
        $sub1++;
     }
     elsif ( substr($_,0,4) eq "*** " && $chap == 13 ) {   # Command Listing
        $funct=substr($_,4);
        print OUTPUT "<A NAME = \"$funct\"><A HREF=\"$PSIREF\"><H3><HR>$funct</H3></A>\n";
        $white = $FIELD - length($funct);
        print PSI " <A HREF=\"$html.html\#$funct\">$funct</A>",' ' x $white;
        if ( $COU++ == 4 ) {print PSI "\n";$COU=0;}
        $line=&getline('DATA');            # Next line is blank, chuck
        $line=&getline('DATA');            # This is the Usage:
        $x=&getline('DATA');               # Next line is blank, chuck
        $use=&getline('DATA');             # This is the actual use
        $next=&getline('DATA');            # Check for more
	if ( $next eq "" ) {
		print OUTPUT "<B>$line</B> <CODE>$use";  
	} else {
 	   print OUTPUT "<B>$line</B><DD><CODE>$use<BR>\n";
	   print OUTPUT "<DD>$next<BR>\n";
           $line=&getline('DATA');
           while ( $line ne "" ) {
             print OUTPUT "<DD>$line<BR>\n";
             $line=&getline('DATA');
           }
        }
	print OUTPUT "</CODE></DT><P>\n";  
     } 

     elsif ( substr($_,0,4) eq "*** " ) {   # Sub section header
        $h3=substr($_,4);
        print OUTPUT "<H3>$h3</H3> \n";
     } 
     elsif ( substr($_,0,5) eq "ARROW" ) {   # Sub section header
        $line=substr($_,5);
        print OUTPUT "<DL><DD><IMG ALT = \"Note : \" ALIGN=LEFT SRC=\"Point.gif\">$line \n";
        $line=&getline('DATA');
        while ( $line ne "" ) {
           print OUTPUT "$line\n";
           $line=&getline('DATA');
        }
        print OUTPUT "</DL>\n";
     }
     elsif ( substr($_,0,2) eq "*)" ) {    # Bullet list till end para
        $h3=substr($_,2);
        print OUTPUT "<UL><LI>$h3</LI> \n";
        $line=&getline('DATA');
        while ( $line ne "" ) {
           print OUTPUT "$line\n";
           $line=&getline('DATA');
        }
        print OUTPUT "</UL>\n";
     }
     elsif ( substr($_,0,4) eq "PROC" ) {   # Probably example program
        print OUTPUT "<UL><PRE>\n";
        print OUTPUT "<B>$_</B>\n";
        $line=&getline('DATA');
        while ( substr($line,0,4) ne "ENDP" ) {
           print OUTPUT "$line\n";
           $line=&getline('DATA');
        }
        print OUTPUT "<B>$line</B>\n";
        print OUTPUT "</PRE></UL>\n";
     }       

     else {print OUTPUT "$_\n";}
  }
  if  ($chap==13) {
    print INDEX "<A HREF = $PSIREF>Function Quick Reference</A>\n";
  }
  print INDEX "</UL><P>\n";
  print OUTPUT "<P><P><HR><H2><A HREF=\"$PSINDEX\"><IMG ALT = \"<==\" SRC =\"psi-logo.gif\"></A>  Return to the Index</H2>\n"; 

}
print INDEX "</UL><P><P><HR>\n"; 

close (OUTPUT);
close (DATA);

####### now do the copyright notice ###################

open(DATA,$copy) || die "Couldn't open $copy: $!\n";

$line=&getline('DATA');
while ( $line eq "" ) {$line=&getline('DATA');}
$line =~ tr/"//d;

print INDEX "<A NAME=\"Copy\"><H2>$line  <IMG ALT = \"\" ALIGN = CENTER SRC = \"psion-s1.gif\"></H2></A>\n";
while (<DATA>) {
     chop;
     if ( $_ eq "" ) {print INDEX "<P>\n";}
     else {print INDEX "$_\n";}
}

print INDEX <<EndOfCopy;
<P>
<HR>
This HTML version of the manual was automagically created (with a
tiny bit of tweaking <IMG ALT = "" SRC = "smiley.gif">) from the 
oplman text files using PERL. If you want the source for this, or if
you have any suggestions, please email me 
<A HREF="mailto:dave\@bccsun1.estec.esa.nl"><I>Dave Stafford</I></A>
 
EndOfCopy

print PSI <<EndOfR;
</PRE>
<HR>
This HTML version of the manual was automagically created (with a
tiny bit of tweaking <IMG ALT = "" SRC = "smiley.gif">) from the 
oplman text files using PERL. If you want the source for this, or if
you have any suggestions, please email me 
<A HREF="mailto:dave\@bccsun1.estec.esa.nl"><I>Dave Stafford</I></A>
 
EndOfR

close (DATA);
close (INDEX);
close (PSI);

###################################################################
sub getline {
    local($file) = @_;
    local($_);
 
    $_ = <$file>;
    {
        chop;
        if (/\\$/) {
            chop;
            $_ .= <$file>;
            redo;
        }
    }    
    $_;
}
 
