#!/usr/bin/perl

use JSON;
$baseURL = `cat /opt/folio/headerInfo/baseURL.txt`;
$headers = `cat /opt/folio/headerInfo/headers.txt`; 

$json = `curl -s -w '\n' -X GET -G $headers -d 'limit=1000' $baseURL/contributor-name-types`;
$hash = decode_json $json;
for ( @{$hash->{contributorNameTypes}} ) {
 $id = $_->{'id'};
 $name = $_->{'name'};
 $contributorNameTypeId{$name} = $id;
}

$json = `curl -s -w '\n' -X GET -G $headers -d 'limit=1000' $baseURL/contributor-types`;
$hash = decode_json $json;
for ( @{$hash->{contributorTypes}} ) {
 $id = $_->{'id'};
 $name = $_->{'name'};
 $contributorTypeId{$name} = $id;
}

use CGI; 
$query = new CGI;
$title = $query->param('title');
$indexTitle = lc($title);
$indexTitle =~ s/^the //;
$indexTitle =~ s/^an //;
$indexTitle =~ s/^a //;

@artist = $query->param('artist');
for ($x = 0; $x <= $#artist; $x++) {
	$thisContributorType = $query->param("artist$x");
	$thisNameType = $query->param("name$x");
	push(@contributors,"$artist[$x]|$thisNameType|$thisContributorType");
}

if ($#contributors >= 0) {
	$contributorsJSON = qq/"contributors" : [ /;
	foreach $row (@contributors) {
		($name,$nameType,$contributorType) = split(/\|/,$row);
		$contributorsJSON .= qq[{"name":"$name",];
		$contributorsJSON .= qq["contributorNameTypeId":"] . $contributorNameTypeId{$nameType} . qq[",];
		$contributorsJSON .= qq["contributorTypeId":"] . $contributorTypeId{$contributorType} . qq["},];
	}
	$contributorsJSON =~ s/,$/ /;
	$contributorsJSON .= qq/],/;
}
			
$fcn = $query->param('fcn');
$notesJSON = qq[{"instanceNoteTypeId":"d6b573ba-2fe2-407a-b803-7387d0393c45","note":"$fcn","staffOnly":false},];
$notesJSON =~ s/,$//;
$subjectsJSON .= qq/"subjects":[/;
@subjects = split(/--/,$fcn);
foreach $thisSubject (@subjects) {
	$subjectsJSON .= qq["$thisSubject",];
}
$subjectsJSON =~ s/,$//;
$subjectsJSON .= "],";

$label = $query->param('label');
$year = $query->param('year');

$publicationJSON = qq["publication":] . "[" . qq[{"publisher":"$label","place":"","dateOfPublication":"$year","role":null}] . "],";

$dataA = qq[{
	$contributorsJSON
	"indexTitle":"$indexTitle",
	"instanceFormatIds":["727c025b-7818-47a3-a957-eddb476d1c8f"],
	"instanceTypeId":"8e32450a-982b-41bd-a055-20dbd63e06aa",
	"notes":[$notesJSON],
	$publicationJSON
	"source":"discogs",
	$subjectsJSON
	"title":"$title"
}];

$dataA =~ s/'/'\\''/g;

$postInstance = `curl -s -w '\n' -X POST $headers -d '$dataA' $baseURL/instance-storage/instances`;
$p .= "postInstance = $postInstance\n";
$hash = decode_json $postInstance;
for ($hash) {$instanceId = $_->{'id'};}
$dataB = qq[{"instanceId":"$instanceId","permanentLocationId":"c0a2b694-0fd2-4b10-8bda-94a2bf3bfa2d"}];
$dataB =~ s/'/'\\''/g;
$postHoldings = `curl -s -w '\n' -X POST $headers -d '$dataB' $baseURL/holdings-storage/holdings`;
$p .= "postHoldings = $postHoldings\n";
$hash = decode_json $postHoldings;
$holdingsRecordId = $hash->{'id'};
$dataC = qq[{"holdingsRecordId":"$holdingsRecordId","materialTypeId":"7b0b1c12-73d5-42f6-a8de-6d93d151bcc5","permanentLoanTypeId":"84ff8075-36d1-4156-82f3-3485ce8affab","status":{"name":"Available"}}];
$dataC =~ s/'/'\\''/g;
$postItem = `curl -s -w '\n' -X POST $headers -d '$dataC' $baseURL/item-storage/items`;
$p .= "postItem = $postItem\n";


$html = qq[
  <html>
  <head>
  <link rel="shortcut icon" type="image/png" href="https://folio.drew.edu/busybee.png">
  <style type="text/css">
  body {font-family: verdana, arial, sans-serif;}
  a:active {text-decoration: none; color: #f3654e;}
  a:link {text-decoration: none; color: #f3654e;}
  a:visited {text-decoration: none; color: #f3654e;}
  a:hover {text-decoration: none; color: #000000; background: #f3654e;}
  a.rusure:active {text-decoration: none; color: #cc1242;}
  a.rusure:link {text-decoration: none; color: #cc1242;}
  a.rusure:visited {text-decoration: none; color: #cc1242;}
  a.rusure:hover {text-decoration: none; color: #ffffff; background: #cc1242;}
  .v12 {font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;color:#ffffff;}
  .v18 {font-size: 18px; font-family: Verdana, Arial, Helvetica, sans-serif;color:#ffffff;}
  .v24 {font-size: 24px; font-family: Verdana, Arial, Helvetica, sans-serif;color:#ffffff;}
  input[type=button], input[type=submit], input[type=reset] {
   background-color: #f3654e;
   border: none;
   color: white;
   padding: 7px 120px;
   text-decoration: none;
   margin: 4px 2px;
   cursor: pointer;
  }
  </style>
  <title>add 2 DoYo</title>
  </head>

  <body bgcolor="#000000"><table border="0" width="100%" height="100%">
  <tr><th></th><th class="v24">folio.drew.edu/discogs | add to the DoYo record collection</th><th></th></tr>
  <tr><th></th><th class="v24"> </th><th></th></tr>
  <tr><th></th><td class="v18">

  $title <br> $label <br> $year <br> has been added to the DoYo record collection
  <p>check your work in <a href="https://drew.locate.ebsco.com/instances/$instanceId" target="_locate">Locate</a> and then <a href="enterData.pl">add another LP</a> to the <a href="https://drew.locate.ebsco.com/search?facets=items.effectiveLocationId%5B%5D%3Dc0a2b694-0fd2-4b10-8bda-94a2bf3bfa2d%26sourceTypes%5B%5D%3DMiscellaneous&option=keyword" target="_locate">DoYo</a>

  </th><th></th></tr>
  <tr>
   <th align="left" valign="bottom"><img border="0" src="https://folio.drew.edu/busybee.png"></th>
   <th> </th><th align="right" valign="bottom"><img width="200" src="https://folio.drew.edu/faith.jpg"></th>
  </tr>
  </table>
  </body>
  </html>
];

print qq[Content-Type: text/html \n\n $html ];