#!/usr/bin/perl
#
# PCDBS - Perl CD Burning Script
# http://bratwurst.tty0.org/
#
# Written by Mikael Sörlin
#
# Dependencies:
# Cdrecord - http://freshmeat.net/projects/cdrecord/
#
# Config part:
# Make sure these two are correct!!
$burnspeed  	= '24';             # set your burnerspeed here, if you're unsure, leave untouched
$burndevice 	= '0,1,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
$rockridge 	= 'on';             # enable/disable Rock Ridge extension, if you're unsure, leave untouched
$joliet 	= 'on';             # enable/disable Joliet extension, if you're unsure, leave untouched
$eject		= 'on';             # ejects the tray when done
$time		= '4';              # seconds before starting to burn

# Don't edit below this point

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

&spam;

if ($burnspeed) {
        print "Burnspeed is set to ${burnspeed}x\n";
        $burnspeed = "-speed=$burnspeed";
} else {
        print "No \$burnspeed set.\n";
}

if ($burndevice) {
        print "Burndevice is set to $burndevice\n";
        $burndevice = "-dev=$burndevice";
} else {
        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[0] =~/\-h[elp]*/) {
	&help;
} elsif ($ARGV[0] =~/\-i[so]*/) {
	&burn;
} elsif ($ARGV[0] =~/\-d[ir]*/) {
	&burn2;
} elsif ($ARGV[0] =~/\-b[uildiso]*/) {
	&makeiso;
} elsif ($ARGV[0] =~/\-m[enu]*/) {
	&menu;
} else {
	&help;
}

sub help {
	print "\nPCDBS usage:\n-h, --help\t\tprint this help screen\n-m, --menu\t\tstart the menu interface\n-d, --dir [DIR]\t\tburn directory\n-i, --iso [ISO]\t\tburn iso image\n-b, --buildiso [DIR]\tbuild 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] =~/\-m[enu]*/) {
                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 $burnspeed $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 $burnspeed $burndevice $gracetime $eject -data $_'");
        }
}

sub burn2 {
        if ($ARGV[0] =~/\-m[enu]*/) {
                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 $burnspeed $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 $burnspeed $burndevice $gracetime $eject -'");
        }
}

sub makeiso {
        if ($ARGV[0] =~/\-m[enu]*/) {
                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 $burnspeed $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 $burnspeed $burndevice $gracetime $eject -data $isoname'");
		}
	}
}

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