#!/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



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

# Catch signals
$SIG{INT} = \&mr_exit;
$SIG{QUIT} = \&mr_exit;
$SIG{ABRT} = \&mr_exit;
$SIG{KILL} = \&mr_exit;
$SIG{TERM} = \&mr_exit;

#
# Global variables
#
mr_init();
$ENV{'PBPKG'} = "mrmini";

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

# Initialize the syntax string
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'}) {
	print("$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'}) {
	# Log file forced externally
	$logfile = $opts{'l'};
} elsif ($pbdebug ge 1) {
	# Log file forced internally to default value as we are in debug mode
	($logfile) =  mr_conf_get("mr_logfile");
}

if (defined $logfile) {
	open(pbLOG,"> $logfile") || die "Unable to log to $logfile: $!";
	$mr->{'logdesc'} = \*pbLOG;
} else {
	$mr->{'logdesc'} = undef;
}

pb_log_init($pbdebug,$mr->{'logdesc'});
mr_mini_main();
mr_exit(0);
