#!/usr/bin/perl -I /home/warly/files/cvs/cooker/soft/mkcd/pm/
#
# to prepare, create and burn iso images
#

my $VERSION = "3.6.1";

use strict;
use File::NCopy qw(copy);       
use Mkcd::Commandline qw(parseCommandLine usage);
use Mkcd::Tools qw(printTable getTracks du cpal checkcds cleanrpmsrate config compute_md5 include_md5 convert_size);
use Mkcd::Group;
use Mkcd::Package qw(check_rpmsrate packageOutOfRpmsrate getLeaves list_hdlist mkcd_build_hdlist %ARCH);
use packdrake;

#
# FIXME not up to date
#
# config structure
#
# $config{name} = name for the product
#
# $config{list} = list data
#   $config{list}[list number]{name} = list name
#                             {filelist} = ( file list location 1, file list location 2, ..., file list location n )
#
#   $config{list}[list number]{packages}[location i] = (RPMS location i, SRPMS location i) 
#
#   $config{list}[list number]{done}
#                             {empty}
#                             {auto}
#                             {cd}
#                             {sources}
#
#   $config{list}[list number]{disc} = { cd => { rep => { options }}  }
#
# $config{disc} = disc data
#   $config{disc}[disc number]{size} = size in bytes
#   $config{disc}[disc number]{serial} = serial number
#   $config{disc}[disc number]{name} = disc number irl
#   $config{disc}[disc number]{longname} = disc long name
#   $config{disc}[disc number]{fastgeneric} = [ generic data 1, generic data 2, ..., generic data n]
#
#   $config{disc}[disc number]{function}{list}[function number] = (function name, { data })
#
#   $config{disc}[disc number]{function}{data}{dir}{repository identifier} = $config[2][cd number][1][function number]
#
#   $config{disc}[disc number]{function}{data}{'installation'} = $config[2][cd number][1][function number]  it should have only one installation by disc, anyway
#
#   $config{disc}[disc number]{function}{data}[2]{'advertising'} = $config[2][cd number][1][function number]
#
#   $config{disc}[disc number]{steps} = function to execute to build the disc
#
#
# $config other values:
#
#    discMax = higher real disc number 
#    configfile = config file use for this session 
#    lists
#    fast
#    nodeps
#    verbose
#    print
#    printscript
#    nolive
#    noiso
#    deps
#    nosrcfit
#    product
#    bugzilla
#    builddir
#    topdir
#    discsize
#    isodir
#    filetag
#    log
#    mkisoopt
#    tmp
#

#
# Availaible functions
#
#   see Functions.pm
#

# old -> new schema
#
# config structure
#
# $config[0][0] -> $config{name}
#
# $config[1] -> $config{list}
#   $config[1][list number][0] ->  $config{list}[list number]{filelist}
#
#   $config[1][list number][1][location i] -> $config{list}[list number]{packages}[location i]
#
#   $config[1][list number][2] -> $config{list}[list number]{done}, $config{list}[list number]{empty} $config{list}[list number]{cd}$config{list}[list number]{auto}
#
#   $config[1][list number][3] -> $config{list}[list number]{disc}
#
# $config[2] -> $config{disc}
#   $config[2][cd number][0] -> $config{disc}[disc number]{size}, $config{disc}[disc number]{serial}, $config{disc}[disc number]{name}, $config{disc}[disc number]{longname}, $config{disc}[disc number]{fastgeneric} 
#
#   $config[2][cd number][1][function number] -> $config{disc}[disc number]{function}{list}[function number]
#
#   $config[2][cd number][2]{dir}{repository identifier} -> $config{disc}[disc number]{function}{data}{dir}{repository identifier}
#
#   $config[2][cd number][2]{'installation'} -> $config{disc}[disc number]{function}{data}{'installation'}
#
#   $config[2][cd number][2]{'advertising'} -> $config{disc}[disc number]{function}{data}[2]{'advertising'}
#
#   $config[2][cd number][3] -> $config{disc}[disc number]{steps}
#
#
# $config[3] -> $config{discMax}, $config{configfile}
#
#

our %FUNCTIONS;
our $functions;
our $optimize;
our $LOG;

