#!/usr/bin/perl

##################################################
#
# GiantDisc mp3 Jukebox
# 
# © 2000, Rolf Brugger
#
##################################################

# gdripcd: Giant Disc rip cd
# reads tracks to be ripped from stdin (pipe)
#

use lib '/home/music/bin';
use gdgentools;
use FileHandle;		# for autoflush
use DBI;
use strict;

### Global constants
my $pl_list = 0;
my $rp_list = 1;
my $co_list = 2;
my $cr_list = 3;

### Parameters
if(scalar(@ARGV) != 3){print("gdripcd needs three parameters (db-host, id and nb)\n");exit(0);}
my $dbhost   = $ARGV[0];
my $playerid = $ARGV[1];
my $audiochannel = $ARGV[2]; 

### open DB connection
my $dbh = DBI->connect("DBI:mysql:GiantDisc:".$dbhost, "music", undef)
    or die "unable to connect to GiantDisc db";

### set termination handler
$SIG{TERM} = sub { print "gdripcd killed\n"; $dbh->disconnect; exit;}; # set Kill event handler


### Open Compress pipe
open (COMPPIPE, "| gdcompr.pl $dbhost $playerid $audiochannel")
  or die "can't fork compress pipe\n";
autoflush COMPPIPE 1;


my $cdripper  = gdgentools::param_value($dbh, $playerid, "cdripper");
my $logtarget = gdgentools::param_value($dbh, $playerid, "logtarget");

#if($logtarget eq "stdout"){
#  print ("CD-Ripping Deamon Started for player $playerid channel $audiochannel\n");
#}
#else{
#  open OUTLOG, ">/dev/null";
#  select OUTLOG;
#}

my ($useparanoia, $wavfile, $ret);

### Get rip arguments
while(<STDIN>){
  my $argline=$_;

  my($action) = (split /\t/, $argline)[0];

  if ($action eq "move"){   ### move ready audio file
    my($action, $sourcefile, $targetfile, $linkname) = split /\t/, $argline;

    system("mv \"$sourcefile\" $targetfile ");
    system("rm $linkname ");
  }
  
  if ($action eq "rip"){    ### rip track from CD
    my($action, $tracknb, $trackid, $bitrate, $length, $filepath, 
	$filename, $artist, $title) = split /\t/, $argline;
	
    my $trid = gdgentools::tracklist_get_item($dbh, $playerid, $rp_list, 0); 
  
    ### Remove other formats first (existing mp3 file could be re-recorded as ogg file)
    if (length($filename)>8){ # make sure we don't accidentially remove wrong files
      system "rm -f $filepath/$filename-$tracknb.*";
    }
    
    ### Rip it
    $useparanoia=1;
    $wavfile = "$filepath/$filename-$tracknb.wav";
    print "gdripcd: $tracknb. $artist - $title to $wavfile\n";
    system("sleep 1s");  # sleep a second
  
    if($cdripper eq "cdparanoia"){
      ### Use cdparanoia
      #print("\nEXECUTING: cdparanoia -w $tracknb $wavfile\n");
      $ret=system("cdparanoia -w $tracknb $wavfile");
    }
    if($cdripper eq "cdda2wav"){
      ### Use cdda2wav
      # -H: no infofiles
      $ret=system("cdda2wav -D /dev/cdrom -t$tracknb -H $wavfile");
      #system("rm $fnameroot.cddb; rm $fnameroot.inf");
    }
  
    ### remove first track from riplist
    gdgentools::tracklist_del_item($dbh, $playerid, $rp_list, 0);
  
    ### compress the grabbed wav file
    gdgentools::tracklist_append_list($dbh, $playerid, $co_list, $trid);
    print (COMPPIPE "$argline");  #send same arguments as received
  }
}


$dbh->disconnect;
print ("Teminated: gdripcd\n");
close COMPPIPE;

exit;

#

