#!/usr/bin/perl -w
#
# Copyright (c) 2005 - 2007 Miek Gieben; Mark J Hewitt
# See LICENSE for the license
#
use strict;

use Getopt::Std;
use File::Basename;
use File::Temp qw{tempdir tempfile};
use File::Spec;
use POSIX;

# common functions
my $prefix="/usr";
my $datarootdir = "${prefix}/share";
require "${datarootdir}/rdup/shared.pl"; 

my $progName = basename $0;
my %opt;
my $d = i18n();

# add -k option, for key selection
getopts('dhV', \%opt);
usage() if $opt{'h'};
version($progName) if $opt{'V'};
my $crOptions = "-e ";
$crOptions = "-d " if $opt{'d'};
$crOptions .= "--use-agent " if $opt{'a'};

`which gpg >/dev/null`;
if ($? != 0) {
        die "** Cannot find \'gpg'";
}

my $default="--default-recipient-self";
if (defined($ARGV[0])) {
	$default="--default-recipient $ARGV[0]";
}

$crOptions .= "-q $default";

my $tmpDir = tempdir("rdup.gpg.XXXXXX", TMPDIR => 1, CLEANUP => 1);
die $d->get("** could not create:") . " $tmpDir: $!" unless -d $tmpDir;

while (($_ = <STDIN>)) {
        chomp;
        my ($t, $bits, $uid, $gid, $psize, $fsize) = split(" ", $_, 6);
        my $dump = substr($t, 0, 1);
        my $type = substr($t, 1, 1);

        sanity_check($dump, $bits, $psize, $fsize, $uid, $gid);

        my $path = "";
        read STDIN, $path, $psize;
        die $d->get("** Empty path") if ($path eq "");

        if ($dump eq '+') {        # add
                if ($type eq '-') {      # REG
                        if ($fsize != 0) {
                                my($fh, $filename) = tempfile("file.XXXXX", DIR => $tmpDir, SUFFIX => ".gpg" );
                                $fh->close();
                                open CRYPT, "|gpg $crOptions 2>/dev/null > $filename" or die "** $filename: $!";
                                copyout($fsize, *CRYPT);
                                close CRYPT or warn "** Crypt failure: result will be empty \`$path': $!";
                                my $size = (stat($filename))[7];
                                syswrite STDOUT, "$dump$type $bits $uid $gid $psize $size\n$path";
                                catfile($filename);
                                unlink $filename;
                                next;
                        }
                        syswrite STDOUT, "$dump$type $bits $uid $gid $psize $fsize\n$path";
                        next;
                }
        } 
	syswrite STDOUT, "$dump$type $bits $uid $gid $psize $fsize\n$path";
}

sub usage {
        print "$progName [OPTIONS] [RECIPIENT]\n\n";
        print "Encrypt or decrypt the file's contents with the\n";
        print "default public key in your key ring\n";
	print "RECIPIENT - recipient name to use for GPG\n";
	print "            defaults to self\n";
        print "\nOPTIONS:\n";
        print " -c    ignored as rdup-gpg always works on content\n";
        print " -a    use gpg-agent (needed when decrypting a lot files)\n";
        print " -d    decrypt the files\n";
        print " -h    this help\n";
        print " -V    print version\n";
        exit;
}
