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

require __DIR__.'/../vendor/autoload.php';

$aboutText =
	  "{$argv[0]}: Generate SQL to be run as the database administrator\n".
	  "to create the test database.";

$usageText = "Usage: {$argv[0]} <path-to-config-json>";

if( count($argv) < 2 ) {
	fwrite(STDERR, "{$argv[0]}: Error: No config JSON file provided.\n\n");
	fwrite(STDERR, $usageText."\n");
	exit(1);
} else if( $argv[1] == '-?' or $argv[1] == '-h' or $argv[1] == '--help' ) {
	echo
		$aboutText, "\n\n",
		$usageText, "\n";
	exit(0);
}

$dbcConfigFile = $argv[1];
if( ($dbcConfigJson = file_get_contents($dbcConfigFile)) === false ) {
	fwrite(STDERR, "{$argv[0]}: Error: Could not read specified config file, '$dbcConfigFile'\n");
	exit(1);
}

try {
	$dbcConfig = EarthIT_JSON::decode($dbcConfigJson);
} catch( EarthIT_JSON_JSONDecodeError $e ) {
	fwrite(STDERR, "{$argv[0]}: Error: Failed to parse config JSON from '$dbcConfigFile':\n".$e->getMessage()."\n");
	exit(1);
}

if( !isset($dbcConfig['port']) ) $dbcConfig['port'] = 5432;

$sePassword = escapeshellarg($dbcConfig['password']);
$seHost     = escapeshellarg($dbcConfig['host']);
$sePort     = escapeshellarg($dbcConfig['port']);
$seDatabase = escapeshellarg($dbcConfig['dbname']);
$seUser     = escapeshellarg($dbcConfig['user']);

echo "#!/bin/sh\n";
echo "\n";
echo "export PGPASSWORD={$sePassword}\n";
echo "exec psql {$seDatabase} -U {$seUser} -h {$seHost} -p {$sePort} \"\$@\"\n";
