#!/usr/bin/perl -w

=head1 NAME

    db-wrapper - a wrapper library for Artemis
    
=head1 SYNOPSIS

    a synopsis goes here
    
=head1 DESCRIPTION
    
    this program/library provides a nice interface
    to our data files. it makes it so we don't have
    to worry about the specifics of the database in
    our other scripts.

=head1 OPTIONS

=head1 RETURN VALUE

=head1 EXAMPLES

=head1 ENVIRONMENT

=head1 AUTHOR

    anders pearson <F<anp8@columbia.edu>>
    david masao dodobara <F<dmd69@columbia.edu>>
    guillermo m ramos <F<gmr9@columbia.edu>>
    karl mcclare questelles <F<kmq2@columbia.edu>>
    miriam shana adlerstein <F<msa22@columbia.edu>>

=head1 FILES

    $Data_File      the main database file
                    defaults to 'data'
    $Legacy_File    old format data
                    defaults to 'legacy_data'

=head1 SEE ALSO

    artemis         the main manpage for the artemis
                    mp3 search engine
    validator       the validation engine
    artemis-spider  the background spidering program

=head1 DIAGNOSTICS

=head1 BUGS

    none that we know of yet.

=head1 NOTES

=head1 HISTORY
    2000-03-04 -- anders
        filled in find_by_artist, find_by_song
        added search_for_artist()
        and find_from_key()
    2000-02-29 -- anders
        filled in parse_line() and add_entry()
    2000-02-28 -- anders
        genesis.

=cut

#' this is here to fix my code coloring. please ignore.
    
use strict;

# ---------- GLOBAL VARIABLES ------------- #

=head2 global variables
    $Data_File     the file that contains
                   our data.
    $Legacy_File   the file that contains
                   data in the old format
=cut               

my $Data_File = "data";   
my $Legacy_File = "legacy_data";


my %Record = ("key"    => '', # unique key
	      "artist" => '', # the artist name
	      "song"   => '', # the title of the song
	      "format" => '', # mp3, wav, realaudio, etc
	      "size"   => '', # file size in kB or mB 
	      "url"    => '', # the location of the file
	      "last_validated" => '');
	      
# &test();

sub test{
    print search_for_artist('clapton');
}
    
# ---------- SUBROUTINES ------------------- #

sub parse_line_leg{

    ## begin POD documentation
    
=head2 parse_line_leg($)

		      
     description:
       reads a line of the legacy database file
       and populates the Record hash with the
       proper values
     preconditions:
       none.
     postconditions:
       Record hash is populated.
     inputs:
       the line from the data file
     outputs:
       returns a reference to the Record hash
=cut

    ## end POD documentation
		      
    my $line = shift @_;
    chomp $line;
    my $part_one;
    ($part_one,
     $Record{"url"}) = split(' ', $line); # pull the url off the end
    ($Record{'artist'},
     $Record{'song'},
     $Record{'format'},
     $Record{'size'}) = split('/', $part_one); # break it up
    $Record{'artist'} =~ s/^\#//;    # remove the '#' from the beginning.
    return \%Record;
}

sub parse_line{
    # parses a line of the db file and populates
    # the %Record hash with its contents

    my $line = shift @_;
    chomp $line; #get rid of pesky newlines
    ($Record{'key'},
     $Record{'artist'},
     $Record{'song'},
     $Record{'url'},
     $Record{'format'},
     $Record{'size'},
     $Record{'last_validated'}) = split(/\t/,$line);
    return \%Record;
}

sub add_entry{

##    description
##	adds an entry to the data file.
##    preconditions
##        %Record hash must have some info in it
##	$Data_File should be defined.
##    postconditions
##        Record hash is populated.
##    inputs
##        the line from the data file
##    outputs
##        returns a reference to the Record hash
## End POD documentation
    
    open(FILE,">>$Data_File")
	or &printErrorAndDie("couldn't append to $Data_File");

    my $current = &get_current();
    $Record{'key'} = $current + 1;
    foreach ('key','artist','title','url','format','size'){
	print FILE "$Record{$_}\t";
    }
    print FILE "$Record{'last_validated'}\n";

}

sub add_legacy_entry{

    # adds an entry to the legacy data file

}

sub find_by_artist{
    # finds all entries associated with
    # an artist in the database

    # returns an array of hashes of data
    
    my $artist = shift @_;
    my @results;
    open(FILE, "$Data_File") or die;
    my %gotten;
    my $part;
	
    while(<FILE>){
	parse_line $_; # loads the %Record hash
	foreach $part (split(/\+/,$artist)){
	    if((!$gotten{$Record{'key'}}) && # make sure we haven't already
	                                     # gotten this one
	       (($Record{'artist'} =~ /$part/i) ||
		($Record{'url'} =~ /$part/i))){
		# we found one
		# add it to the list
		my %temp = %Record;
		push(@results,\%temp);
		$gotten{$Record{'key'}} = 1; # make sure we won't try to get
		                             # this one again
#		print STDERR "$part\t";
	    }
	}
    }
    return @results;
    
}

sub find_by_song{
    # finds all entries for a given song
    # in the database

    my $song = shift @_;
    my @results;
    open(FILE,"$Data_File") or die;

    while(<FILE>) {
	parse_line $_;
	if(($Record{'song'} =~ /$song/i) ||
	   ($Record{'url'}  =~ /$song/i)) {
	    # we found one that looks like a match
	    push(@results,$Record{'key'});
	}
    }
    return \@results;
}

sub find_from_key{
    my $key = shift @_;
    open(FILE,"$Data_File") or die;

    while(<FILE>){
	parse_line $_;
	if($Record{'key'} eq $key){
	    return \%Record;
	}
    }
    return 0; # if we're here it means that the
              # key wasn't in the database
}

sub search_for_artist{
    # WARNING: with a change i just made to search_by_artist, this is broken
    # returns the results of searching for an artist
    # format is a string with key/value pairs seperated by
    # newlines. each key and value seperated by a tab.
    # records are separated by two newlines.

    # this output should be parsed by slurping one record at a time
    # splitting on newlines, then entering values into a hashtable
    # by splitting on tabs. 

    my $artist = shift @_;
    my @results = find_by_artist $artist;
    my $key = 0;
    my $return_string = "";
    foreach $key (@results){
	if(find_from_key $key){
	    $return_string .= "artist\t$Record{'artist'}\n"
	                   . "song\t$Record{'song'}\n"
	                   . "format\t$Record{'format'}\n"
	                   . "size\t$Record{'size'}\n"
	                   . "url\t$Record{'url'}\n"
	                   . "last validated\t$Record{'last_validated'}\n\n";
	}
    }
    return $return_string;
}


# ----------------------- END -------------------- #















