#!/usr/bin/perl
##########################
## Burncenter 3
## Copyright(c) 2000-2005 Alexandre Janon <alex14fr@yahoo.fr>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
##########################
## Menu interface for Burncenter.pm, like old burncenter

# Joey Schulze <joey *at* infodrom *dot* org> contributed manpage and 
# modified copyalltracks to copy only selected tracks. 

=head1 NAME

burncenter - Text-menu frontend for CD burning and reading

=head1 SYNOPSIS

B<burncenter>

=head1 DESCRIPTION

B<burncenter> is a small program that provides a menu-driven frontend
to commonly used programs that read and write from or to CD and DVD
media.  In particular it provides a CUI (character user interface) to
B<cdrecord>, B<cdda2wav>, B<mkisofs> and others.

When your B<cdrecord> program is not installed setuid root but you
would like to burn your CDs as user or rather execute this program as
user and burn as root, you should take a look at which features
B<sudo> and B<super> provide and configure your writing command as
I<sudo cdrecord> instead.

=head1 NOTES

When reading an Audio CD you can specify which tracks the program
should read.  The default is to read all tracks.  If you want to limit
the number of tracks, you can specify single tracks and ranges,
delimited by a comma or space characters.  A range looks like I<2-4>.

=head1 FILES

I</etc/burncenter.conf> System-wide configuration file
I<~/.burncenter> User configuration file

=head1 SEE ALSO

B<cdda2wav>(1),
B<cdrecord>(1),
B<mkisofs>(8),
B<mpg123>(1),
B<ogg123>(1).

=cut


use Burncenter;

sub printmenu {
	while(true) {
		print "1. Create (or add sessions to) a Multi-session CD\n";
		print "2. Create a Single-session CD\n";
		print "3. Create an Audio CD from WAVes, MP3 or OGG files\n";
		print "4. Create an Audio CD from a Playlist\n";
		print "5. Quickly Erase a CDRW\n";
		print "6. Blank a CDRW\n";
		print "7. Copy a Data CD\n";
		print "8. Copy an Audio CD\n";
		print "9. Read an Audio CD\n";
		print "10. Burn a CD from an ISO image\n";
		print "Q. Quit burncenter\n";
		print "R. Reconfigure burncenter\n";
		print "\n";
		$choice = Burncenter::getline("Your choice?");

		if ($choice =~ /^[qx]$/i) { exit() }
		elsif ($choice =~ /^r$/i) { Burncenter::reconfigure() }
		elsif ($choice =~ /^1$/) { burnmultidata() }
		elsif ($choice =~ /^2$/) { burnsingledata() }
		elsif ($choice =~ /^3$/) { burnaudio() }
		elsif ($choice =~ /^4$/) { burnplaylist() }
		elsif ($choice =~ /^5$/) { quickerase() }
		elsif ($choice =~ /^6$/) { completeerase() }
		elsif ($choice =~ /^7$/) { copydata() }
		elsif ($choice =~ /^8$/) { copyaudio() }
		elsif ($choice =~ /^9$/) { readaudio() }
		elsif ($choice =~ /^10$/) { burniso() }
	}
}

sub burnsingledata {
	$dir=Burncenter::getlinebasedonconfig("What directory do you want to burn?","burndir","/tmp/ToBurn");
	Burncenter::getline("Insert destination disc in writer and press Enter");
	print "Creating Image...";
	$im=Burncenter::makefirstisoimage($dir);
	print "Writing Image...";
	Burncenter::burncdimage($im);
	print "Burn complete.\n";
	system("rm -f $im");
}

sub burnmultidata {
	$dir=Burncenter::getlinebasedonconfig("What directory do you want to burn?","burndir","/tmp/ToBurn");
	Burncenter::getline("Insert destination disc in writer and press Enter");
	print "Merging other sessions... ";
	$othersessions=Burncenter::checkothersessions();
	if($othersessions == 1) {
		print "Other sessions imported.\n";
		$img=Burncenter::makesecondisoimage($dir);
	} else {
		print "Creating the first session...\n";
		$img=Burncenter::makefirstisoimage($dir);
	}
	print "Writing Image...";
	Burncenter::burncdimage($img,"-multi");
	print "Burn complete.\n";
	system("rm -f $img");
}

sub burnaudio {
	my $dir=shift;
	if($dir eq '') {
		$dir=Burncenter::getlinebasedonconfig("Location of the directory where your Wawe, Ogg or MP3 files are?", "burndir", "/tmp/ToBurn");
	}
	Burncenter::getline("Insert destination disc in writer and press Enter");
	print "Decompressing MP3s and OGGs...";
	Burncenter::mp3towav($dir);
	print "Burning Image...";
	Burncenter::burnwavfiles($dir);
	print "Burn complete.\n";
}

sub burnplaylist {
	$playlist=Burncenter::getline("Location of your playlist?");
	$dir=Burncenter::playlist2dir($playlist);
	burnaudio($dir);
	system("rm -fr $dir");
}

sub copydata {
	Burncenter::getline("Insert SOURCE disc in reader and press Enter");
	print "Creating Image...\n";
	$image=Burncenter::makecdimage();
	Burncenter::getline("Insert DESTINATION disc in writer and press Enter");
	print "Burning Image...\n";
	Burncenter::burncdimage($image);
	print "Copy complete.\n";
	system("rm -f $image");
}

sub copyaudio {
	Burncenter::getline("Insert SOURCE disc in WRITER and press Enter");
	print "Read Audio CD...\n";
	$audiodir = Burncenter::copyalltracks();
	Burncenter::getline("Insert DESTINATION disc in writer and press Enter");
	print "Writing disc...\n";
	Burncenter::burnwavfiles($audiodir);
	print "Copy complete.\n";
	system("rm -fr $audiodir");
}

sub readaudio {
	my $dir=shift;
	if($dir eq '') {
		$dir=Burncenter::getlinebasedonconfig("Directory to copy the WAVes files to?", "burndir", "/tmp/ToBurn");
	}
	$parts=Burncenter::getline("Which tracks to read [default=all]?");
	Burncenter::getline("Insert source disc in READER and press Enter");
	print "Reading Audio CD...\n";
	$audiodir = Burncenter::copyalltracks($parts);
	if($dir ne '') {
		mkdir ($dir, 0755) if (! -d $dir);
		system("mv $audiodir/*.wav $dir");
	}
	print "Copy complete\n";
	system("rm -fr $audiodir");
}

sub quickerase {
	Burncenter::getline("Insert CDRW in your drive and press Enter");
	print "Blanking disc...\n";
	Burncenter::blankcdrw("fast");
}

sub completeerase {
	Burncenter::getline("Insert CDRW in your drive and press Enter");
	print "Blanking disc...\n";
	Burncenter::blankcdrw("all");
}

sub burniso() {
	my $image = Burncenter::getline("Location of the ISO image to burn ?");
	Burncenter::getline("Insert blank CD-R and press Enter");
	print "Writing disc...\n";
	Burncenter::burncdimage($image);
}

Burncenter::reconfigure() if (! -e $Burncenter::conf_global && ! -e $Burncenter::conf_local);
;
printmenu;
