#!/usr/bin/perl -w
#
# mrmini main application
# Mini-distribution maker for the MondoRescue project
#
# $Id$
#
# Copyright B. Cornec 2008-2010
# Provided under the GPL v2

# Syntax: see below

use strict 'vars';
use Getopt::Long qw(:config auto_abbrev no_ignore_case);
use Data::Dumper;
use English;
use File::Basename;
use File::Copy;
use File::stat;
use Digest::MD5 qw(md5_hex);
use lib qw (lib);
use POSIX qw(strftime);
use ProjectBuilder::Base;
use ProjectBuilder::Conf;
use ProjectBuilder::Distribution;
use ProjectBuilder::Display;
use MondoRescue::LVM;
use MondoRescue::Base;
use MondoRescue::DynConf;
use MondoRescue::Mini::Base;

=pod

=head1 NAME

mrmini - Tool to create a boot environment from a distribution

=head1 DESCRIPTION

B<mrmini> creates a bootable ISO/USB image using files from the system it runs on. B<mrmini> will try hard to reproduce the environment of its host system including loaded modules to ensure that the system can be booted properly from the created rescue media. B<mrmini> is used by monodarchive(8) to produce the required USB/ISO images but can also be used stand-alone.

For stand-alone usage, B<mrmini> may be called without any parameters or switches. It will then interactively ask the user for all information required to create a set of boot/root media. Options on the command line or a configuration file can also be used to alter the way B<mrmini> is working

The probably more frequent way of calling B<mrmini> is non-interactively from mondoarchive(8) using a dedicated configuration file.

=head1 SYNOPSIS

mrmini [-v]|[-q]|[-h]|[--man]

=head1 OPTIONS

=cut

# Handle options
#

=pod
=over 4
=item B<-v|--verbose>

Print a brief help message and exits.

=item B<-q|--quiet>

Do not print any output.

=item B<-h|--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=item B<-i|--iso iso_image>

Name of the ISO image you want to created.

=item B<-u|--usb usb_device>

Name of the USB device on which you want to created your image.

=item B<-t|--tape tape_device>

Name of the Tape device on which you want to created your image.

=item B<-o|--obdr>

Activate OBDR mode for tape (Bootable tape devices)

=item B<-V|--version>

Display mrmini version and exit

=item B<-f|--force>

Force usage of defaults parameters or values without asking questions

=item B<-p|--printvars variable>

Prints the value of the variable passed as parameter

=cut

# Global variables
my %opts;					# CLI Options

GetOptions(
		"verbose|v+" => \$opts{'v'},
		"quiet|q" => \$opts{'q'},
		"help|?|h" => \$opts{'h'}, 
		"man" => \$opts{'man'},
		"iso|i=s" => \$opts{'i'},
		"usb|u=s" => \$opts{'u'},
		"tape|t=s" => \$opts{'t'},
		"obdr|o" => \$opts{'o'},
		"version|V" => \$opts{'V'},
		"force|f" => \$opts{'f'},
		"printvar|p=s" => \$opts{'p'},
		"log-files|l=s" => \$opts{'l'},
) || pb_syntax(-1,0);

=pod
=back 

=head1 WEB SITES

The main Web site of the project is available at L<http://www.mondorescue.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.

=head1 USER MAILING LIST

The miling list of the project is available at L<mailto:mondo@lists.sf.net>

=head1 CONFIGURATION FILES

The system administrator may have a configuration file in F<$HOME/.mondorescue>. The values in this file may overwrite any other configuration file value. 

Here is an example of such a configuration file:

 # mrcachedir points to the directory where the tool will store generated content
 # If not defined, mrcachedir is under /var/cache/mrmini
 mrcachedir mrmini = /var/cache/mrmini

 Also look at man mrmini.conf
 
=head1 AUTHORS

The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.

=head1 COPYRIGHT

mrmini is distributed under the GPL v2.0 license
described in the file C<COPYING> included with the distribution.

=cut



# ---------------------------------------------------------------------------

# Initialize the syntax string

#
# Global variables
#
my $proj;
($mr->{'confdir'},$proj) = mr_dynconf_init();
my @date = pb_get_date();
$mr->{'start_date'} = strftime("%Y-%m-%d %H:%M:%S", @date);

$ENV{'PBPROJ'} = $proj;
$ENV{'PBPKG'} = "mrmini";

# Adds conf files in order
pb_conf_add("$mr->{'confdir'}/mondorescue.conf.dist","$mr->{'confdir'}/mondorescue.conf");
($mr->{'install_dir'},$mr->{'version'}) =  mr_conf_get("mr_install_dir","mr_version");

pb_syntax_init("$ENV{'PBPKG'} Version $mr->{'version'}\n");
pb_display_init("text","");

# easy options
if (defined $opts{'h'}) {
	pb_syntax(0,1);
}
if (defined $opts{'man'}) {
	pb_syntax(0,2);
}
if (defined $opts{'p'}) {
	pb_display("$ENV{$opts{'p'}}\n");
	exit(0);
}

if (defined $opts{'v'}) {
	$pbdebug = $opts{'v'};
}

my $force = 0;

if (defined $opts{'f'}) {
	$force=1;
}
if (defined $opts{'q'}) {
	$pbdebug=-1;
}
my $iso;

if (defined $opts{'i'}) {
	$iso = $opts{'i'};
}
my $logfile = undef;
if (defined $opts{'l'}) {
	$logfile = $opts{'l'};
}

# 
# Temp dir
#
pb_temp_init();
mr_mini_main($logfile);
mr_exit(0);
