#!/usr/local/bin/perl

# This script will decrypt a Perl script that has been encrypted using the
# "encrypt" script. It cannot decrypt any other kind of encrypted Perl script.
#
# Usage is decr file...
#

$XOR             = 'Perl' ;
$BLOCKSIZE       = length $XOR ;
$HEADERSIZE      = 2 ;
$CRYPT_MAGIC_1   = 0xff ;
$CRYPT_MAGIC_2   = 0x00 ;
$Version         = 1 ;
$module_name     = 'Filter::decrypt' ;

$Fingerprint     = pack ("C*", $CRYPT_MAGIC_1, $CRYPT_MAGIC_2) ;

die "Usage: decrypt file...\n"
  unless @ARGV ;


# Loop through each file in turn.
foreach $file (@ARGV)
{
    if (! -f $file)
    {
        print "Skipping directory $file\n" if -d $file ;
        #print "Skipping strange file $file\n" if ! -d $file ;
        next ;
    }

    open (F, "<$file") || die "Cannot open $file: $!\n" ;

    # skip the #! line
    $a = <F> ;
    if ($a =~ /^#!/)
    {
        $sharp_bang = $a ;
        $a = <F> ;
    }

    # skip "use decrypt;" line
    die "No use $module_name in $file\n"
        unless $a =~ /use\s+$module_name\s*;/ ;

    read(F, $f, length($Fingerprint)) || die "Cannot read from $file: $!\n" ;
    print ("skipping file '$file': not encrypted\n"), next
        unless $f eq $Fingerprint ;

    print "decrypting $file to $file.pd\n" ;
    open (O, ">${file}.pd") || die "Cannot open ${file}.pd: $!\n" ;
    print O $sharp_bang if $sharp_bang ;
    while ($size = read(F, $block, $BLOCKSIZE) )
    {
        print O ($block ^ substr($XOR, 0, $size)) ;
    }


    close F ;
    close O ;

}

