#!/usr/bin/perl
#
# tear version 0.4.5 # May Not Be Up to Date ;)
#
# This software created under the GPL (see LICENSE) by:
# Gregory Reynolds (DevDuck@users.sourceforge.net)
#
# This will use cdparanoia, cdda2wav, lame, bladeenc, gogo, Xing,
# CDDB_get.pl, and id3tool to rip and encode an entire CD into MP3 format

# cdparanoia can be found at 
#  http://www.xiph.org/paranoia/
# cdda2wav can be found at
#  ftp://ftp.gwdg.de/pub/linux/misc/cdda2wav/ 
# lame can be found at
#   http://www.sulaco.org/mp3/
# bladeenc can be found at
#   http://bladeenc.mp3.no/
# gogo can be found at
#   http://homepage1.nifty.com/herumi/gogo_e.html
# xing can be bought at
#   http://www.xingtech.com/mp3/
# CDDB_get.pl can be found at
#   http://armin.emx.at/cddb/
# id3tool can be found at
#   http://kitsumi.xware.cx/id3tool/

# It is assumed that all programs are properly installed.

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if($hour >= 13) { $hour = $hour - 12; }
@time = ($hour,$min,$sec);

use CDDB_get;

&defaults;

if($use_conf != 0) { &conffile; }

