#!/usr/bin/perl -w
#
# $Id$
# Copyright B. Cornec 2016-today
# Provided under the GPL v2
#
# Create an ISO image for the MondoRescue project
#
use strict 'vars';
use ProjectBuilder::Base;
use ProjectBuilder::Version;
use ProjectBuilder::Distribution;
use ProjectBuilder::Conf;
use MondoRescue::Version;
use MondoRescue::Base;
use Data::Dumper;
use Getopt::Long qw(:config auto_abbrev no_ignore_case);
use English;
use Carp qw/confess cluck/;

=pod

=head1 NAME

mr-mkiso Create an ISO image

=head1 DESCRIPTION

mr-mkiso Create an ISO image for the MondoRescue project

=head1 SYNOPSIS

mr-mkiso [-v][-h] -o target.iso [options] directories

=head1 ARGUMENTS

=over 4

=item B<directories>

These are the directories whose content needs to be used tobuild the ISO

=back 

=head1 OPTIONS

=over 4

=item B<-o|--output>

Name of the ISO image to produce

=item B<options>

additional options that will be passed to the tool used to create the ISO (mkisofs, genisoimage, xorriso, ...)

=item B<-v|--verbose>

Increase verbosity

=item B<-h|--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=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

For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>

=head1 AUTHORS

The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.

=head1 COPYRIGHT

MondoRescue is distributed under the GPL v2.0 license or later,
described in the file C<COPYING> included with the distribution.

=cut

# Global variables
my ($mrver,$mrrev) = mr_version_init();
my $appname = "mr-mkiso";
my %opts;					# CLI Options

# Initialize the syntax string

pb_syntax_init("$appname Version $mrver-$mrrev\n");

GetOptions("help|?|h" => \$opts{'h'}, 
		"man" => \$opts{'man'},
		"output|o" => \$opts{'iso'},
		"verbose|v+" => \$opts{'v'},
) || pb_syntax(-1,0);

my $iso = undef;

if (defined $opts{'h'}) {
	pb_syntax(0,1);
}
if (defined $opts{'man'}) {
	pb_syntax(0,2);
}
if (defined $opts{'v'}) {
	$pbdebug++;
}
if (defined $opts{'iso'}) {
	$iso = $opts{'iso'};
} else {
	pb_syntax(0,1);
	die "No name given for the ISO to generate";
}

mr_init();
my $pbos = pb_distro_get_context();
# Get the mode we want to use to create the ISO
my ($mode) = pb_distro_get_param($pbos,pb_conf_get_if("mr_iso_mode"));
confess "Unable to find mr_iso_mode in configuration file, please add it" if (not defined $mode);

if ($mode eq "best") {
	# If xorriso exists then use it
	if (defined pb_check_req("xorriso",0)) {
		$mode = "xorriso";
	} else {
		# If not then use the default command
		$mode = "mkisofs";
	}
}
# Now get the parameters linked to that mode
my ($isocmd,$optgen,$optmindi,$optmr) = pb_distro_get_param($pbos,pb_conf_get_if("mr_cmd_$mode","mr_opt_gen_$mode","mr_opt_mindi_$mode","mr_opt_mr_$mode"));

pb_log(0,"mode = $mode\nmr_cmd_$mode = $isocmd\nmr_opt_gen_$mode = $optgen\nmr_opt_mindi_$mode = $optmindi\nmr_opt_mr_$mode = $optmr\n");