our $topdir = `pwd`;
chop $topdir;
our %config = (
    lists => [],
    fast => 0,
    nodeps => 0,
    verbose => 0,
    print => 0,
    printscript => 0,
    nolive => 0,
    noiso => 0,
    deps => 0,
    nosrcfit => 0,
    nosrc => 0,
    product => 0,
    bugzilla => 0,
    builddir => 0,
    topdir => $topdir,
    discsize => 681000000,
    isodir => 0,
    filetag => 0,
    log => 0,
    mkisoopt => "-r -J -hide-rr-moved -nobak -cache-inodes",
    tmp => $ENV{TMPDIR} || "$topdir/tmp",
    disc_building_tries => 3,
    rejected_options => { 
	no_disc => "Could not add more disc for this package",
	no_space => "Not enough space",
	deps => "Missing dependencies",
	old_version => "More recent version found",
	deps_rejected => "Some needed dependencies rejected",
	excluded => "Explicitely excluded",
	order_pb => "Needed dependency could not be put before",
	sequential => "Could not add interlist dependencies in sequential mode",
	autodeps => "not selected in rpm lists"
    },
    optimize_space => 1,
    ARCH => \%ARCH
);

$config{group} = new Mkcd::Group(\%config);
$functions = $config{group}{disc}{functions}{functions};
my $arg = @ARGV;
our @params;
@params = ( 
    #    [ "one letter option", "long name option", "number of args (-X means ´at least X´)", "help text", "function to call", "log info"]
    [ "", "mkcd", 0, "<options>", "mkcd Mandrake Linux Disc maker", sub { $arg or usage('mkcd', \@params) }, "" ],
    [ "a", "auto", [
	["", "auto", -1, "<repository> <extra RPMS directory 1> <extra RPMS directory 2> ... <extra RPMS directory n>", "Auto mode configuration", 
	sub { 	
            my ($tmp, @arg) = @_;
	    $tmp->[0] ||= {};
	    push @$tmp, @arg;
	    1
	}, "Setting auto mode arguments"],
	["c", "cd", 1, "<number of discs>", "Max number of discs", sub { my ($tmp, $cd) = @_; if ($cd =~ /\d+/) { $tmp->[0]{cd} = $cd } else { return 0 }; 1 }, "Setting max number of discs"],
	["s", "sources", [
	  [ "", "sources", 0, "<options>", "Sources mode options", 
	    sub {   my ($tmp) = @_;
	        $tmp->[0] ||= {};
	        1
	    }, "Setting source mode options" ],
	  [ "s", "separate", 0, "", "Separate binaries discs from sources discs", sub { my ($tmp) = @_; $tmp->[0]{separate} = 1 }, "Setting source mode separate option" ]
	], "", "Create SRPMS discs too", sub { my ($tmp, $opt) = @_; $tmp->[0]{sources} = $opt }, "Setting source mode"],
	["", "noisolinux", 0, "", "Do not use a isolinux boot", sub { my ($tmp) = @_; $tmp->[0]{noisolinux} = 1 }, "Setting noisolinux option"]
], "[options] <repository> <extra RPMS directory 1> <extra RPMS directory 2> ... <extra RPMS directory n>", "Automated mode, build discs from a repository.", \&auto_mode, "Auto mode" ],
    [ "", "addmd5", 1, "<iso file>", "Add md5sum to iso header.", sub { my $err = include_md5(pop, 1, 1); print $err }, "Adding md5 to ISO header" ],
    [ "k", "checkmd5", 1, "<iso file>", "Checking md5sum of iso header.", sub { my $err = include_md5(pop, 0, 1); print $err }, "Checking md5 of ISO header" ],
    [ "", "bugzilla", 0, "", "Use bugzilla as information source.", sub { $config{bugzilla} = 1 }, "Using Bugzilla" ],
    [ "b", "builddir", 1, "<build dir>", "Where live iso image are created (default current dir).",  sub { $config{builddir} = pop @_ }, "Setting the build directory" ],
    [ "", "buildhdlist", -1, "<rpms dir 1> <rpms dir 2> ... <rpms dir n>", "Build hdlist.cz files for given directories.",  
    sub {
	my $i;
	mkcd_build_hdlist(1 + @_, [ 0, map { $i++; { rpms => [ glob "$_/*.rpm" ], hdlist => "./hdlist$i.cz", synthesis => "./synthesis.hdlist$i.cz" } } @_ ], "$config{tmp}/.build_hdlist")
    }, "building hdlist files" ],
    [ "", "batch", 2, "<discs list> <batch file>", "batch mode to rebuilt discs from a previous session.",  \&batchMode, "Batch mode" ],
    [ "c", "catto", 1, "<log file>", "Log file.", sub { $config{log} = pop @_; open $LOG, ">$config{log}" or die "unable to open $config{log}\n" }, "Log file" ],
    [ "", "listrpmsrate", 1, "<rpmsrate file>", "List the package in the rpmsrate file", \&packageOutOfRpmsrate, "Listing rpmsrate file" ],
    [ "", "checkrpmsrate", -2, "<rpmsrate file> <rpms dir 1> <rpms dir 2> ... <rpms dir n>", "List the package in the rpmsrate file and not in the given repositories", \&check_rpmsrate, "Checking rpmsrate file" ],
    [ "d", "depslist-creation", 0 , "", "rebuild the desplist.ordered file before checking the list.", sub { $config{deps}=1 }, "Depslist creation switch" ],
    [ "", "discsize", 1 , "<disc size in bytes>", "Select a custom disc size (default $config{discsize}).", sub { $config{discsize} = convert_size(pop, $config{discsize}, $config{LOG}) }, "Custom disc size selection" ],
    [ "f", "fast", 0 , "", "fast mode.", sub { 
	$config{fast} = 1;
	$config{disc_building_tries} = 1;
	$optimize = 0;
	$config{optimize_space} = 0
    }, "Fast mode" ],
    [ "", "getleaves", 1, "<depslist file>", "Getting leaves from a depslist.ordered file", \&getLeaves, "Getting leaves from a depslist.ordered" ],
# FIXME function help should take 0 or one argument, but this is not possible with this structure
    [ "h", "help", -1, "<path> <to> <the> <function>", "Display help, eg. mkcd -h installation fixed. Type mkcd -h config for configuration files options.", 
    sub { 
	my (@function) = @_; 
	if (@function) { 
	    my $key = join '/', @function; 
	    if (ref $FUNCTIONS{$key}) {
		usage($key, $FUNCTIONS{$key})
	    }
	}
	usage("mkcd", \@params);
	}, "" ],
    [ "", "check", -1, "<dir 1> <dir 2> ... <dir n>", "Check the hdlists, depslist and RPMS consistency.", sub { checkcds(@_) }, "Checking the hdlists, depslist and RPMs consistency" ],
    [ "K", "checkdisc", 2, "<ISO mount point> <md5 file>", "Check the disc.", sub { check_discs(@_) }, "Checking the disc" ],
    [ "i", "isodir", 1, "<iso dir>", "Where ISOs are built (default ./iso/product_name/).",  sub { $config{isodir} = pop @_ }, "Setting the iso directory" ],
    [ "l", "lists", 1 , "", "lists of discs taken into account.", sub { $config{lists} = getTracks(pop @_) }, "Using given disc list" ],
    [ "m", "make", 1, "<cds number>", "Build the discs.", \&make , "Building the discs" ],
    [ "", "nodeps", 0, "", "Do not include automatically dependencies of packages", sub { $config{nodeps} = 1 }, "Setting nodeps flag" ],
    [ "", "nolive", 0, "", "Do not create live image of the discs.", sub { $config{nolive} = 1 }, "Setting nolive option" ],
    [ "", "noiso", 0, "", "Do not create iso images of the discs.", sub { $config{noiso} = 1 }, "Setting noiso option" ],
    [ "", "nosrcfit", 0, "", "Do not stop if sources discs are full", sub { $config{nosrcfit} = 1 }, "Setting nosrcfit option" ],
    [ "", "nosrc", 0, "", "Do not include sources", sub { $config{nosrc} = 1 }, "Setting nosrc option" ],
    [ "", "oem", -1, "<root of disc 1> <root of disc 2> ... <root of disc n>", "Build a OEM installation CD based on the given disc", \&oem, "Building oem disc" ],
    [ "p", "printscript", 1, "<script file>", "Print the script that can be use to rebuild the discs", sub { $config{printscript} = shift }, "Printing script" ],
    [ "", "printdiscsfiles", 1, "<file>", "Print the contains of each disc", sub { $config{print} = shift }, "Printing disc contains" ],
    [ "", "pl", -1, "<hdlist 1> <hdlist 2> ... <hdlist n>", "Do a packdrake -l on the hdlists", sub { list_hdlist(\@_,1) }, "Printing hdlist contents" ],
    [ "s", "spec", 1, "<config file>", "Configuration file", sub { config(shift, \%config,$functions) } , "Loading configuration file" ],
    [ "t", "topdir", 1, "<top dir>", "Where files are created (default current dir).",  sub { $config{topdir} = pop @_; $config{tmp} = $ENV{TMPDIR} || "$config{$topdir}/tmp" }, "Setting the top directory" ],
    [ "", "update-rpmsrate", -2, "<rpmsrate> <rpms directory 1> <rpms directory 2> ... <rpms directory n>", "Add major to libraries in rpmsrate",  
	sub { 
	    my %rpm;
	    my $rpmsrate = shift @_;
	    foreach my $d (@_) { $rpm{$d} = [ map { s,$d/?(.*)\.rpm$,$1,; $_ } glob "$d/*.rpm" ] };
	    cleanrpmsrate($rpmsrate, 0, 0, \%rpm) 
	}, "Adding major to libraries in rpmsrate" ],
    [ "", "verbose", 1 , "<log level>", "Print more messages (the higher the more, 5 is the higher)", sub { $config{verbose} = shift; 1 }, "Setting the verbose flag" ],
    [ "v", "version", 0, "", "Print program version",  sub { print_fh($config{LOG}, "\nmkcd version $VERSION\n"); 1 }, "" ],
    [ "", "disc_building_tries", 1 , "<maximum number of iteration to build correct ISO size>", "Set the number of iterations when trying to adjust ISO size", sub { $config{disc_building_tries} = shift; $optimize = 1 }, "Setting the disc_building_tries value" ],
    [ "", "use_optimize_space", 1 , "<number of disc building tries>", "Use optimize_space algorythm", 
    sub { 
	my $t = shift; 
	$config{disc_building_tries} = $t; 
	$optimize = 1;
	$config{optimize_space} = $t
        }, "Setting the disc_building_tries value" ],
    [ "P", "Publisher", 1 , "<publisher name>", "Set the publisher name for ISO header publisher_id (128 char max)", sub { $config{Publisher} = substr shift, 0, 128 }, "Setting the publisher ID flag" ],
#    [ "", "test", 0 , "", "Set the publisher name for ISO header publisher_id (128 char max)", sub { print "ARCH ", keys %{$config{ARCH}} , "\n\n" }, "Setting the publisher ID flag"],
);