unless ($#ARGV == -1) { &arguments; }

# Quiet Pipes
$extra = ""; 
if($noise == 0) { $extra = "\ 2>\ \/dev\/null"; }

# Extra Settings
if($indi != 0) { $batch = 0; }
if($step == 1) { $enc = 0; $batch = 1; $wav = 1; $cddb_use = 4; }
if($step == 2) { $rip = 0; $batch = 0; $cddb_use = -1; }

if ($debug == 1) {
$dbfile = "$ENV{HOME}\/.tear\-debug";
open (DEBUG,">$dbfile");
print DEBUG "$vers\n";
print DEBUG "$time[0],$time[1],$time[2]\n";
}

if ($noise == 2) {
    print "Settings:\n\$noise = $noise\n\$bit = $bit\n\$enc = $enc\n";
    print "\$wav = $wav\n\$rip = $rip\n\$batch = $batch\n\$indi = $indi\n";
    print "\$cddb_use = $cddb_use\n\$error = $error\n\$debug = $debug\n";
    print "\$dbfile = $dbfile\n\$ripnice = $ripnice\n\$encnice = $encnice\n";
}

if ($debug == 1) {
    print DEBUG "Settings:\n\$noise = $noise\n\$bit = $bit\n\$enc = $enc\n";
    print DEBUG "\$wav = $wav\n\$rip = $rip\n\$batch = $batch\n\$indi = $indi\n";
    print DEBUG "\$cddb_use = $cddb_use\n\$error = $error\n";
    print DEBUG "\$config{\"CD_DEVICE\"} = $config{\"CD_DEVICE\"}\n";
    print DEBUG "\$ripnice = $ripnice\n\$encnice = $encnice\n\$step = $step\n";
}

# Get Disc Information from CDDB
if($cddb_use == 3) { &chkCDDB; exit; }
elsif($cddb_use == 4) { &chkCDDB; }
elsif($cddb_use >= 1) { &CDDB; }

if($step == 2) { $lock = 1; &lockCDDB; }
elsif($cddb_use <= 1) { &myCDDB; }

if($noise == 2) { print "\$artst = $artst\n\$title = $title\n\$cat = $cat\n"; }
if($debug == 1) { print DEBUG "\$artst = $artst\n\$title = $title\n\$cat = $cat\n"; }

&setNames;

&dirs;

# Batch Rip?
if($batch == 1 && $rip == 1) { &paranoia; }
if($batch == 1 && $rip == 2) { &cdda2wav; }
if($batch == 1 && $rip != 0) { &run; }

if($batch == 1 && $noise > 0) { print "CD ripped\n"; }

# Start Playlist
if($playlist) {
    $playlist = break_apart($playlist);
    open(PLAYLIST,">>$playlist");
}

# Encode
if ($enc > 0) { &encode; };

if($step == 1) { for($i = 0; $i < $trknm; $i++) { &wav; } }
if($lock) { $lock++; while($lock) { &lockCDDB; if($lock) { $lock++; &setNames; &dirs; &encode; } } }

&quit;

#### Sub-Routines

#### #### ####
sub defaults {
# Set Defaults (May Get Changed by Arguments)
    $vers="0.4.5"; # Version Number

# "Regular" Options
    $noise = 0;    # Verbosity     -> 0 = Extreme Quiet (Default)
                                   #  1 = Full Noise
                                   #  2 = Debug
                                   #  3 = Debug > ~/.tear-debug
    $debug = 0;    # Debug Mode    -> 0 = Regular (Default)
                                   #  1 = To file
    $use_conf = 1; # Conf Files    -> 0 = Don't Use - Override Conf-Files
                                   #  1 = Use (Default)
    $rip = 1;      # .WAV Ripper   -> 0 = Skip Ripping
                                   #  1 = cdparanoia (Default)
                                   #  2 = cdda2wav
    $wav = 0;      # Save WAVs     -> 0 = No (Default)
                                   #  1 = Yes
    $cddb_use = 2; # Use CDDB      -> 0 = No
                                   #  1 = Check
                                   #  2 = Yes (Default)
                                   #  3 = Special
    $error = 1;    # Add CHKSUM    -> 0 = No
                                   #  1 = Yes (Default)
    $enc = 1;      # MP3 Encoder   -> 0 = Skip Encoding
                                   #  1 = lame (Default)
                                   #  2 = bladeenc
                                   #  3 = gogo
                                   #  4 = xing (STILL Untested)
                                   #  5 = mp3enc (STILL Untested)
    $batch = 0;    # Batch Ripping -> 0 = No (Default)
                                   #  1 = Yes
                                   #  2 = Skip Ripping (DEBUG)
    $id3_vers = 1; # ID3 v?        -> 0 = No ID3 tags
                                   #  1 = ID3v1 (Default)
                                   #  2 = ID3v2 (Not Yet)
    $ripnice = 0;  # CD Nice Level ->-20= Max Priority
                                   # Other Values See Above
    $encnice = 0;  # Encoder Nice  -> 0 = Regular (Default)
                                   # Other Values See Above
    $indi = 0;     # tear track    -> 0 = All (Default)
                                   #  % = Only Track No. %
    $step = 0;	 # Multi-Step	   -> 0 = All the Steps (Default)
                                   #  1 = Batch Rip and Save
                                   #  2 = Encode Save WAV files
    $bit = 256;    # Bitrate (32 40 48 56 64 80 96 112 128 160 192 224 <256> 320)

# File Name Templates
    $munging = "";
    $mp3name = "%a - %s.mp3";
    $wavname = "%a - %s.wav";
    $playlist = "";

# ID3 stuff:
    $id3 = "";
    $artst = "";
    $title = "";
    $cat = "";
    $comment = "";

# CDDB stuff:
    $config{CDDB_HOST} = "freedb.freedb.org"; # set cddb host
#    $config{CDDB_PORT} = 888;                 # set cddb port
    $config{CD_DEVICE} = "/dev/cdrom";        # set cd device

# DEBUG stuff
    $debug = 0;
}
####

#### #### ####
sub conffile {
# Which File - Local, Home or Global?
    $cffile = ".\/.tearrc";
    $homefile = "$ENV{HOME}\/.tearrc";
    $etcfile = "\/etc\/tear.conf";

# Does the File Exist?
    if(-e "$cffile") {
	open(CONF,"$cffile");
	$use_conf = 1;
    }
    elsif(-e $homefile) {
	open(CONF,"$homefile");
	$use_conf = 1;
    }
    elsif(-e $etcfile) {
	open(CONF,"$etcfile");
	$use_conf = 1;
    }
    else { $use_conf = 0; }

# Read in the File
  LINE: while ($line = <CONF>) {

# Skip Comment Lines
      next LINE if $line =~ /^#/;

# Remove \n's
      chomp($line);

# Remove any spaces around :
      $line =~ s/(\ :\ )/:/g;

# Break on :'s
      ($hash, $value) = split /:/, $line, 2;
      unless($hash eq "ID3COM"
	     || $hash eq "MP3DIR" || $hash eq "WAVDIR"
	     || $hash eq "MP3NAME" || $hash eq "WAVNAME") {
	  $value =~ s/(\w+)/\u\L$1/g; }

# Set Pieces into Hash
      $conf{$hash} = $value;
  }
    close(CONF);

# General Information
    if($conf{"VERBOSE"} eq "Yes" || $conf{"VERBOSE"} == 1) { $noise = 1; }

# Filename Templates
    if($conf{"MUNGING"} && $conf{MUNGING} != "No") { $munging = 1; }
    if($conf{"MP3NAME"}) { $mp3name = $conf{"MP3NAME"}; }
    if($conf{"WAVNAME"}) { $wavname = $conf{"WAVNAME"}; }
    if($conf{"PLAYLIST"} && $conf{PLAYLIST} != "No") { $playlist = $conf{"PLAYLIST"}; }

# CD Ripping Information
    if($conf{"CDRIP"} eq "Cdda2wav") { $rip = 2; }
    if($conf{"CDBATCH"} eq "Yes" || $conf{"CDBATCH"} == 1) { $batch = 1; }
    if($conf{"SAVEWAV"} eq "Yes" || $conf{"SAVEWAV"} == 1) { $wav = 1; }
    if($conf{"CDDEV"}) { $config{"CD_DEVICE"} => $conf{"CDDEV"}; }

# MP3 Information
    if($conf{"MP3ENC"} eq "Bladeenc") { $enc = 2; }
    if($conf{"MP3ENC"} eq "Gogo") { $enc = 3; }
    if($conf{"MP3ENC"} eq "Xing") { $enc = 4; }
    if($conf{"MP3ENC"} eq "Mp3enc") { $enc = 5; }
    if($conf{"MP3BIT"}) { $bit = $conf{"MP3BIT"}; }

#Nice Settings
    if($conf{"NICE"}) { $ripnice = $conf{"RIPNICE"}; $encnice = $conf{"ENCNICE"}; }
    if($conf{"RIPNICE"}) { $ripnice = $conf{"RIPNICE"}; }
    if($conf{"ENCNICE"}) { $encnice = $conf{"ENCNICE"}; }

# CDDB Information
    if($conf{"CDDBUSE"} eq "No") { $cddb_use = 0; }
    if($conf{"CDDBUSE"} eq "Check") { $cddb_use = 1; }

# ID3 Information
    if($conf{"ID3COM"}) {
	$comment = $conf{"ID3COM"};
	$comment = "\"$comment\"";
    }

# Directory Settings
    if($conf{"MP3DIR"}) { $dir{"MP3"} = $conf{"MP3DIR"} };
    if($conf{"WAVDIR"}) { $dir{"WAV"} = $conf{"WAVDIR"} };

# Debug Settings
    if($conf{"DEBUG"}) {
	if($conf{"DEBUG"} == 1) { $noise = 2; }
	elsif($conf{"DEBUG"} == 2) { $debug = 1; }
    }
}
####

#### #### ####
sub arguments {
# Get Arguments

    use Getopt::Long;
  Getopt::Long::Configure ("bundling", "noignorecase");
    GetOptions (
# Super Settings
		'help|h|?' => \&help,
		'version|v' => sub { print "tear version $vers\n"; exit; },
		'debug|d' => sub { $noise = 2; },
		'D' => \$debug,
		'conf!' => \$use_conf,
		'nice=i' => \$nice,
		'noise|verbose|n' => \$noise,
		'step|s=i' => \$step,
		'track|single|t=i' => \$indi,
		'munge' => \$munging,

# CD Settings
		'b' => \$batch,
		'batch!' => \$batch,
		'cdrom=s' => \$config{CD_DEVICE},
		'ripnice=i' => \$ripnice,
		'ripper=i' => \$rip,
		'w' => \$wav,
		'wav!' => \$wav,
		'wavdir=s' => \$conf{WAVDIR},
		'wavname=s' => \$wavname,

# CDDB Settings
		'cddb!' => \$cddb_use,
		'readCDDB' => sub { $cddb_use = 3; },
		'cddb-host|host=s' => \$config{CDDB_HOST},
		'cddb-port|port=s' => \$config{CDDB_PORT},

# MP3 Settings
		'bitrate|r=i' => \$bit,
		'encnice=i' => \$encnice,
		'encoder|e=i' => \$enc,  
		'error!' => \$error,
		'mp3dir=s' => \$conf{MP3DIR},
		'mp3name=s' => \$mp3name,
		'playlist|p=s' => \$playlist,

# ID3 Settings
		'comment=s' => \$comment,
		'id3!' => \$id3_vers);

    if($nice) { $ripnice = $nice; $encnice = $nice; }
}
####

#### #### ####
sub help {
print <<HELP;
tear Version $vers Help

 Option     | Short    | Use
------------+----------+------------
 --verbose  | -n       | Noisy Mode
 --noconf   |          | Skip Configuration Files / Use Defaults
 --batch    | -b       | Batch Rip from CD
 --track    | -t <##>  | Only tear Track <##>
 --playlist |          | Create Playlist of name \"..\"
 --cddb     |          | Double-check CDDB results
 --nocddb   |          | Enter CDDB information manually
 --version  | -v       | Print Version Number
 --help     | -h       | Print This Help Message
------------+----------+------------

Also see use the MAN file: `man tear' for more useful settings.
Please report bugs to\n Gregory Reynolds \<DevDuck\@users.sourceforge.net\>
HELP

    &quit;
}
####

#### #### ####
sub run {
    if($noise == 2) { print "$command\n"; }
    if($debug == 1) { print DEBUG "$command\n"; }

    system("$command");

    $command = "";
}
####

#### #### ####
sub break_apart {
# Capture Passed Reference
    $break = @_[0];

# Break Apart the Captured Reference and substitute.
    $break =~ s/(%t)/$title/;		# Set Disc Title
    $break =~ s/(%a)/$artst/;		# Set Artist
    $break =~ s/(%s)/$track[$i]/;	# Set Track Name
    $break =~ s/(%g)/$cat/;		# Set Genre/Category
    $break =~ s/(%n)/$a/;			# Set Track Number

# Return New Value
    return $break;
}
####

#### #### ####
sub CDDB {
# Real CDDB access

# Retrieve CDDB information
    %cd = get_cddb(\%config);

# Covers CDDB errors of almost all sorts
    unless(defined $cd{title}) { die "No CDDB Entry Found"; }

    print_cd(\%cd);

    $cat =~ s/(\w+)/\u\L$1/g;

    if($noise == 2) {
	print "\$artst = $artst\n\$title = $title\n\$cat = $cat\n";
    }
    if($debug == 1) {
	print DEBUG "\$artst = $artst\n\$title = $title\n\$cat = $cat\n";
    }

# Bugfix :-)
    $id3cat = "id3tool -l | grep \"$cat\" > /dev/null";
    if(system("$id3cat")) { $cat = "Other"; }
}
####

#### #### ####
sub chkCDDB {
    $noise = 0;

    &CDDB;

    $i = 0;
    if($step) {
      GRAB: while ($i >= 0) {
	  $i++;
	  $chk = "/tmp/tear$i";
	  next GRAB if(-e $chk);
	  $i = -1; }
    }
    else { $chk = "/tmp/tear-cddb"; }

    open(CHK,">$chk");

    print CHK "discid : $dscid\ncdtitle : $title\nartist : $artst\ngenre : $cat\ntracks : $trknm\n";
    for($i=0; $i <= $#track; $i++) {
	print CHK "track ";
	print CHK $i + 1;
	print CHK " : $track[$i]\n";
    }

    close(CHK);
}
####

#### #### ####
sub lockCDDB {
    $artst = "";
    $title = "";

    $file = "/tmp/tear$lock";
    if(-e $file) {
	open(GET,"$file");
	while($line = <GET>) {
	    chomp($line);
	    ($hash, $value) = split /\ :\ /, $line, 2;
	    $cddb{$hash} = $value;
	}
	close(GET);

	$dscid = $cddb{"discid"};
	$trknm = $cddb{"tracks"};
	$title = $cddb{"cdtitle"};
	$artst = $cddb{"artist"};
	$cat = $cddb{"genre"};

	for($i=0; $i < $trknm; $i++) {
	    $a = $i+1;
	    $track[$i] = $cddb{"track $a"};
	}

	$command = "rm \"$file\"";
	&run;
    }
    else { $lock = 0; }
}
####

#### #### ####
sub myCDDB {
# For Edited CDDB information
    while ($yes == 0) {
	&prt_CDDB;

	print "Is this Correct? (y N) ";
	$tmp = <STDIN>;
	chomp($tmp);
	if($tmp eq "y" || $tmp eq "Y") { $yes = 1; }
	print "\n";

	if($yes == 0) {
	    if($cddb_use == 1) { print "Hit \<ENTER\> to keep current entries\n"; }
	    if($cddb_use == 0) { print "Enter Information about the CD\n"; }

	    print "Artist: ";
	    $tmp = <STDIN>;
	    chomp($tmp);
	    unless($tmp eq "") { $artst = "$tmp"; }

	    print "Disc: ";
	    $tmp = <STDIN>;
	    chomp($tmp);
	    unless($tmp eq "") { $title = "$tmp"; }

	    print "Tracks: ";
	    $tmp = <STDIN>;
	    chomp($tmp);
	    unless($tmp eq "") { $trknm = "$tmp"; }

	    $gen = 0;
	    while ($gen == 0) {
		print "Genre: ";
		$tmp = <STDIN>;
		chomp($tmp);
		$tmp =~ s/(\w+)/\u\L$1/g;
		if($tmp eq "") { $gen = 1; }
		else {
		    $command = "id3tool -l | grep \"$tmp\" > /dev/null";
		    if(system("$command")) { print "Genre \[ $tmp \] is not available\n"; }
		    else { $gen = 1; $cat = "$tmp"; }
		}
	    }
	}
	print "\n";
    }

    while ($yes == 1) {
	for ($n = 0; $n < $trknm; $n++) {
	    $a = $n + 1;
	    if($a < 10) { $a = "0$a"; }
	    print "Track \#$a: \[ $track[$n] \]\n";
	}

	print "Is this Correct? (y N) ";
	$tmp = <STDIN>;
	chomp($tmp);
	if($tmp eq "y" || $tmp eq "Y") { $yes = 0; }

	if($yes == 1) {
	    for ($n = 0; $n < $trknm; $n++) {
		$a = $n + 1;
		if($a < 10) { $a = "0$a"; }
		if($cddb_use == 1) { print "Track \#$a: \[ $track[$n] \]\n"; }
		print "Track \#$a: ";
		$tmp = <STDIN>;
		chomp($tmp);
		unless($tmp eq "") { $track[$n] = "$tmp"; }
	    }
	}
	print "\n";
    }
}
####

#### #### ####
sub prt_CDDB {
  print "Current Information:\n";
  print "Artist: \[ $artst \]\n";
  print "Disc:   \[ $title \]\n";
  print "Tracks: \[ $trknm \]\n";
  print "Genre:  \[ $cat \]\n";
}
####

#### #### ####
sub print_cd {
    my $cd=shift;
  
# Artist
    $artst = $cd->{artist};
# CD Title
    $title = $cd->{title};
# Category
    $cat = $cd->{cat};
# cd-discid   
    $dscid = $cd->{id};
# No. of tracks
    $trknm = $cd->{tno};

# Dump Track Names into Array
    $n=1;
    foreach my $i ( @{$cd->{track}} ) {
	$track[$n - 1] = $i;
	$n++;     
    }
}
####

#### #### ####
sub setNames {
    $artst =~ s/\"/\'/g;
    $artst =~ s/\`/\'/g;
    $id3_artst = "\"$artst\"";
    $title =~ s/\"/\'/g;
    $title =~ s/\`/\'/g;
    $id3_title = "\"$title\"";

    for ($i = 0; $i <= $#track; $i++) { 
	$a = $i + 1;
	if($a < 10) { $a = "0$a"; }

	$track[$i] =~ s/\//\\/g;
	$track[$i] =~ s/\"/\'/g;
	$track[$i] =~ s/\`/\'/g;

	$id3_track[$i] = "\"$track[$i]\"";

	$track_name[$i] = break_apart($mp3name);
	if($munging) { $track_name[$i] =~ s/\ /_/g; }

	$track_name_wav[$i] = break_apart($wavname);
	if($munging) { $track_name_wav[$i] =~ s/\ /_/g; }
    }
}
####

#### #### ####
sub dirs {
    unless($conf{MP3DIR}) { $dir{MP3} = "%a\/%t"; }
    unless($conf{WAVDIR}) { $dir{WAV} = "wav"; }

    $mp3dir = break_apart($dir{MP3});
    $wavdir = break_apart($dir{WAV});

    @mp3dir = split /\//, $mp3dir;
    @wavdir = split /\//, $wavdir;

    $mp3dir = "";
    $wavdir = "";

    foreach $i (@mp3dir) {
	$mp3dir = "$mp3dir$i\/";
	unless(-e "$mp3dir") {
	    system("mkdir \"$mp3dir\"");
	}
    }

    if($wav == 1) { # Make sure we want WAV files
	foreach $i (@wavdir) {
	    $wavdir = "$wavdir$i\/";
	    unless(-e "$wavdir") {
		system("mkdir \"$wavdir\"");
	    }
	}
    }

    if($noise == 2) {
	print "\$mp3dir = $mp3dir\n";
	if($wav != 0) { print "\$wavdir = $wavdir\n"; }
    }
    if($debug == 1) {
	print DEBUG "\$mp3dir = $mp3dir\n";
	if($wav != 0) { print DEBUG "\$wavdir = $wavdir\n"; }
    }
}
####

#### #### ####
sub paranoia {
    $args = "-";
    if ($noise == 0) { $args = "$args\q"; }
    elsif ($batch == 0) { $args = ""; }
    if ($batch == 1) { $args = "$args\B"; }
    if ($batch == 0) { $args = "$args\ $a $input"; }

    $args = "$args -d  $config{CD_DEVICE}";

    $command = "nice -n $ripnice cdparanoia $args $extra";
}
####

#### #### ####
sub cdda2wav {
    $args = "-H";
    if($noise == 0) { $args = "$args\q"; }
    if($batch == 1) { $args = "$args\B"; }
    $args = "$args\D  $config{CD_DEVICE}";
    if($batch == 0) { $args = "$args -t $a\+$a $input"; }

    $command = "nice -n $ripnice cdda2wav $args $extra";
}
####

#### #### ####
sub encode {
    for ($i=0;$i<$trknm;$i++) {
# Set #'s human-readable for track names
	$a = $i + 1;
	if($indi != 0) { $a = $indi; $i = $a - 1; }
	$a_length = length($a);
	if ($a_length != 2) { $a = "0$a"; }

	if($rip == 0) { $input = "$wavdir$track_name_wav[$i]"; }
	if($rip == 1) { $input = "track$a\.cdda\.wav"; }
	if($rip == 2) { $input = "audio_$a.wav"; }

# Call Ripper if not Batch Rip
	if($batch == 0 && $rip == 1) { &paranoia; }
	if($batch == 0 && $rip == 2) { &cdda2wav; }
	if($batch == 0 && $rip != 0) { &run; }

	if($noise > 0 && $batch == 0) { print "$track_name_wav[$i] ripped\n"; }

# Call Encoder
	if ($enc == 1) { &lame; }
	elsif ($enc == 2) { &bladeenc; }
	elsif ($enc == 3) { &gogo; }
	elsif ($enc == 4) { &xing; }
	elsif ($enc == 5) { &mp3enc; }
	&run;

# ID3 Tag Files
	if ($id3_vers != 0) { &id3; }
	&run;

# Make Playlist Entry.
	if($playlist) { print PLAYLIST "$mp3dir$track_name[$i]\n"; }

# Move MP3 files to final place
	$command = "mv \"$track_name[$i]\" \"$mp3dir$track_name[$i]\"";
	&run;

	unless($step == 2) { &wav; }

	if ($noise != 0) { print "$track_name[$i] encoded\n"; }

	if($indi != 0) { $i = $trknm; }
    }
}
####

#### #### ####
sub lame {
    $args = "";

    if($noise == 0) { $args = "-S"; }
    if($error) { $args = "$args -p"; }
    $args = "$args -b $bit";

    $command = "nice -n $encnice lame $args \"$input\" \"$track_name[$i]\" $extra";
}
####

#### #### ####
sub bladeenc {
    $args = "-$bit -q -nocfg";

    if($error) { $args = "$args -crc"; }
    if($noise == 0) { $args = "$args -quiet"; }

    $command = "nice -n $encnice bladeenc $args \"$input\" \"$track_name[$i]\" $extra";
}
####

#### #### ####
sub gogo {
    $args = "-b $bit";

    if($noise == 0) { $args = "$args -silent"; }
    if($noise == 2) { $args = "$args -debug"; }

    $command = "nice -n $encnice gogo $args \"$input\" \"$track_name[$i]\" $extra";
}
####

#### #### ####
sub xing {
    $args = "-B$bit";
    if($noise == 0) { $args = "$args -S"; }

    $command = "nice -n $encnice xingmp3enc \"$input\" \"$track_name[$i]\" $args $extra";
}
####

#### #### ####
sub mp3enc {
    $args = "-br $bit";

    if($noise == 2) { $args = "$args -v"; }
    if($error == 1) { $args = "$args -crc"; }

    $command = "nice -n $encnice mp3enc -if \"$input\" -of \"$track_name[$i]\" $args $extra";
}
####

#### #### ####
sub id3 {
    $comm = "";
    if($comment) { $comm = "-n $comment"; }
    $id3 = "-t $id3_track[$i] -a $id3_title -r $id3_artst -G \"$cat\" $comm";

    $command = "id3tool $id3 \"$track_name[$i]\"";
}
####

#### #### ####
sub wav {
# (Re)Move WAV files
    $a = $i + 1;
    if($a < 10) { $a = "0$a"; }

    if($rip == 0) { $input = "$track_name_wav[$i]"; }
    if($rip == 1) { $input = "track$a.cdda.wav"; }
    if($rip == 2) { $input = "audio_$a.wav"; }

    if($wav == 0) { $command = "rm \"$input\""; &run; }
    else {
	$command = "mv \"$input\" \"$wavdir$track_name_wav[$i]\" -f";
	&run;
    }
}
####

#### #### ####
sub time {
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    if($hour >= 13) { $hour = $hour - 12; }

    $diff{hour} = $hour - $time[0];
    if($diff{hour} < 0) { $diff{hour} = $diff{hour} + 12; }
    $diff{min} = $min - $time[1];
    if($diff{min} < 0) { $diff{min} = $diff{min} + 60; $diff{hour}--; }
    $diff{sec} = $sec - $time[2];
    if($diff{sec} < 0) { $diff{sec} = $diff{sec} + 60; $diff{min}--; }

    @time = ($hour,$min,$sec,$tmp);
    @total = ($diff{hour},$diff{min},$diff{sec});
}
####

#### #### ####
sub quit {
    if($playlist) { close(PLAYLIST); }

# Grab Final Time & Print to ~/.tear-debug
    if($debug == 1) {
	&time;
	print DEBUG "$time[0],$time[1],$time[2]\n";
	print DEBUG "$total[0],$total[1],$total[2]\n";
	close(DEBUG);
    }

# Corrects Occasional Side Effect Of Paranoia
    if (-e "track00.cdda.wav") { system("rm track00.cdda.wav"); }

# BEEP
    print "\a";
    exit;
}
