#!/usr/bin/perl -w

## begin POD documentation

=head1 NAME
    artemis-lib - a library of general functions and global variables
                  for Artemis mp3 search engine

=head1 SYNOPSIS

=head1 DESCRIPTION

    this is a collection of general use library functions for the
    artemis mp3 search engine.

=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

    artemis.cfg       the config file

=head1 SEE ALSO

    artemis           the main artemis man page
    validator         the validation engine
    artemis-spider    the background spidering program
    db-wrapper        a wrapper for the database

=head1 DIAGNOSTICS

=head1 BUGS

    none that we know of yet.

=head1 NOTES

=head1 HISTORY

    2000-02-29 -- anders
        filled in read_config()
        added print_error_and_die()
        added %cfg
    2000-02-28 -- anders
        genesis.

=cut

## end POD documentation

    use strict;

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

# define our supported filetypes.
#              extension    format name
my %File_Types = ('mp3' => 'mp3',
		  'ram' => 'realaudio',
		  'rm'  => 'realaudio',
		  'wav' => 'windows WAVE',
		  'mov' => 'quicktime',
		  'au'  => 'sun audio',
		  'aiff'=> 'audio interchange file format',
		  'mod' => 'amiga mod',
		  'mid' => 'midi',
		  'midi'=> 'midi');

# a quick list of supported file extensions
my @File_Exts = ('mp3','ram','rm','wav','mov','au','aiff','mod','mid','midi');

# make a handy regular expression macro that
# will match any of our supported filetypes

my $File_Ext_RE = '(';
foreach(@File_Exts){
    $File_Ext_RE .= "$_|";
}
substr($File_Ext_RE,-1) = ")";

# define our configuration hashtable and set the defaults

my %cfg = ('search_timeout' => 5,
	   'validator_timeout' => 10);

# --------- SUBROUTINES --------------- #

&test();

sub test{
    "foo.MP3" =~ /$File_Ext_RE/i;
    print $1;
}

sub read_config{
    # description:
    #  reads in the configuration file
    #  config file should be in the format of
    #  key: value
    #  currently doesn't deal well with blank lines and
    #  other weirdness. 
    # preconditions:
    #  config file must exist
    # postconditions:
    #  %cfg hash populated
    # inputs:
    #  optional: [path to config file]
    # outputs:
    #  reference to %cfg if successful
    #  0 if couldn't open file or format error

    my $cfg_file = shift @_ || "artemis.cfg"; # default to artemis.cfg
    open(CFG,"$cfg_file") or return 0;

    my @cfg = <CFG>;
    foreach (@cfg){
	# read in each line and split
	# into key/value pair on a colon
	# followed by whitespace
	my($key,$value) = split(/:\w*/, chomp($_));
	
	$cfg{$key} = $value;
    }
    return \%cfg;
}

sub guess{
    # description:
    #  attempts to guess artists name / song title purely from
    #  filename
    #  deals with names of form ARTIST - TITLE.ext
    #  which is common with many mp3 rippers (deals with any
    #  kind of whitespace: ' ', '_', '%20', etc);
    # preconditions:
    #  $File_Ext_RE defined
    # postconditions:
    #  none
    # inputs:
    #  filename or url
    # outputs:
    #  (artist, title), 0 if it can't make a guess.

    # not written yet
}

sub print_error_and_die{
    # prints out an error message and dies
    # to be called in case of a fatal error
    
    my $error_message = shift @_;
    print "Artemis ERROR: $error_message\n";
    die;
}










