#!/usr/bin/env php
<?php

/**
 * Bart Nagel <bjn@ecs.soton.ac.uk>
 */

require dirname(dirname(__FILE__)) . "/setup.php";

// sort out commandline options
require dirname(dirname(__FILE__)) . "/lib/clioptions/CliOptions.class.php";
$opts = new CliOptions();
$opts->add(null, "help");
$opts->add(null, "force", CliOption::TYPE_SWITCH, null, "Force regenerating the RDF");
$opts->add(null, "id", CliOption::TYPE_SWITCH, null, "Interpret argument as ID rather than path");
$opts->add("F", "format", CliOption::TYPE_VALUE, "RDFXML", "Output the given format (a string recognized by Arc2)");

function usage($code = 255) {
	global $opts;
	$stream = $code == 0 ? STDOUT : STDERR;
	fwrite($stream, "Usage: " . basename($_SERVER["SCRIPT_NAME"]) . " [options] <filepath>\n");
	fwrite($stream, "Output the RDF for the given audiofile (regenerating it first if necessary)\n");
	fwrite($stream, "Mind the permissions -- the web server may want to modify the RDF later.\n");
	fwrite($stream, $opts->listopts());
	exit($code);
}

try {
	$options = $opts->getopts();
} catch (CliOptionException $e) {
	fwrite(STDERR, $e->getMessage() . "\n\n");
	usage();
}

// help message if requested
if ($options["help"]) {
	usage(0);
}

// complain if not exactly one non-option argument
if (count($options["_"]) != 1) {
	fwrite(STDERR, "Expected exactly one non-option argument\n\n");
	usage();
}

$repo = new AFRepo();

if ($options["id"])
	$id = $options["_"][0];
else
	$id = $repo->filePathToId($options["_"][0]);

try {
	echo $repo->getRDF($id, $options["format"], $options["force"]);
} catch (Exception $e) {
	fwrite(STDERR, $e->getMessage() . "\n");
	exit(1);
}

exit;

?>
