#!/usr/bin/perl -w
#
# DEVELOPMENT
#
# PCDBS - Perl CD Burning Script
# http://bratwurst.tty0.org/
#
# Written by Mikael Sörlin and also alot contributed by Marcus Berglöf
#
# Dependencies:
# cdrtools - http://freshmeat.net/projects/cdrecord/
# cdrdao - http://cdrdao.sourceforge.net/
#
# Config part:
# Make sure these two are correct!!
my $burnspeed  = '0';       # set your burnerspeed here, if you're unsure, leave untouched
my $burndevice = '0,0,0';   # set your burnerdevice here, if you're unsure, do a 'cdrecord -scanbus' to see your devices
# You probably don't need to change these
my $rockridge  = 'on';      # enable/disable Rock Ridge extension, if you're unsure, leave untouched
my $joliet     = 'on';      # enable/disable Joliet extension, if you're unsure, leave untouched
my $eject      = 'on';      # ejects the tray when done
my $time       = '4';       # seconds before starting to burn

# Don't edit below this point

use Switch;

my $version = "0.3pre1";
my $author   = "Mikael Sörlin";
my $website = "http://bratwurst.tty0.org";

if (!$burnspeed) {
  print "!! No \$burnspeed set.\n";
}

if (!$burndevice) {
  print "!! No \$burndevice set.\n";
}

if ($time) {
  $gracetime = "gracetime=$time";
} else {
  $gracetime = '';
}

if ($rockridge eq 'on') {
  $rockridge = '-r';
} elsif ($rockridge eq 'off') {
  $rockridge = '';
} else {
  print "Bad value set at Rock Ridge option. Aiiieee! I'm dying!\n";
  exit;
}

if ($joliet eq 'on') {
  $joliet = '-J';
} elsif ($joliet eq 'off') {
  $joliet = '';
} else {
  print "Bad value set at Joliet option. Aiiieee! I'm dying!\n";
  exit;
}

if ($eject eq 'on') {
  $eject = '-eject';
} elsif ($eject eq 'off') {
  $eject = '';
} else {
  print "Bad value set at Eject option. Aiiieee! I'm dying!\n";
  exit;
}

if (@ARGV < 1) {
  &help;
  exit;
}
switch ($ARGV[0]) {
  case '--iso'    { &burn }
  case '--dir'    { &burn2 }
  case '--mkiso'  { &makeiso }
  case '--toc'    { &toc }
  case '--menu'   { &menu }
  case '--help'   { &help }
  else            { print $ARGV[0] . ": unkonwn option\n"; &help }
}

## subroutines
sub help {
  print "PCDBS usage:
  --help           print this help screen
  --menu           start the menu interface
  --dir [DIR]      burn directory
  --iso [ISO]      burn iso image
  --toc [FILE]     burn toc/cue file
  --mkiso [DIR]    make iso image from file/directory\n\n";
  &spam;
}

sub menu {
  print "\n\tMenu\n\t(1) Burn ISO\n\t(2) Burn dir/file\n\t(3) Create ISO\n\t-> ";
  $option = <STDIN>;
  chomp($option);

  if ($option eq 1) {
    &burn;
  } elsif ($option eq 2) {
    &burn2;
  } elsif ($option eq 3) {
    &makeiso;
  } else {
    print "\n\tOuch.. Wrong button..\n";
    &menu;
  }
}

sub burn {
  if ($ARGV[0] eq '--menu') {
    print "\nEnter the filname of the iso image you wish to burn: ";
    $_ = <STDIN>;
    chomp($_);
  } else {
    $_ = $ARGV[1];
  }
  if ($< == 0) {
    print "$_ will now be burned\n";
    system("cdrecord -v -speed=$burnspeed -dev=$burndevice $gracetime $eject -data $_");
  } else {
    print "You are not logged in as root.\nPlease enter your root password to burn $_\n";
    system("su -c 'cdrecord -v -speed=$burnspeed -dev=$burndevice $gracetime $eject -data $_'");
  }
}

sub burn2 {
  if ($ARGV[0] eq '--menu') {
    print "\nEnter the file/dir of the iso image you wish to burn: ";
    $_ = <STDIN>;
    chomp($_);
  } else {
    $_ = $ARGV[1];
  } if ($< == 0) {
    print "$_ will now be burned\n";
    system("mkisofs -r $rockridge $joliet $_ | cdrecord -v -speed=$burnspeed -dev=$burndevice $gracetime $eject -");
  } else {
    print "You are not logged in as root.\nPlease enter your root password to burn $_\n";
    system("su -c 'mkisofs -r $rockridge $joliet $_ | cdrecord -v -speed=$burnspeed -dev=$burndevice $gracetime $eject -'");
  }
}

sub makeiso {
  if ($ARGV[0] eq '--menu') {
    print "\nEnter the file/dir to make a iso of: ";
    $_ = <STDIN>;
    chomp($_);
  } else {
    $_ = $ARGV[1];
  }
  $isoname = "${_}.iso";
  system("mkisofs $rockridge $joliet -o $isoname $_");
  print 'Do you want to burn ' . $isoname . ' now? (y/n) [y] ';
  $yesno = <STDIN>;
  chomp($yesno);
  if ($yesno =~/y[es]*/) {
    &isoburning;
  } elsif ($yesno =~/n[o]*/) {
    &spam;
  } else {
    &isoburning;
  }
}

sub isoburning {
  if ($< == 0) {
    print "$isoname will now be burned\n";
    system("cdrecord -v -speed=$burnspeed -speed=$burndevice $gracetime $eject -data $isoname");
  } else {
    print "You are not logged in as root.\nPlease enter your root password to burn $_\n";
    system("su -c 'cdrecord -v -speed=$burnspeed -speed=$burndevice $gracetime $eject -data $isoname'");
  }
}

sub toc {
  if ($ARGV[0] eq '--menu') {
    print "\nEnter the toc/cue filename you wish to burn: ";
    $_ = <STDIN>;
    chomp($_);
  } else {
    $_ = $ARGV[1];
  } if ($< == 0) {
    print "$_ will now be burned\n";
    system("cdrdao write --speed $burnspeed --device $burndevice -$eject");
  } else {
    print "You are not logged in as root.\nPlease enter your root password to burn $_\n";
    system("su -c 'cdrdao write --speed $burnspeed --device $burndevice -$eject'");
  }
}

sub spam { # heheh :-)
  print "PCDBS $version\n(C) 2003 $author - $website\n\n";
}