#!/usr/bin/php -q
<?php
	$ariadne="../lib";
	require($ariadne."/configs/ariadne.phtml");
	require($ariadne."/configs/store.phtml");
	require($ariadne."/configs/axstore.phtml");
	require($ariadne."/includes/loader.web.php");
	require($ariadne."/stores/mysqlstore.phtml");
	include($ariadne."/stores/axstore.phtml");
	global $options;

	$arg_c=0;
	$arg_v[$arg_c++]=$argv[0];

	next($argv); // do not parse the filename
	while (list(,$cmd)=each($argv)) {
		if (strpos($cmd, "--") === 0) {
			if (strpos(substr($cmd, 2),"-")===false) {
				$option_switch=substr($cmd, 2);
			} else {

				$option_switch=substr($cmd, 2, strpos(substr($cmd, 2),"-"));

				if (strpos($cmd, "=")!==false) {
					$option_value=substr($cmd, strpos($cmd, "=")+1);
					$option=substr($cmd, strlen($option_switch)+3, strpos($cmd, "=") - (strlen($option_switch)+3));
				} else {
					$option=substr($cmd, strlen($option_switch)+3);
				}
			}

			//echo "switch($option_switch)($option)='$option_value'\n";

			switch ($option_switch) {
				case 'with':
					$options[$option]=($option_value) ? $option_value : true;
				break;
				case 'without':
					$options["without_$option"]=true;
				break;

				case 'verbose':
					$options["verbose"]=true;
				break;

				case 'tar_nofail':
					$ax_config["tar_nofail"]=true;
				break;

				case 'tar_fail':
					$ax_config["tar_nofail"]=false;
				break;

				default:
					echo "unknow option-switch ($option_switch)\n";
			}
		} else {
			$arg_v[$arg_c++]=$cmd;
		}
	}

        set_time_limit(0);

	if ($arg_c>=3) {
		$ARLogin="admin";
		$AR->user=new object;
		$AR->user->data=new object;
		$AR->user->data->login="admin";

		if ($arg_c>3) {
			$ax_config["database"]=$arg_v[3];
			$srcpath=$arg_v[1];
			$destpath=$arg_v[2];
		} else {
			$ax_config["database"]=$arg_v[2];
			$srcpath=$arg_v[1];
		}
		$ax_config["writeable"]=true;
		$importStore=new axstore("", $ax_config);
		$ARCurrent->importStore=&$importStore;
		if (!$importStore->error) {
			$store=new mysqlstore($root,$store_config);

			$args="srcpath=".rawurlencode($srcpath)."&destpath=".rawurlencode($destpath);
			//echo "args ($args)\n";
			$error=@current($store->call("system.export.phtml", $args,
				$store->get("/")));

			//echo "closing store\n";
			$importStore->close();

		} else {
			$error="ax error: ".$importStore->error;
		}

		if ($error) {
			echo $error."\n";
		}
	} else {
		echo "USAGE: export [export_path] [ax_file]\n";
	}
?>
