#!/usr/local/bin/perl
# Works with Perl4 and Perl5
#
# pgphtml -- a perl script to make PGP signed web-pages
#
# by SANFACE Software <sanface@geocities.com> 19 June 2001
#
# Requires the PGP or GPG
# GPG support added by John Arundel <john@splange.freeserve.co.uk>
#
# 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 4.0.
#
#use strict;
use Getopt::Long;

my $help="";
my $verbose="";
my $passwd="";
my $backup="";
my $gpg=0;
my $gpgflags = "";

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

my $out = "pgphtml-out$$";
my $x=""; my $pgp="";
my $backupfile="";
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.
 	 $gpgflags = "--passphrase-fd 0" if $passwd;
          if($gpg) {
 	 	system("gpg --clearsign $gpgflags < ${out} > ${pgp}");
 	 } else {
 	 	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);
            if ($backup) {$backupfile=$x.".bak";rename($x,$backupfile);}
            else {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 && !$gpg)
         {

         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>\n!;
   }

sub printusage {
    print <<USAGEDESC;

usage:
        pgphtml [-options ...] files

where options include:
    -help                        print out this message
    -gpg                         use GPG instead of PGP
    -verbose                     verbose
    -backup                      save the original files in .bak
    -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.sanface.com/pgphtml.html

USAGEDESC
    exit(1);
}
