#!/usr/bin/perl -w
# $Id: bkp-section.in.in,v 1.5 2003/10/12 12:14:01 x99laine Exp $
# Copyright (c) 2003 Jeremy Lain

use strict;
use lib '/usr/share/bkp';

use Getopt::Std;
use Date::Format;
use Sys::Hostname;
use Bkp;

# package and version
my $package = "bkp-section";

# config and options
my (%conf,%opts);


sub syntax {
   print "[ This is $package (bkp 0.5.2), a directory backup utility ]\n\n",
        "Syntax:\n",
        "  $package <action> [options]\n\n",
        " Actions:\n",
        "  -a           - process all sections\n",
	"  -c <class>   - process sections of class <class>\n",
        "  -h           - this help message\n",
	"  -o <section> - process only section <section>\n\n",
        " Options:\n",
        "  -d           - debug mode\n",
        "  -f <file>    - use <file> as config file\n",
        "  -n           - simulation mode (do not touch filesystem)\n";
}


sub init {
  if ( not getopts('dhnf:ac:o:', \%opts) or $opts{'h'}) {
    &syntax();
    exit(1);
  }

  # process options
  if ($opts{'d'}) {
    $Bkp::debug = 1;
    Bkp::dprint("Running in debug mode ...\n");
  }
  
  if ($opts{'n'}) {
    Bkp::dprint("Running in simulation mode ...\n");
  }
  
  # check the requested action
  if (!$opts{'a'} and !$opts{'c'} and !$opts{'o'}) {
    print("no action was selected\n");
    &syntax;
    exit(1);
  }

  # read config file
  if ($opts{'f'}) {
    %conf = Bkp::read_conf($opts{'f'});
  } else {
    %conf = Bkp::read_conf($Bkp::confile);
  }
}


sub section {
  my $section = shift;
  my($entry,$fdir,$pdir,$zip,$targetopts);
  my $date = time2str("%Y-%m-%d", time);
  my $hostname = hostname;
  my $sectdir = "$conf{'savedir'}/$hostname/$section";
  my @targets = @{$conf{'sections'}{$section}{'targets'}};
  
  if (Bkp::create_dir($sectdir,$opts{'n'})) {
    print("could not create '$sectdir', skipping section '$section'!\n");
    return;
  }

  # perform required backups
  while ($entry = shift(@targets)) {
    Bkp::dprint("** Processing entry $entry **\n");
    ($pdir,$fdir,$zip,$targetopts) = split /:/,$entry;
    if ($targetopts) {
      Bkp::dprint("extra options : $targetopts\n");
    } else {
      $targetopts="";
    }
		
    # strip final slash if present
		$fdir =~ s/,/ /g;
		$fdir =~ s/\/\s/ /g;
		$pdir =~ s/^(.+)\/$/$1/; 

    # backup the file
    if (Bkp::change_dir($pdir,$opts{'n'})) {
      print("could not change to '$pdir', skipping target '$entry'!\n");
      next;
    }
    
    my $cmd = "tar cjf $sectdir/$zip.$date.tar.bz2 $fdir $targetopts";
    Bkp::dprint("running : $cmd\n");
    if (!$opts{'n'}) {
      if (system($cmd)) {
        print("error running tar!\n");
      }
    }
    Bkp::dprint("\n");
  }

  # erase old backups
  Bkp::dprint("** Erasing old backups **\n");
  Bkp::expire_dir($sectdir,$conf{'sections'}{$section}{'maxdays'},$opts{'n'});
}


sub main {
  &init;

  foreach my $section (@{$conf{'all'}{'sections'}}) {
    if ( ($opts{'a'}) or 
         ($opts{'c'} and ($conf{'sections'}{$section}{'class'} =~ $opts{'c'})) or
	 ($opts{'o'} and ($section =~ $opts{'o'})) ) {
      Bkp::dprint("\n* Processing section $section *\n");
      &section($section);
    }
  }
}

&main();
