#!/usr/bin/perl

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


# gdtri2txt.pl: Giant Disc, translate one tri file to a text line
#               the fields are tab separated.
#		It also creates a database record.
#
# takes as arguments the .tri files to be translated
#
# Output should be redirected to tmp/tracks.txt



### BUGS: this script reads lyrics into the variable $lyrics but it
#         doesn't store them in the database!



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

### Parse Arguments
if($#ARGV < 0){
  print "Error: at least one .tri file as argument required\n";
  print "       Option: -n  do not write a database record\n";
  exit(-1);
}

if($ARGV[0] eq "-n"){
  shift;
  $writedb = 0;
  print("Database writing skipped\n");
}
else{
  $writedb = 1;
}

my $dbhost = "localhost";

### 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++){
  ### Analyze .tri file line by line

  $tracknb = $artist = $title = $genre2 = $genre1 = "";
  $year = $lang = $type = $rating = "";
  $lentot = $source = $sourceid = "";
  $condition = '0';
  $lengthfrm = $startfrm = $bpm = $voladjust = '0';
  $created = $modified = $id = $lyrics= "";


  # fill in default values
  $type = 1;   # normal
  $source = 0; # cd
  $trifn = $ARGV[$i];
  $mp3fn = $trifn;
  $mp3fn =~ s/.tri/.mp3/;
  $lentot = `mp3info -p "%S" $mp3fn`;
  chop($lentot);
  $sourceid = '';
  if($mp3fn=~m/(tr.x[0-9a-f]*-?[0-9]*.mp3)\s*/){
    $mp3file=$1;
  }
  if($trifn=~m/tr0x([0-9a-f]*)-[0-9]*.tri\s*/){
    $sourceid=$1;
  }

  
  open(TRIFILE, $ARGV[$i]);
  while(<TRIFILE>){
    $line = $_;

    # match: IDENTIFIER=(strings-not-ended-by-whitespace) whitespace


    if($line=~m/^TRACK=(.*\S)\s*/){
      $tracknb=$1;
    }
    if($line=~m/^TTITLE=(.*\S)\s*/){
      $title=$1;
    }
    if($line=~m/^TARTIST=(.*\S)\s*/){
      $artist=$1;
    }
    if($line=~m/^TGENRE1=(.*\S)\s*/){
      $genre1=$1;
    }
    if($line=~m/^TGENRE2=(.*\S)\s*/){
      $genre2=$1;
    }
    if($line=~m/^TYEAR=(.*\S)\s*/){
      $year=$1;
    }
    if($line=~m/^TLANG=(.*\S)\s*/){
      $lang=$1;
    }
    if($line=~m/^TTYPE=(.*\S)\s*/){
      $type=$1;
    }
    if($line=~m/^TRATING=(.*\S)\s*/){
      $rating=$1;
    }
    if($line=~m/^TLENGTH=(.*\S)\s*/){
      $lentot=$1;
    }
    if($line=~m/^TSOURCE=(.*\S)\s*/){
      $source=$1;
    }
    if($line=~m/^TSOURCEID=(.*\S)\s*/){
      $sourceid=$1;
    } 
    if($line=~m/^TCONDITION=(.*\S)\s*/){
      $condition=$1;
    } 
    if($line=~m/^TVOLADJUST=(.*\S)\s*/){
      $voladjust=$1;
    } 
    if($line=~m/^TLENGTHFRM=(.*\S)\s*/){
      $lengthfrm=$1;
    } 
    if($line=~m/^TSTARTFRM=(.*\S)\s*/){
      $startfrm=$1;
    } 
    if($line=~m/^TBPM=(.*\S)\s*/){
      $bpm=$1;
    } 
    if($line=~m/^TBITRATE=(.*\S)\s*/){
      $bitrate=$1;
    } 
    if($line=~m/^TCREATED=(.*\S)\s*/){
      $created=$1;
    } 
    if($line=~m/^TMODIFIED=(.*\S)\s*/){
      $modified=$1;
    } 
    if($line=~m/^TLYRICS=/){
      # read lyrics until end of file
      while(<TRIFILE>){
        $lyrics .= $_;
      }
    } 
  }

  if (length($created)<4){
    $created='1999-01-01';
  }

  print("$title\t");
  print("$artist\t");
  print("$genre1\t");
  print("$genre2\t");
  print("$year\t");
  print("$lang\t");
  print("$type\t");
  print("$rating\t");  
  print("$lentot\t");
  print("$source\t");
  print("$sourceid\t");
  print("$tracknb\t");
  print("$mp3file\t");
  print("$condition\t");
  print("$voladjust\t");
  print("$lengthfrm\t");
  print("$startfrm\t");
  print("$bpm\t");
  print("\t");   #/*lyrics*/
  print("$created\t");
  print("$modified");
  print("\n");
  print("LYRICS:$lyrics\n");

  close(TRIFILE);
  
  if($writedb){
    $trid = gddb::insert_track_record($dbh,$artist,$title,$genre1,$genre2,$year,
               $lang,$type,$rating,$lentot,$source,$sourceid,
               $tracknb,$mp3file,$condition,$voladjust,
               $lengthfrm,$startfrm,$bpm,$bitrate,
               $created,$id);
  }
}

print ("recalculate genre/language frequencies\n");
system("gdrefresh.pl --dbhost $dbhost --trkgenre --trklanguage --albgenre");

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


### end
