<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w

package DVD::Maker;

use strict;

use Getopt::Long qw(GetOptions);
use File::Temp qw(tempdir);
use File::Find;
use File::Path qw(rmtree mkpath);
use File::Spec;
use File::Copy;
use File::Basename;

our $DEBUG = 0;

sub usage {
    print &lt;&lt;EOT;
Usage: $0 [options] Folder

    -h | --help         This screen
    -d | --debug        Debug output
    -n | --name         Name added to the DVD
    -e | --skip_empty   Skip emtpy folders
    
EOT
    exit;
}

__PACKAGE__-&gt;run unless caller;


sub new {
    my $class = shift;
    return bless {}, $class;
}

sub run {
    my $class = shift;
    
    my $self = $class-&gt;new();
    
    my $name = "";
    my $max_size = 4_508_876 * 1024;
    my $skip_empty = 0;
    
    GetOptions(
        "h|help" =&gt; \&amp;usage,
        "d|debug" =&gt; \$DEBUG,
        "n|name" =&gt; \$name,
        "s|size" =&gt; \$max_size,
        "e|skip_empty" =&gt; \$skip_empty,
        );
    
    $self-&gt;{name} = $name;
    $self-&gt;{max_size} = $max_size;
    $self-&gt;{skip_empty} = $skip_empty,
    
    my $dir = shift(@ARGV) || usage();
    $self-&gt;{folder} = File::Spec-&gt;rel2abs($dir);
    $self-&gt;{name} ||= basename($self-&gt;{folder});
    
    die "Not a folder: $dir\n" unless -d $self-&gt;{folder};
    
    $self-&gt;process_dir();
    
    exec("open", "-a", "Disk Utility", &lt;Disk_*.iso&gt;);
}

#mkisofs -J -r -o TitanicCD.iso -V Titanic TitanicCD/

sub process_dir {
    my $self = shift;
    
    my $dir = $self-&gt;{folder};
    
    open(my $du, "-|", "du", "-sk", $dir) || die "du failed: $!";
    chomp(my $size = &lt;$du&gt;);
    close($du);
    
    $size =~ s/\s+.*$//;
    
    $size *= 1024;
    
    dbg("size of $dir: $size");
    
    if ($size &lt; $self-&gt;{max_size}) { # approx 4.3GB - stop short of DVD size
        dbg("making ISO Disk_1.iso");
        system("mkisofs", "-J", "-r", "-o", "Disk_1.iso", "-V", $self-&gt;{name}, $dir);
        open(my $mkisofs, "-|", "mkisofs", "-J", "-r", "-o",
            "Disk_1.iso", "-V", $self-&gt;{name}, $dir) || die "mkisofs failed: $!";
        while (&lt;$mkisofs&gt;) {
            chomp;
            print "\r$_";
        }
        print "\n";
        close($mkisofs);
        return;
    }
    
    $self-&gt;{disk_id} = 1;
    $self-&gt;{cur_size} = 0;
    
    find({ wanted =&gt; sub { $self-&gt;wanted(@_) }, no_chdir =&gt; 1 }, $dir);
    
    # always an extra one to make at the end
    $self-&gt;mk_iso();
}

sub dbg {
    return unless $DEBUG;
    
    print @_, "\n";
}

sub wanted {
    my $self = shift;
    
    my $curr = $File::Find::name;
    
    my $relative = File::Spec-&gt;abs2rel( $curr, $self-&gt;{folder} );
    
    if (-d $curr) {
        return if $self-&gt;{skip_empty};
        # no current temp dir, so create one
        $self-&gt;{temp_dir} ||= tempdir("mk_dvd_XXXXXXXX", TMPDIR =&gt; 1);
    
        my $path = File::Spec-&gt;catdir( $self-&gt;{temp_dir}, $relative );
        dbg("Creating dir: $path");
        mkpath( $path );
        return;
    }
    
    my $rel_dir  = File::Spec-&gt;abs2rel( $File::Find::dir, $self-&gt;{folder} );
    
    if (-f $curr) {
        my $size = -s _;
        
        if ( ($self-&gt;{cur_size} + $size) &gt; $self-&gt;{max_size} ) {
            dbg("$self-&gt;{cur_size} + $size &gt; $self-&gt;{max_size}");
            $self-&gt;mk_iso();
        }
        
        # no current temp dir, so create one
        $self-&gt;{temp_dir} ||= tempdir("mk_dvd_XXXXXXXX", TMPDIR =&gt; 1);
        
        my $path = File::Spec-&gt;catdir( $self-&gt;{temp_dir}, $rel_dir );
        unless (-d $path) {
            dbg("Creating dir: $path");
            mkpath( $path );
        }
        
        dbg("Copying $curr -&gt; $path");
        
        system("cp", $curr, $path) &amp;&amp; die "cp failed!";
        
        $self-&gt;{cur_size} += $size;
    }
    else {
        warn("don't know how to handle $curr\n");
    }
    
    return;
}

sub mk_iso {
    my $self = shift;
    my $name = "$self-&gt;{name} Disk $self-&gt;{disk_id}";
    
    dbg("making ISO Disk_$self-&gt;{disk_id}.iso");
    open(my $mkisofs, "-|", "mkisofs", "-J", "-r", "-o",
        "Disk_$self-&gt;{disk_id}.iso", "-V", $name, $self-&gt;{temp_dir}) || die "mkisofs failed: $!";
    while (&lt;$mkisofs&gt;) {
        chomp;
        print "\r$_";
    }
    print "\n";
    close($mkisofs);
    
    $self-&gt;{disk_id}++;
    rmtree($self-&gt;{temp_dir});
    $self-&gt;{temp_dir} = undef;
    $self-&gt;{cur_size} = 0;
}
</pre></body></html>