#!/usr/local/bin/perl
my $VERSION = "1.01";

# fssnap; used as a wrapper for /usr/sbin/fssnap that changes user and
# group permissions to let a 'backup' user do backups.  Must be run as
# root, but this is probably best done with 'sudo'.

# Nominally part of TCB::Backup.

$|++;
use strict;
use Getopt::Std;

our $SNAP  = '/usr/sbin/fssnap';
our $DEBUG = 0;
our $GID   = "11001";			# 'backup' group

use vars qw( $opt_F $opt_V $opt_o $opt_d $opt_i );
getopts('F:Vo:di');

my $cmd;
my $change = 1;

my @args;
push @args, "-F $opt_F" if defined $opt_F;
push @args, "-o $opt_o" if defined $opt_o;

if ($opt_i)    { $change = 0; $cmd = join(' ', $SNAP, @args, "-i @ARGV"); } 
elsif ($opt_d) { $change = 0; $cmd = join(' ', $SNAP, @args, "-d @ARGV"); }
else           {              $cmd = join(' ', $SNAP, @args, @ARGV); }

warn "$cmd\n" if $DEBUG;
exit 0 if $opt_V;
my @answer;
open (SNAP, "$cmd |") or die "Couldn't run $SNAP: $!\n";
my @answer = <SNAP>;
close SNAP;

if ($change) {
  sleep 1;
  my $file = shift @answer;  chomp $file;
  if ($file) {
    chown(0, $GID, $file);
    chmod(0750, $file);
    print "$file\n";
  }
} else { print @answer; }
