The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl

use strict;
use warnings;

use Carp;
use Getopt::Long;
use Log::Dispatch;
use Log::Dispatch::Screen;
use Mozilla::Backup 0.05;
use Return::Value;

our $VERSION = '0.04'; # $Revision: 1.7 $

my $Help    = 0;
my $List    = 0;
my $Backup  = 0;
my $Restore = 0;

my $Type    = "firefox";              # profile type (e.g. "firefox")
my $Name    = "default";              # profile name (e.g. "default")
my $Path    = ".";
my $Plugin  = "Zip",

my $Quiet   = 0;
my $ShowVer = 0;

unless (GetOptions("type=s"   => \$Type,
                   "name=s"   => \$Name,  
                   "backup"   => \$Backup,
                   "restore"  => \$Restore,
                   "path=s"   => \$Path,
                   "plugin=s" => \$Plugin,
                   "help|h"   => \$Help,
                   "list|l"   => \$List,
                   "quiet|q"  => \$Quiet,
                   "version|v" => \$ShowVer,
		  )) {
  $Help = 1;
}

if ($ShowVer) {
  my $v = $Mozilla::Backup::VERSION;
  print << "VER";
mozback $VERSION (Mozilla::Backup $v)
Copyright (c) 2005 Robert Rothenberg. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

VER
}

if ($Help) {
print STDERR << "USAGE";
Usage: mozback [options]
  --help       show this screen
  --list       list available profile types and names
  --backup     backup a profile
  --restore    restore a profile
  --plugin     backup plugin (defaults to "$Plugin")
  --type       type of profile (defaults to "$Type")
  --name       name of profile (defaults to "$Name")
  --path       path to save backup (defaults to current directory)
  --quiet      quite mode (no output)
  --version    show version and license
USAGE
  exit(1);
}

my $Log = Log::Dispatch->new;

$Log->add( Log::Dispatch::Screen->new( name => 'screen',
                                       min_level => 'notice',
                                       stderr => 1,
)) unless ($Quiet);

if ($List) {
  my $moz = Mozilla::Backup->new( log => $Log );
  foreach my $type (sort $moz->found_profile_types) {
    foreach my $name (sort $moz->type(type => $type)->profile_names) {
      print sprintf("%-16s %-16s %-16s\n", $type, $name,
        ($moz->type($type)->profile_is_default(name => $name) ? "default" : "")
      );
    }
  }
}
elsif ($Backup) {
  my $moz = Mozilla::Backup->new(
    log    => $Log,
    plugin => "Mozilla::Backup::Plugin::$Plugin",
  );
  if ($moz->type(type => $Type)->profile_exists(name => $Name)) {
    if (-d $Path) {
      my $r = $moz->backup_profile(
        type => $Type,
        name => $Name,
        destination => $Path
      );
      croak "$r\n" unless ($r);
    } else {
      croak msg( level => "error", message => "$Path does not exist\n" );
    }
  }
  else {
    croak msg( level => "error",
	       message => "Cannot find $Type profile caled $Name\n");
  }
}
elsif ($Restore) {
  my $moz = Mozilla::Backup->new(
    log    => $Log,
    plugin => "Mozilla::Backup::Plugin::$Plugin",
  );
  if (-e $Path) {
    if ($moz->type_exists( type => $Type )) {
      my $r = $moz->restore_profile(
        archive_path => $Path,
        type => $Type,
        name => $Name,
      );
      croak "$r\n" unless ($r);
    }
    else {
      croak msg( level => "error", message => "$Type is not a valid type\n" );
    }
  }
  else {
      croak msg( level => "error", message => "$Path does not exist\n" );
  }
}

sub msg {
  my %p = @_;
  $Log->log(%p);
  return $p{message};
}