# FIXME this permit to have specific help
foreach (@params) {
    $FUNCTIONS{$_->[1]} = ref($_->[2]) ? $_->[2] : [ $_ ]
}

$FUNCTIONS{mkcd} = \@params;
foreach my $k (keys %$functions) {
    $FUNCTIONS{$k} = $functions->{$k};
    foreach (@{$functions->{$k}}) {
	$FUNCTIONS{"$k/$_->[1]"} = ref($_->[2]) ? $_->[2] : [ $_ ]
    }
    push @{$FUNCTIONS{config}} , $functions->{$k}[0]
}

open($LOG, ">&STDERR");	
$config{LOG} = $LOG;

my $todo = parseCommandLine("mkcd", \@ARGV, \@params);
@ARGV and usage("mkcd", \@params, "@ARGV, too many arguments");
foreach my $t (@$todo)  {
    print $LOG "mkcd: $t->[2]\n";
    &{$t->[0]}(@{$t->[1]}) or print $LOG "ERROR: $t->[2]\n";
}

# FIXME only to make perl_checker happy
sub print_fh {
    my ($fh, $text) = @_;
    print $fh $text
}

sub batchMode {
    my ($cds, $file) = @_;
    config($file, \%config,$functions);
    my ($discsFiles, $cd) = readBatchFile($file);
    (my $lists,$cds) = getDiscsList($cds);
    my @mkisos;
    my @size;
    Mkcd::Disc::makeDiscs(0,$lists,$cds, \@size, \@mkisos,$discsFiles);
    Mkcd::Disc::makeDiscs(1,$lists,$cds, \@size, \@mkisos,$discsFiles,$cd);
}

