#!/usr/local/bin/perl
# Works with Perl4 and Perl5
#
# pgphtml -- a perl script to make PGP signed web-pages
#
# by SANFACE Software <sanface@sanface.com> 19 June 2002
#
# 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.1.
#
#use strict;
use Getopt::Long;
use File::DosGlob 'glob';

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 @files;
my $out = "pgphtml-out$$";
my $x=""; my $pgp="";
my $backupfile="";
if (@ARGV)
   {
   if ($^O =~ /^MSWin32$/i && !$recursive) {
     foreach $i (@ARGV) {
       if($i=~/\*|\?/) {push @files,glob($i)}
       else {push @files,$i}
       }
     }
   else {@files = @ARGV}
   foreach $x (@files)
      {
      if ($x=~/\.htm$/i || $x=~/\.html$/i)
         {
         $verbose and print "Processing file: $x\n\n";
	 if($backup) {
	    $backupfile=$x.".bak";
	    open (BACK,">$backupfile")
	      || die("pgphtml: couldn't open backup $backupfile\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);
            unlink($x);
            if (!rename($out, $x))
               {print STDERR "pgphtml: couldn't replace $x\n";exit(1);}
            unlink($out);
            unlink($pgp);	
            }
	 $backup and close(BACK);
         }
      else {print "Warning: the extension of the file isn't one of these .htm .HTM .html .HTML\n";}
      }
   }
else {printusage();}

sub prepgp
# Modifies HTML files before the execution of pgp command
   {
   print "$passwd\n";
   print " -->\n";
   while (<STDIN>)
      {
      $backup and print BACK;
      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;
     if (/<\/BODY>[ \t]*<\/HTML>/i) {
       s/<\/BODY>[ \t]*<\/HTML>/<KBD><\/KBD>\n<pre>/i;
       }
     next if (/<\/BODY>/i);
     s/<\/HTML>/<KBD><\/KBD><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);
}
