#!/usr/bin/perl

# by Samy Kamkar [commport5@lucidx.com]

# requires burncd for burning isos
# requires mkisofs if you want to burn regular files/dirs onto a cd

use Tk;
$mdb = MainWindow->new();
$mdb->title(" CD-R(W) Burner");
$mhead = $mdb->Frame(-relief => 'ridge', -borderwidth => 2)->pack(-fill => 'x', -anchor => 'nw', -side => 'top');
$mright = $mdb->Frame(-relief => 'ridge', -borderwidth => 2)->pack(-fill => 'x', -anchor => 'ne', -side => 'right');
$mright->Label(-text => 'Media Type')->pack(-fill => 'x', -anchor => 'nw', -side => 'top');
@typerad = ('Audio [XA]', 'Data', 'Data/Audio', 'No Idea');
for my $type (0 .. 3) {
 $mright->Radiobutton(-text => "$typerad[$type]", -variable => \$dbtype, anchor => 'w', -relief => 'flat', -value => $type)->pack(-side => 'top');
}
$mright->Label(-text => "\n\nDevice")->pack;
$device = $mright->Entry(-borderwidth => 2, -width => 8)->pack;
$device->insert('end', "racd1a");
$mright->Label(-text => "\nSpeed")->pack;
$speed = $mright->Entry(-borderwidth => 2, -width => 8)->pack;
#$speed->insert('end', "2");
$mbot = $mdb->Frame( -relief=>'ridge', -borderwidth => 2)->pack(-fill => 'x', -anchor => 'nw', -side => 'bottom');
$mbot->Checkbutton(-text => "Eject when done", -variable => \$cstart, anchor => 'e', -relief => 'flat', -command => sub { &eject })->pack(-side => 'top');
$mbot->Checkbutton(-text => "Preemphasis on audio tracks", -variable => \$asknew, anchor => 'e', -relief => 'flat', -command => sub { &emph })->pack(-side => 'top');
$mbot->Checkbutton(-text => "Test burn", -variable => \$mkrev, anchor => 'e', -relief => 'flat', -command => sub { &test })->pack(-side => 'top');
$mbot->Checkbutton(-text => "Quiet at console", -variable => \$mknew, anchor => 'e', -relief => 'flat', -command => sub { &quiet })->pack(-side => 'top');
for $i (1 .. 5) {
 ($f, $l, $e) = ("mhead_${i}", "mhead_${i}_l", "mhead_${i}_e");
 ${$f} = $mdb->Frame();
 ${$f}->pack(-side => 'top', -pady => '2', -anchor => 'e');
 ${$l} = ${$f}->Label(-text => "ISO $i", -anchor => 'e');
 ${$e} = ${$f}->Entry(-width => '20', -relief => 'sunken');
 ${$e}->insert('0', $dbfill[$i]);
 ${$e}->pack(-side => 'right');
 ${$l}->pack(-side => 'right');
}
for $i (6 .. 10) {
 $r = ($i - 5);
 ($f, $l, $e) = ("mhead_${i}", "mhead_${i}_l", "mhead_${i}_e");
 ${$f} = $mdb->Frame();
 ${$f}->pack(-side => 'top', -pady => '2', -anchor => 'e');
 ${$l} = ${$f}->Label(-text => "File/Dir $r", -anchor => 'e');
 ${$e} = ${$f}->Entry(-width => '20', -relief => 'sunken');
 ${$e}->insert('0', $dbfill[$i]);
 ${$e}->pack(-side => 'right');
 ${$l}->pack(-side => 'right');
}
$btnok = $mhead->Button(-text => 'Burn');
$btnok->configure(-command => \&write);
$btnok->pack(-side => 'left', -padx => '2');
$btndisc = $mhead->Button(-text => 'Blanken');
$btndisc->pack(-side => 'left', -padx => '2');
$btndisc->configure(-command => \&blank);
$btnconnect = $mhead->Button(-text => 'Eject');
$btnconnect->pack(-side => 'left', -padx => '2');
$btnconnect->configure(-command => \&command);
$btnsave = $mhead->Button(-text => 'Information');
$btnsave->configure(-command => \&info);
$btnsave->pack(-side => 'left', -padx => '2');
$btncancel = $mhead->Button(-text => 'Exit', -command => [$mdb,'destroy']);
$btncancel->pack(-side => 'left', -padx => '2');
MainLoop;
sub command {
 $dev = get $device;
 print "Executing: burncd -f /dev/$dev -e\n";
 system("burncd -f /dev/$dev -e");
}
sub blank {
 my $isos;
 my $files;
 $args = "burncd";
 $spd = get $speed;
 $dev = get $device;
 $args .= " -f /dev/$dev";
 if ($spd) {
  $args .= " -s $spd";
 }
 if ($eject %2 != 0) {
  $args .= " -e";
 }
 if ($quiet %2 != 0) {
  $args .= " -q";
 }
 if ($test %2 != 0) {
  $args .= " -t";
 }
 $args .= " blank";
 print "Executing: $args\n";
 system($args);
 print "Finished.\n\n";
}
sub write {
 $args = "burncd";
 for (1 .. 5) {
  $mhead = "mhead_${_}_e";
  $file = get ${$mhead};
  if ($file) {
   $files .= " $file";
  }
 }
 for (6 .. 10) {
  $mhead = "mhead_${_}_e";
  $file = get ${$mhead};
  if ($file) {
   $isos .= "$file ";
  }
 }
 $rand = rand(1000000);
 print "Executing: mkisofs $isos> /tmp/.burn.$rand";
 system("mkisofs $isos> /tmp/.burn.$rand");
 $files .= " /tmp/.burn.$rand";
 $spd = get $speed;
 $dev = get $device;
 $args .= " -f /dev/$dev";
 if ($spd) {
  $args .= " -s $spd";
 }
 if ($eject %2 != 0) {
  $args .= " -e";
 }
 if ($test %2 != 0) {
  $args .= " -t";
 }
 if ($quiet %2 != 0) {
  $args .= " -q";
 }
 if ($emph %2 != 0) {
  $args .= " -p";
 }
 if ($dbtype) {
  $args .= " data";
 }
 else {
  $args .= " audio";
 }
 $args .= $files;
 $args .= " fixate";
 print "Executing: $args\n";
 system($args);
 print "Finished.\n\n";
}
sub info {
 my $top2 = $mdb->Toplevel;
 $top2->Label(-text => "\n      Perl/Tk CD Burner [burncd front-end]      \n      by Samy Kamkar\n")->pack;
}
sub quiet {
 $quiet++;
}
sub eject {
 $eject++;
}
sub emph {
 $emph++;
}
sub test {
 $test++;
}
