#!/usr/bin/php
<?php
	chdir(dirname($argv[0]));
	require("../libraries/configuration.php");

	$db = new MySQLi_connection(DB_HOSTNAME, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
	if ($db->connected == false) {
		exit("Internal error: database not available.\n");
	}

	if (($argc = count($argv)) <= 1) {
		/* List available websites
		 */
		$query = "select * from hostnames order by hostname";
		if (($hostnames = $db->execute($query)) === false) {
			print "Error while retrieving hostnames.\n";
			return;
		}

		foreach ($hostnames as $hostname) {
			printf("- %s\n", $hostname["hostname"]);
		}

		return;
	}

	/* Remove website
	 */
	for ($i = 1; $i < $argc; $i++) {
		$hostname = $argv[$i];
		printf("Removing %s from database.\n", $hostname);

		$queries = array(
			array("delete from host_statistics where hostname_id in (select id from hostnames where hostname=%s)", $hostname),
			array("delete from cgi_statistics where hostname_id in (select id from hostnames where hostname=%s)", $hostname),
			array("delete from hostnames where hostname=%s", $hostname));

		$db->transaction($queries);
	}
?>
