#! /usr/bin/perl -T
#
# showscore.pl
#
# (c) 1999-2000 Sibelius Software Limited
#
# $Id: showscore.pl,v 1.1.1.1 2000/06/23 10:10:45 dmatthewman Exp $
#
# this script shows a single score, whose file name has been passed as a parameter

use strict;

$ENV{'PATH'} = '/usr/bin:/bin:/usr/local/bin';
delete @ENV{ 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

use lib qw(./); 
use ScorchLib::Configuration;
use ScorchLib::Catalog;
use ScorchLib::Cart;
use ScorchLib::HTMLTemplate;

my $cart = new ScorchLib::Cart;
my $catalog = new ScorchLib::Catalog;
my $page = new ScorchLib::HTMLTemplate;
my $config = new ScorchLib::Configuration;

# Noa's method of creating the right cookie and serverside *.txt and deleting useless *.txt
$cart->load();
$cart->tidyCarts();
my $cookie = $cart->save();
print "Set-cookie: $cookie\n" if (defined $cookie && $cookie ne '');

my $filename = $ENV{QUERY_STRING};
my $cart_id = $cart->getCounter();

# look for the title of this score
$catalog->load();
my $score_title		= $catalog->scoreTitle($filename);
my $score_author	= $catalog->scoreAuthor($filename);
my $score_price 	= $catalog->scorePrice($filename); # Noa added

# open the showscore template and print it out

my $script_url		= $config->{'urls'}->{'addtobasket'};
my $filename_url	= "$config->{'urls'}->{'score'}/$filename";

my ($html_title, $html_content, $html_footer) = $page->load($config->{'templates'}->{'showscore'});

my $scorch_command		= <<EOHTML;

<object id="ScorchPlugin"
classid="clsid:A8F2B9BD-A6A0-486A-9744-18920D898429"
width="578"
height="840"
codebase="http://www.sibelius.com/download/software/win/ActiveXPlugin.cab#version=5,2,1,4" >
<param name="src" value="$filename_url">
<param name="type" value="application/x-sibelius-score">
<param name="scorch_minimum_version" value="5210">
<param name="scorch_preferred_version" value="5210">
<param name="scorch_shrink_limit" value="100">
<param name="scorch_command" value="view">
<param name="scorch_num_copies" value="1">
<param name="scorch_price" value="0">
<param name="scorch_first_page_footer" value="www.harmageddon-music.com - Page 1 preview">
<param name="scorch_second_page_footer" value="www.harmageddon-music.com - Page 1 preview">
<param name="scorch_base_url" value="http://$ENV{'SERVER_NAME'}/musicshop/cgi-bin/">
<param name="scorch_cart_id" value="$cart_id">
<embed src="$filename_url"
	scorch_minimum_version="5210"
	scorch_preferred_version="5210"
	scorch_shrink_limit="100"
	width="578"
	height="840"
	type="application/x-sibelius-score"
	pluginspage="http://www.sibelius.com/cgi/plugin.pl"
	scorch_command = "view"
	scorch_num_copies = "1"
	scorch_price = "0"
	scorch_first_page_footer = "www.harmageddon-music.com - Page 1 preview" 
	scorch_second_page_footer = "www.harmageddon-music.com - Page 1 preview"
	scorch_base_url = "http://$ENV{'SERVER_NAME'}/musicshop/cgi-bin/"
	scorch_cart_id = "$cart_id">
</embed>
</object>

EOHTML

$html_title	= HTMLReplaceScore($html_title);
$html_content	= HTMLReplaceScore($html_content);
$html_footer	= HTMLReplaceScore($html_footer);

$page->print($html_title, $html_content, $html_footer);

sub HTMLReplaceScore
{
	my $html = shift;
	$html = '' unless defined $html;
	
	$html =~ s/SCORE_FILENAME/$filename_url/gim;
	$html =~ s/SCORE_ID/$cart_id/gim;
	$html =~ s/SCORE_ADDTOBASKET/$script_url/gim;
	$html =~ s/SCORE_TITLE/$score_title/gim;
	$html =~ s/SCORE_AUTHOR/$score_author/gim;
	$html =~ s/SCORE_PRICE/$score_price/gim; # Noa added
	$html =~ s/SCORE_FILE/$filename/gim;
	$html =~ s/SCORCH_COMMAND/$scorch_command/gim;

	# Noa checks if the song is faraway or one of those others, we'll put a different SPECIAL_CONTENT_INFORMATION text
	if (($filename eq 'faraway.sco') || ($filename eq 'faraway_2vl_vla_vc_pf.sco') || ($filename eq 'ruska.sco')) {
		$html =~ s/SPECIAL_CONTENT_INFORMATION/<p class=\"info\"><font color=\"#FFFF00">Note: This song contains a <b>five-part score<\/b> and <b>five separate parts<\/b>.<\/font><\/p>/gim;
	} else {
		$html =~ s/SPECIAL_CONTENT_INFORMATION/<p class=\"info\">Note: This song contains a <b>four-part score<\/b> and <b>four separate parts<\/b>.<\/p>/gim;
	}
	
	return $html;
}
