#!/home/product/code/webui-perl/bin/perl -T -I /home/product/code/firmware/current/lib/perl5
#SUDOER_USERS: nobody
#SUDOER_RUNAS_USERS: root, mta
#SUDOER_TAGS: NOPASSWD, EXEC
use strict;
use warnings;
use Barracuda::Sudo::Lib;

use IPC::Run;

=pod

=head1 NAME

decrypt_updates

=head1 DESCRIPTION

Decrypts and unpacks a file downloaded from the update server for storing in
the bcs repository

=cut

sub capture_call {
    my(@command) = (@_);
    my $result;
    my $error;
    IPC::Run::run([@command], \undef, \$result, \$error);
    die "Error executing command \n".join(' ',@command)."\nExited with code $?\n$@\n$error" if $?;
    return split('\n', $result) if(wantarray);
    return $result;
}

my ($source, $destination) = get_argv(qr|^[\w\./]+$|, qr|^[\w\./]+$|);

my $result = capture_call('/usr/bin/gpg', '--homedir', '/root/.gnupg', '--decrypt', '--batch', '--yes', '--output', $destination, $source);
exit($result);
