#!/usr/bin/perl
#
# by Danie Roux <droux@tuks.co.za>

use strict;
use File::stat;

my $dir = '/mnt/tlo/backup/class_Nov13_2002';
# WARNING!!! Everthing in this directory will get deleted!!!!
my $tmp_dir = '/tmp/cd_create';
# Where to create the iso
my $iso_dir = '/tmp';
my $cdrecord = "nice --19 cdrecord -v fs=6m dev=0,0,0 driveropts=burnfree -eject -multi -";
my $mkisofs = "nice --19 mkisofs -J -r";

print "Now going to delete the temporary directory. Press Ctrl-C to stop me ...\n";

<STDIN>;

system "rm -rf $tmp_dir";

mkdir $tmp_dir || die "Could not create temp directory $tmp_dir\n";

chdir $dir;

opendir DIR, $dir || die "Could not open directory";

my $CDSIZE = 650*1024*1024;

my @cds;

&create_cd_lists();
&create_cds();

# CD's is an array of arrays. First element of each array is the
# cumulative size of the files in the array.
sub create_cd_lists {

    my %files;
    # Get files and sizes
    while (my $file = readdir DIR)
    {
        if ($file ne "." && $file ne "..")
        {
            my $size = (stat $file)->size;
            die "File too big: $file" if ($size > $CDSIZE);
            $files{$size} = $file;
        }
    }

    # Put on biggest first

    my @sorted = sort {$b <=> $a} keys %files;
    my $cd_count=0;
    my $ref = &new_cd($cd_count, @cds);

    my $elem = 0; # First element

    while (length (@sorted) > 0)
    {
        if (($elem < $#sorted)  && (${$ref}[0] + $sorted[$elem] < $CDSIZE))
        {
            #print "Adding ".$files{$sorted[$elem]};
            #print "Adding ".$sorted[$elem]."\n";
            ${$ref}[0] += $sorted[$elem];
            push @{$ref}, $files{$sorted[$elem]};
            splice @sorted, $elem, 1;
            #print "\t".$#sorted."\t".$sorted[0]."\n";
        }
        else
        {
            if ($elem < $#sorted) # Try next file
            {
                $elem++;
            } else
            {
                # DONE.
                return @cds if ($#sorted == 0);

                $cd_count++;
                $ref = &new_cd($cd_count, @cds);
                $elem = 0;
            }
        }
    }
}

sub new_cd {
    my $cd_count = shift;

    #print "Creating CD: ".($cd_count+1)."\n";
    $cds[$cd_count] = [];
    my $ref = $cds[$cd_count];
    push @{$ref}, 0; # Size of the files in this array
    return $ref;
}

sub create_cds {
    my $cd_count = 0;

    print "Creating ISOS\n";

    foreach my $cd (@cds)
    {
        $cd_count++;
        my @arr = @{$cd};
        print "CD ".$cd_count." is ".$arr[0]." big \n";

        print "Can I start copying it to the temporary directory?";

        $_ = <STDIN>;
        if (/y/)
        {

            for (my $i = 1; $i < $#arr; $i++)
            {
                my $file_name = ${$cd}[$i];
                print "Doing file: ".$file_name."\n";
                system "cp $file_name $tmp_dir" || die "Could not copy file\n";
            }

            print "Ready to start writing, please enter a CD and press y and enter. Any other key will skip this CD \n";

            $_ = <STDIN>;

            if (/y/)
            {
                while (system "$mkisofs $tmp_dir | $cdrecord")
                {
                    print "Possible problem with recording, press enter to try again \n";
                    $_ = <STDIN>;
                }
            }
        }

        print "Press enter when ready for next one or Ctrl-C to quit\n";
        $_ = <STDIN>;

        system "rm -rf $tmp_dir/*";
    }
}