sub getDiscsList {
    my ($cds) = @_;
    $cds = getTracks($cds);
    print $LOG "getDiscList: discs @$cds\n";
    my %list;
    $cds = [grep { if (ref $config{disc}[$_]) { $list{$_} = 2; push @{$config{lists}}, $_; 1 } else { print $LOG "WARNING: disc $_ not defined\n"; 0 } } @$cds];
    $config{lists} ||= $cds;
    $config{lists} = [grep { $list{$_} or ref $config{disc}[$_] and $list{$_} = 1 or print $LOG "WARNING: disc $_ not defined\n" and 0 } @{$config{lists}}];
    foreach (@{$config{virtual_disc}}) {
	$list{$_} = 1;
	push @{$config{lists}}, $_
    }
    return (\%list,$cds)
}

sub auto_mode {
    my ($opt,$repository, @rpms) = @_;
    $config{nolive} = 1;
    $config{nosrcfit} = 1;
    $config{deps} = 1;
    if (!$optimize) {
	$config{disc_building_tries} = 1;
	$config{optimize_space} = 0;
    }
    -d "$repository/Mandrake" or print "ERROR: $repository/Mandrake does not exist\n" and return 0;
    my $dir = "$repository/Mandrake";
    local *DIR; opendir DIR, $dir;
    my $size;
    foreach (readdir DIR) {
	-d "$dir/$_" or next;
	m/RPMS(\d*)$/ or next;
	print $LOG "auto_mode: adding $dir/$_\n";
	unshift @rpms, "$dir/$_"
    }
    my ($name, $tag);
    if (-f "$repository/VERSION") {
	open my $A, "$repository/VERSION";
	<$A> and /^Mandrake Linux (.*) \d{8} \d{2}:\d{2}$/ and ($name,$tag) = split ' ', $1;
	close $A
    }
    $name ||= "Cooker-download";
    $config{name} = $name;
    foreach (keys %$opt) { $config{list}[1]{$_} = $opt->{$_} }
    $config{list}[1]{auto} = 1;
    if (-d "$repository/Mandrake/base") {
	opendir my $basedir, "$repository/Mandrake/base";
	foreach my $file (readdir $basedir) {
	    $file =~ /^pubkey/ or next;
	    push @{$config{list}[1]{keyfiles}}, "$repository/Mandrake/base/$file"
	}
	closedir $basedir
    }
    foreach (keys %{$config{list}[1]}) { print $LOG "auto_mode: list options $_ -> $config{list}[1]{$_}\n" }
    foreach (@rpms) {
	#	$size += du($_);
	push @{$config{list}[1]{packages}}, { rpm => [ $_ ], srpm => \@rpms }
    }
    my %cd = (1 => 2);
    my @cd = 1;
    #print $LOG "Total RPMS $size\n";
    $config{disc}[1]{size} = $config{discsize};
    $config{disc}[1]{serial} = "${name}-disc1";
    $config{disc}[1]{name} = 1;
    $config{disc}[1]{longname} = "MandrakeLinux $name";
    $config{disc}[1]{appname} = "MandrakeLinux $name disc 1";
    $config{disc}[1]{label} = substr "MandrakeLinux-$name-1.i586", 0, 32;
    my $idx = 1;
    my %idx;
    &{$functions->{dir}[0][5]}(1,$idx, "rpms", "Mandrake/RPMS");
    $idx++;
    &{$functions->{generic}[0][5]}(1,$idx, "rpms",1);
    &{$functions->{generic}[2][5]}(1,$idx);
    $idx++;
    $idx{installation} = $idx;
    &{$functions->{installation}[0][5]}(1,$idx);
    &{$functions->{installation}[5][5]}(1,$idx, $repository);
    &{$functions->{installation}[10][5]}(1,$idx, $tag);
    &{$functions->{installation}[14][5]}(1,$idx);
    &{$functions->{installation}[18][5]}(1,$idx, "1/rpms");
    $idx++;
    &{$functions->{boot}[0][5]}(1,$idx);
    if (-w "$repository/isolinux/isolinux.bin" && !$opt->{noisolinux}) {
	&{$functions->{boot}[1][5]}(1,$idx, { isolinux => 1 }, "isolinux");
	&{$functions->{boot}[2][5]}(1,$idx, { bootimg => 1 }, "isolinux/isolinux.bin");
	&{$functions->{boot}[5][5]}(1,$idx, "$repository/isolinux");
	$idx++;
	&{$functions->{cp}[0][5]}(1,$idx, "$repository/images", "images/");
    } else {
	&{$functions->{boot}[2][5]}(1,$idx, { bootimg => 1, dir => "Boot" }, "images/cdrom.img");
	&{$functions->{boot}[4][5]}(1,$idx, "$repository/images");
    }
    if ($opt->{sources}) {
	$config{nosrcfit} = 0;
	if ($opt->{sources}{separate}) {
	    $config{disc}[2]{size} = $config{discsize};
	    $config{disc}[2]{serial} = "${name}-2-src";
	    $config{disc}[2]{name} = 2;
	    $config{disc}[2]{longname} = "MandrakeLinux $name sources";
	    $config{disc}[1]{appname} = "MandrakeLinux $name sources disc 2";
	    push @cd,2;
	    $cd{2} = 2;
	    &{$functions->{dir}[0][5]}(2,1, "srpms", "Mandrake/SRPMS");
	    &{$functions->{generic}[0][5]}(2,2, "srpms",1);
	    &{$functions->{generic}[1][5]}(2,2, { source => 1 });
	    &{$functions->{installation}[17][5]}(1, $idx{installation}, "2/srpms")
	} else {
	    $idx++;
	    &{$functions->{dir}[0][5]}(1,$idx, "srpms", "Mandrake/SRPMS");
	    $idx++;
	    &{$functions->{generic}[0][5]}(1,$idx, "srpms",1);
	    &{$functions->{generic}[1][5]}(1,$idx, { source => 1 });
	    &{$functions->{installation}[17][5]}(1, $idx{installation}, "1/srpms")
	}
    } else {
     	&{$functions->{installation}[6][5]}(1,$idx)
    }
    printTable(\%config);
    $config{group}->makeWithGroups(\%cd, \@cd);
    1	
}

