Install Error
			Shimmie needs to be run via a web server with PHP support -- you
			appear to be either opening the file from your hard disk, or your
			web server is mis-configured.
			
If you've installed a web server on your desktop PC, you probably
			want to visit the local web server.
			
For more help with installation, visit
			the
			documentation wiki.
		
 
		
			
Error\nPHP's GD extension seems to be missing, ".
		      "and imagemagick's \"convert\" command cannot be found - ".
			  "no thumbnailing engines are available.";
	}
	else {
		$gd = "";
	}
	print <<
			Shimmie Installer
			$gd
			Install
			
			Help
					
			Databases should be specified like so:
			
ie: protocol://username:password@host/database?options
			
eg: mysql://shimmie:pw123@localhost/shimmie?persist
			
			
For more help with installation, visit
			the
			documentation wiki.
		
 
EOD;
} // }}}
function install_process($database_dsn) { // {{{
	create_tables($database_dsn);
	insert_defaults($database_dsn);
	build_dirs();
	write_config($database_dsn);
	
	header("Location: index.php");
} // }}}
function create_tables($dsn) { // {{{
	$db = NewADOConnection($dsn);
	if(!$db) {
		die("Couldn't connect to \"$dsn\"");
	}
	else {
		if(substr($dsn, 0, 5) == "mysql") {
			$engine = new MySQL();
		}
		else if(substr($dsn, 0, 5) == "pgsql") {
			$engine = new PostgreSQL();
		}
		else if(substr($dsn, 0, 6) == "sqlite") {
			$engine = new SQLite();
		}
		else {
			die("Unknown database engine; Shimmie currently officially supports MySQL
			(mysql://), with hacks for Postgres (pgsql://) and SQLite (sqlite://)");
		}
		$engine->init($db);
		$db->execute($engine->create_table_sql("aliases", "
			oldtag VARCHAR(128) NOT NULL PRIMARY KEY,
			newtag VARCHAR(128) NOT NULL,
			INDEX(newtag)
		"));
		$db->execute($engine->create_table_sql("config", "
			name VARCHAR(128) NOT NULL PRIMARY KEY,
			value TEXT
		"));
		$db->execute($engine->create_table_sql("users", "
			id SCORE_AIPK,
			name VARCHAR(32) UNIQUE NOT NULL,
			pass CHAR(32),
			joindate DATETIME NOT NULL DEFAULT SCORE_NOW,
			admin SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
			email VARCHAR(128)
		"));
		$db->execute($engine->create_table_sql("images", "
			id SCORE_AIPK,
			owner_id INTEGER NOT NULL,
			owner_ip SCORE_INET NOT NULL,
			filename VARCHAR(64) NOT NULL,
			filesize INTEGER NOT NULL,
			hash CHAR(32) UNIQUE NOT NULL,
			ext CHAR(4) NOT NULL,
			source VARCHAR(255),
			width INTEGER NOT NULL,
			height INTEGER NOT NULL,
			posted TIMESTAMP NOT NULL DEFAULT SCORE_NOW,
			locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
			INDEX(owner_id),
			INDEX(width),
			INDEX(height),
			FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE
		"));
		$db->execute($engine->create_table_sql("tags", "
			id SCORE_AIPK,
			tag VARCHAR(64) UNIQUE NOT NULL,
			count INTEGER NOT NULL DEFAULT 0
		"));
		$db->execute($engine->create_table_sql("image_tags", "
			image_id INTEGER NOT NULL,
			tag_id INTEGER NOT NULL,
			INDEX(image_id),
			INDEX(tag_id),
			UNIQUE(image_id, tag_id),
			FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
			FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
		"));
		$db->execute("INSERT INTO config(name, value) VALUES('db_version', 7)");
	}
	$db->Close();
} // }}}
function insert_defaults($dsn) { // {{{
	$db = NewADOConnection($dsn);
	if(!$db) {
		die("Couldn't connect to \"$dsn\"");
	}
	else {
		$config_insert = $db->Prepare("INSERT INTO config(name, value) VALUES(?, ?)");
		$user_insert = $db->Prepare("INSERT INTO users(name, pass, joindate, admin) VALUES(?, ?, now(), ?)");
		$db->Execute($user_insert, Array('Anonymous', null, 'N'));
		$db->Execute($config_insert, Array('anon_id', $db->Insert_ID()));
		if(check_im_version() > 0) {
			$db->Execute($config_insert, Array('thumb_engine', 'convert'));
		}
		$db->Close();
	}
} // }}}
function build_dirs() { // {{{
	if(!file_exists("images")) @mkdir("images"); // *try* and make default dirs. Ignore any errors -- 
	if(!file_exists("thumbs")) @mkdir("thumbs"); // if something is amiss, we'll tell the user later
	if(!file_exists("data")) @mkdir("data");
	if(
			((!file_exists("images") || !file_exists("thumbs") || !file_exists("data")) && !is_writable("./")) ||
			(!is_writable("images") || !is_writable("thumbs") || !is_writable("data"))
	) {
		print "Shimmie needs three folders in it's directory, 'images', 'thumbs', and 'data',
		       and they need to be writable by the PHP user (if you see this error,
			   if probably means the folders are owned by you, and they need to be
			   writable by the web server).
			   
			   Once you have created these folders, hit 'refresh' to continue.";
		exit;
	}
	else {
		assert(file_exists("images") && is_writable("images"));
		assert(file_exists("thumbs") && is_writable("thumbs"));
		assert(file_exists("data") && is_writable("data"));
		if(!file_exists("images/ff")) {
			for($i=0; $i<256; $i++) {
				mkdir(sprintf("images/%02x", $i));
				mkdir(sprintf("thumbs/%02x", $i));
			}
		}
	}
} // }}}
function write_config($dsn) { // {{{
	$file_content = "";
	
	if(is_writable("./") && file_put_contents("config.php", $file_content)) {
		assert(file_exists("config.php"));
	}
	else {
		$h_file_content = htmlentities($file_content);
		print <<
						
		One done, Continue
EOD;
		exit;
	}
} // }}}
?>