#!/usr/bin/perl

# cd2mp3 - MP3 utility suite  - JAH 05/07/00
# Uses Bladeenc and Dagrab 
# Ver 0.3

# Print boasting stuff

print "\n\n  /================================================\\ \n";
print "  |           Cd2Mp3 - V0.3 05/07/00 JAH           |\n";  
print "  |          http://www.jamiesdomain.co.uk         |\n";
print "  |================================================|\n";
print "  | Please check dagrab and bladeenc are installed |\n";
print "  |              In your path                      |\n";
print "  |================================================|\n";
print "  |   This free software project is sponsored by   |\n";
print "  |     Rebel Net - http://www.rebel-net.co.uk     |\n";
print "  \\================================================/\n"; 

sub check {
     if ($presamp =~ /\b56\b/) {
         $finalsamp = "56";
        }
     elsif ($presamp =~ /\b128\b/) {
         $finalsamp = "128";
        }
     elsif ($presamp =~ /\b256\b/) {
         $finalsamp = "256";
        }
     else {
        print("\n 56K Default used.\n ");
         $finalsamp = "56";
       }
}
      
# This function makes sure the info passed to bladeenc is correct

sub sytx_correct {
            if ($track <= 9) {
             $fileprfx = "track0";
             }
            else {
             $fileprfx = "track";
            }
}

# Function to run dagrab and bladeenc, and clean up any temp files

sub encode {
           system ("dagrab -v -d $drive $track");
           print "\n$fileprfx$track Ripped!! Encoding to MP3..\n";
           system ("bladeenc -br $finalsamp $fileprfx$track.wav $outputmp3"); 
           system ("rm $fileprfx$track.wav");
           print "\nMP3 Created as $outputmp3. Thanks for using cd2mp3.\n\n"; 
}

# Entire disk routine

sub entire {
           print("Where is your CD-ROM Drive?? Usually /dev/cdrom: ");
           print STDIN " ";
           chop($alldrive = <STDIN>);
           print("\n Using $alldrive \n\n");
           print("Enter Sampling rate. Default is 56K \(Values are 56, 128 or 256\): ");
           print STDIN " ";
           $presamp = <STDIN>;
           check();
           print "\n Using a bitrate of $finalsamp\K \n";
           print "\nOutput directory for mp3's \(ie: /usr/home/sparc/mp3\): ";
           print STDIN " ";
  	   chop($dest = <STDIN>);
           print "\nOK, encoding entire CD, output to $dest\n\n";
	   system("dagrab -v -d $alldrive -a -f $dest/track\%02d.wav");
           system("bladeenc -br $finalsamp $dest/*.wav");
           qx{rm $dest/*.wav};
           print "\nCD encoded in $dest. Thanks for using cd2mp3!\n\n";
           exit;
}	
           
# .WAV file routine

sub wav {
        print "\nWhere is your CD-ROM Drive?? Usually /dev/cdrom: ";
        print STDIN " ";
        chop($wavdrive = <STDIN>);
        print "\n Using $wavdrive \n";
        print "\nOutput .WAV file to \(ie: /usr/home/sparc/audio/mytrack.wav\): ";
        print STDIN " ";
        chop($wavout = <STDIN>);
        print "\n OK, output file will be $wavout\n";
        print "\nWhich track do you want to rip? \(Use Track Number Ie 5 For Track5\): ";
        print STDIN " ";
        chop($wavrip = <STDIN>);
        print "\n Ripping Track $wavrip ! \n\n";
        system("dagrab -d $wavdrive -v -f $wavout $wavrip");
        print "\n Track $wavrip Ripped ! Thanks for using cd2mp3 \n\n";
        exit;
} 

# .WAV to MP3

sub w2m { 
           print "\n";
           print "Enter Sampling rate. Default is 56K \(Values are 56, 128 or 256\): ";
           print STDIN " ";
           $presamp = <STDIN>;
           check();
           print "\n Using a bitrate of $finalsamp\K \n";
           print "\nEnter full path to .WAV file to encode \(ie /usr/home/sparc/mysong.wav\): ";
           print STDIN " ";
           chop($wavpath = <STDIN>);
           print "\nEnter destination for MP3 \(ie /usr/home/sparc/mysong.mp3\): ";
           print STDIN " ";
           chop($wavdest = <STDIN>);
           print "\nDelete .WAV file after encode? \(y/n\): ";
           print STDIN " ";  
           chop($dele = <STDIN>);
           print "\n Encoding $wavpath \n";
           system("bladeenc -br $finalsamp $wavpath $wavdest");
             if ($dele =~ /\by\b/) {
		      system("rm $wavpath");
                  }
             else {
               }
           print "\n $wavpath encoded to mp3. Thanks for using cd2mp3 \n\n";
           exit;
      }

# First check they are root..

if ($< == 0) {
    }
   else {
          print "\n** CDMP3 MUST BE RUN AS ROOT TO RIP TRACKS FROM CD-ROM **\n\n";
          exit
}

menu();

sub menu {
print "\n";
print "1.\) Encode a single track\n";
print "2.\) Encode the entire CD\n";
print "3.\) Rip a track from CD to a .WAV file\n";
print "4.\) Encode a .WAV file to MP3\n";
print "\nWhat would you like to do today?: ";
print STDIN " ";
$eors = <STDIN>;
  if ($eors =~ /\b1\b/) {
          print "\nOK, encoding a single track from CD\n";
          print "\n  Hit Enter to accept defaults\n\n";
      }
   elsif ($eors =~ /\b2\b/) {
          print "\nOK, encoding entire CD\n";
          print "\n  Hit Enter to accept defaults\n\n";
          entire();
      }
   elsif ($eors =~ /\b3\b/) {
          print "\nOK, Ripping a Track to .WAV file\n";
          print "\n  Hit Enter to accept defaults\n";
          wav();
      }
   elsif ($eors =~ /\b4\b/) {
          print "\nOK, Encoding a .WAV file\n";
          print "\n  Hit Enter to accept defaults\n";
          w2m();
      }
   else {
         print "\nBad choice, please enter either 1,2,3 or 4. Try again\n";
         menu();
       }
}

# Sampling rate input and checking

print "Enter Sampling rate. Default is 56K \(Values are 56, 128 or 256\): ";
print STDIN " ";
$presamp = <STDIN>;
check();
print "\n Using a bitrate of $finalsamp\K \n";

# output file

print "\nEnter the path and output file name \(ie /home/jamie/coolmp3.mp3\): ";
print STDIN " ";
chop($outputmp3 = <STDIN>);
print "\n MP3 Will be stored as $outputmp3\n";

# ask for cdrom

print "\nWhere is your CD-ROM Drive?? Usually /dev/cdrom: ";
print STDIN " ";
chop($drive = <STDIN>);
print "\n Using $drive";

# get track number

print "\nWhat track do you want to encode \(Use Track Number Ie 5 For Track5\): ";
print STDIN " ";
chop($track = <STDIN>);
sytx_correct();
print "\n Now Ripping $fileprfx$track!\n\n";

# Execute the encoding routine
encode();
