#!/usr/local/bin/perl
# Works with Perl4 and Perl5
#
# pgphtml -- a perl script to make PGP signed web-pages
#
# by Fabrizio Pivari <Pivari@geocities.com> 22 March 1998
#
# Requires the pgp program
#
# Copy, use, and redistribute freely, but don't take my name off it and
# clearly mark an altered version.  Fixes and enhancements cheerfully 
# accepted.
#
# This is version 3.0.
#
#use strict;
use Getopt::Long;

my $help="";
my $verbose="";
my $passwd="";

do GetOptions("passwd=s" => \$passwd,
             "help"     => \$help,
             "verbose"  => \$verbose) || printusage() ;  
$help and printusage();

my $out = "pgphtml-out$$";
my $x=""; my $pgp="";
if (@ARGV)
   {
   foreach $x (@ARGV)
      {
      if ($x=~/\.htm$/i || $x=~/\.html$/i)
         {
         $verbose and print "Processing file: $x\n\n";
         open(TEMP, ">$out")
             || die("pgphtml: couldn't open tempfile $out \n");
         close(STDIN);
         if (!open(STDIN, "$x"))
            {print STDERR "pgphtml: couldn't open $x for input\n";exit(1);}
         else
            {
            select(TEMP);
            prepgp();
            close(TEMP);
            }
         $pgp = $out.".pgp";

         $ENV{'PGPPASSFD'} = '0'; # see pgp2.6.2 source code; this has the
                                  # effect of using the first line of the
                                  # input as the secret password.
         system("pgp -fast < ${out} > ${pgp}");
         $ENV{'PGPPASSFD'} = "";

         open(TEMP, ">$out")
             || die("pgphtml: couldn't open tempfile $out \n");
         close(STDIN);
         if (!open(STDIN, "$pgp"))
            {print STDERR "pgphtml: couldn't open $pgp for input\n";exit(1);}
         else
            {
            select(TEMP);
            postpgp();
            close(TEMP);
            unlink($x);
            if (!rename($out, $x))
               {print STDERR "pgphtml: couldn't replace $x\n";exit(1);}
            }
         }
      else {print "Warning: the extension of the file isn't one of these .htm .HTM .html .HTML\n";}
         unlink($out);
         unlink($pgp);
      }
   }
else {printusage();}

sub prepgp
# Modifies HTML files before the execution of pgp command
   {
   print "$passwd\n";
   print " -->\n";
   while (<STDIN>)
      {
      if (/-----BEGIN PGP SIGNED MESSAGE-----/)
         {
         print STDERR "pgphtml: $x is just PGP signed\n";
         unlink($out);
         exit(1);
         }
      if (length > 127)
         {

         print STDERR "pgphtml: The maximum line length can not exceed 
                       127 characters.\n";
         unlink($out);
         exit(1);
         }

# If HTML files has lines beginning with hyphen (-) puts a space in front of the
# hyphen (pgp alters lines beginning with a hyphen)
     s/^-/ -/;
     s/<BODY.*>/$&\n<KBD>----BEGIN PGP SIGNED WEB-PAGE----<\/KBD>/i;
##   changed in next line -- I like this better. (kp)
##      s/<BODY.*>/$&\n<PRE>----BEGIN PGP SIGNED DOCUMENT----<\/PRE>/i;
      next if(/<\/BODY>/i);
      s/<\/HTML>/<!PGPHTML written by Fabrizio Pivari (Pivari\@geocities.com)><PRE>/i;
      print $_;
      }
   }

sub postpgp
# Modifies HTML files after the execution of pgp command
   {
   print "<!--\n";
   while (<STDIN>) {print $_;}
   print qq!</PRE>\n</BODY>\n</HTML>!;
   }

sub printusage {
    print <<USAGEDESC;

usage:
        pgphtml [-options ...] files

where options include:
    -help                        print out this message
    -verbose                     verbose
    -passwd  yourpgppasswd

files:
    with files you can use metacharacters and relative and absolute path name
    
example:
    pgphtml *.html
    pgphtml -p "myfrase" *.htm

If you want to know more about this tool, you might want
to read the docs. They came together with pgphtml!

Home: http://www.geocities.com/CapeCanaveral/Lab/3469/pgphtml.html

USAGEDESC
    exit(1);
}
