#!/usr/bin/perl

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


# takes as arguments the text files that should be added as lyrics to
# database records
#



use lib '/home/music/bin';
use gdgentools;
use gddb;
use DBI;

### Parse Arguments
if($#ARGV < 0){
  print "Error: at least one text file as argument required\n";
  print "Filename convention:\n";
  print "  A lyrics file must have the same name as the corresponding mp3\n";
  print "  file, except for the extension which is .txt (instead of .mp3).\n";
  exit(-1);
}


### Open database connection
print ("### Open database connection\n");
$dbh = DBI->connect("DBI:mysql:GiantDisc", "music", undef)
    or die "unable to connect to GiantDisc db";


for($i=0;$i<=$#ARGV;$i++){
  $lyrics= "";

  # get filename of corresponding mp3 file
  $lyrfn = $ARGV[$i];
  $mp3fn = $lyrfn;
  $mp3fn =~ s/.txt/.mp3/;

  open(TRIFILE, $ARGV[$i]);
  # read lyrics until end of file
  while(<TRIFILE>){
    $lyrics .= $_;
  }

  $res = $dbh->do(
     "UPDATE tracks "
    ."SET lyrics=".$dbh->quote($lyrics)
    ."WHERE mp3file='".$mp3fn."'");
  if ($res == 1){
    print("importing LYRICS $lyrfn \n");
  }
  else{  
    print("could not import lyrics file $lyrfn! No corresonding record found in database ($mp3fn)\n");
  }

  close(TRIFILE);
  
  
}

print("### close database connection\n");
$dbh->disconnect;


### end
