#  CDDB - Read the CDDB entry for an audio CD in your drive
#
#  This module/script gets the CDDB info for an audio cd. You need
#  LINUX, a cdrom drive and an active internet connection in order
#  to do that.
#
#  (c) 2002 Armin Obersteiner <armin@xos.net>
#
#  LICENSE
#
#  This library is released under the same conditions as Perl, that
#  is, either of the following:
#
#  a) the GNU General Public License Version 2 as published by the
#  Free Software Foundation,
#
#  b) the Artistic License.
#
#  Does as above, but as a simle function call (by Aaron Birenboim)

use CDDB_get qw( get_cddb get_discids );
sub MyCDDBget{

  ## if CD that made these tracks is still in drive, try to guess
  # from cddb
  my %config;
###############################################################
# following variables just need to be declared if different from defaults
# defaults are listed below (cdrom default is os specific)

# $config{CDDB_HOST}="freedb.freedb.org";	# set cddb host
# $config{CDDB_PORT}=888; 			# set cddb port
# $config{CDDB_MODE}="cddb";			# set cddb mode: cddb or http, this is switched with -f
$config{CD_DEVICE}="/dev/cdrom";		# set cd device
#$config{CD_DEVICE}="/dev/sg0";		# set cd device

# $config{HELLO_ID} ="root nowhere.com fastrip 0.77"; # hello string: username hostname clientname version

#$CDDB_get::debug=1 if($option{D});

# get proxy settings for cddb mode
#$config{HTTP_PROXY}=$ENV{http_proxy} if $ENV{http_proxy}; # maybe wanna use a proxy ?

#$config{CDDB_MODE}="http" if($option{f}); 

# user interaction welcome?

  $config{input}=0;   # 1: ask user if more than one possibility
                      # 0: no user interaction
  $config{multi}=0;   # 1: do not ask user and get all of them
                      # 0: just the first one

  local $artist = "UNKNOWN";
  local $album = "UNKNOWN";
  local %cd;
  eval { 
    %cd = get_cddb(\%config);
  };
  if ($@) {
    print "CDDB failed: $!\n";
    return("UNKNOWN","UNKNOWN");
  }
  foreach (keys %cd)
    {
      print "$_\t",$cd{$_},"\n";
    }
  print "*** Track Data:\n";
  local @trk = @{$cd{"track"}};

  local $n = 1;
  foreach $trk0 ( @{$cd{"track"}} )
    {
      print "$n $trk0\n";
      #print "$n $trk : from ",$cd->{frames}[$trk-1]," to ",$cd->{frames}[$trk]-1,"\n";
#      if ($n <= $#files + 1)
#        {
#          $trackName[$n-1] = $trk;
#        }
      $n++;
    }

  $artist = $cd{"artist"} if (defined($cd{"artist"}));
  $album  = $cd{"title"}  if (defined($cd{"title"}));
  return ($artist,$album,@trk);
}
1;
__END__
