#!/usr/bin/perl -w
#######################################################################
#
# amazonUPCSearch.pl
#
#	Uses Net::Amazon to perform a UPC search for music.
#
# Version history
#
#	* 0.1	1/27   Program created. Code pretty much taken entirely from Net::Amazon man page.
#
#	* 0.2	1/31	Updated to ask user for input and to extract information from Amazon's response. Added the ability to
#			pass in arguments from the command line.
#
#	* 0.3	2/18	Now it will launch the script "allMusicGuideSearch.pl" (as long as they are in the same directory) after it
#			finds the information it needs.
#
#	* 0.4	2/28	Now it will launch the script "towerSearch.pl". Also, I fixed it so it will decode iso-8859-1 characters.
#
#######################################################################

use strict;
use Encode;

my $VERSION = "0.4";
my $DEBUG = 0;

my $key = ''; # enter Amazon Subscription ID here

print "\nThis is *** amazonUPCSearch *** (version $VERSION). \n";

use Net::Amazon;

my $ua = Net::Amazon->new (token => $key);

my $loop = 1;
while ($loop) {
	$loop = 0 if @ARGV; # assume we don't want to search multiple UPCs if arg passed from command line
	
	print "\nUPC:" unless @ARGV;

	chomp(my $UPC = shift(@ARGV) || <>);

	# exit if input = quit/close/exit or any combination of those letters in the proper order ("quote" works and so does "cx").
									#( I know this is a silly way to do it, but I thought it was fun.)
	last if $UPC =~ m/^[ceq][lnux]?[dio]?[st]?e?$/i;
	print "\n";
	
	debug ("Searching for $UPC...\n");
		
	# Get a request object
	my $response = $ua->search(upc => "$UPC", mode => "music") ;

	unless ($response->is_success()) {
		print "Error: ", $response->message(), "\n";
		next;
	}
	
	my %results = %{$response->properties()};
	my ($artistsRef,$album,$year,$price,$asin) = 
			@results{'artists','ProductName','year','ListPrice','Asin'};
			
	my @artists = @{$artistsRef};
	
	# Sometimes a cd will have multiple artists, so amazon returns a list. 
	# We don't want more than one artist, so we set artist to "Various Artists"
	my $artist;
	if (scalar(@artists) == 1) {
		($artist) = @artists;
	} else {
		$artist = "Various Artists";
	}
	
	$artist = decode("iso-8859-1", $artist);
	
	print "Artist = $artist, Album = $album, ASIN = $asin\n\n";
	
	my @args;
	my $debugFlag = "";
	$debugFlag = "-d" if $DEBUG;
	
	# below is another program I made to find artist names formatted for sorting (returns Dylan, Bob)
	#	it's really not done, but I included it anyway.
	
	# run towerSearch.pl to find sortName	
	print "Searching Tower Records...\n";
	@args = ('c:\perl\bin\perl.exe', 'towerSearch.pl',
				"$UPC", "$artist", "$album");
	system(@args) == 0 or die "system @args failed: $?";		
	
	# run allMusicGuideSearch.pl to find genre
	print "Searching AllMusicGuide...\n\n";
	debug ("Running allMusicGuideSearch.pl to find genre:\n");
	@args = ('c:\perl\bin\perl.exe', 'allMusicGuideSearch.pl',
				 "$artist", "$album", $debugFlag);
	system(@args) and die "system @args failed: $?";
}

#============================ Subroutines ============================#
# debug: print debug statements
# parameters: print statement

sub debug{
  my ($statement) = @_;
  print "DEBUG: $statement\n" if ($DEBUG);
}