sub make {
    my ($cds) = @_;
    $config{group}->makeWithGroups(getDiscsList($cds));
    1
}		

sub oem {
    my (@cds) = @_;
}

sub check_discs {
    my ($mntpt, $mdfile) = @_;
    local *A; open A, $mdfile;
    my %ignore;
    my $sum;
    while (<A>) {
	chomp;
	last if ($sum) = /^(.*) - /;
	my $t = "/$_";
	$t =~ s,//+,/,g;
	$ignore{$t} = 1
    }
    close A;
    my $hexdigest = compute_md5([[ "/", $mntpt ]], \%ignore);
    if ($hexdigest eq $sum) {
	print "\nOK ($hexdigest)\n"
    } else {
    	print "\nFAILED (computed $hexdigest <> expected $sum)\n";
	return 0
    }
    1
}

#
# Changeloh
#
# 2002 03 15
# new sources handling
#
# 2002 03 19
# cdcom are now handled as normal list, so that deps are not forced.
#  
# 2002 03 23
# change Group.pm getAlone to work with alone group.
#
# 2002 03 29
# fix a bug in Functions.pm for nolive mode and rpmsrate
#
# 2002 04 12
# oem mode
#
# 2002 05 02
# add separate mode for auto sources mode
#
# 2002 05 07
# add check_discs
#
# 2002 05 09
# check_discs, compute_md5, md5_add_tree
# 
# 2002 05 13
# fix fentry problem in List.pm that create the "needed spreading" problem
#
# 2002 06 01
# first draft for new needed code
# new perl-URPM
#
# 2002 06 01
# fix perl-URPM integration
#
# 2002 06 15
# begin new diff mode.
#
# 2002 08 12
# change clean_rpmsrate
#
# 2002 08 19
# start optimize_space for sources moving
# change diff structure
# change process_diff
#
# 2002 08 20
# some interlist and intergroup binaries moving in optimize_space
#
# 2002 08 25
# various update of optimize_space
# new needed and deps handling in optimize_space
#
# 2002 08 29
# prepare_cloned_disc enhanced
# add disc_prereq data in groups for cloned discs
# 
# 20020918
# fixes in optimize_space
#
# 20020930
# fix help mode for options with extra options such as auto
#
# 20030401
# include new list_hdlist
# add virtual disc for hdlist only virtual CD
