#!/usr/bin/perl 
# only meant to use with dagrab
my $prog="gogo";#I think you could use lame instead, but gogo is faster
my $enable_tag;
BEGIN{
    $enable_tag=1;
    unless(eval "require MPEG::MP3Info") { $enable_tag=undef;};
};
sub usage{
    die <<EOF;
dagmp3enc - encode an mp3 file using $prog, and sets ID3 tags for the file.
    only meant to use from dagrab.
    ID3 tags are a way to embed information such as album title, song name ,
    track number and other not available from CDDB.
    Tag length is limited to about 30 char.
    ID3 tags setting is only available if you have MPEG::MP3Info perl module
    installed.
    
    usage: dagmp3enc [-d] <wav> <artist> <album> <track #> <title> <title(fn)>
    where the first title,unmodified, will be stored in the ID3 tag, and the
    second is for the filename, and is usually mangled to get a valid filename
    without spaces.
    if -d option is set, the original wav files are deleted after encoding.
EOF
};
my $del="";
if ($ARGV[0] eq "-d") {$del="-delete"; shift};
usage if $#ARGV != 5;
my ($wav,$art,$alb,$tn,$track,$trackfn)=@ARGV;
my ($mp3fn)=sprintf "%02d-%s.mp3",$tn,$trackfn;
$cmd=sprintf("$prog %s %s %s",$wav,$del,$mp3fn);
printf "%s\n",$cmd;
system $cmd;
if ($enable_tag){
    MPEG::MP3Info->import; 
    MPEG::MP3Info::set_mp3tag $mp3fn,$track,$art,$alb,"","","",$tn;
}
else  
{
    print "MPEG::MP3Info not installed - can't set ID3 tags\n";
};