#!/usr/bin/php -q
<?php
	/*******************************************************
		Convert axfiles which have been generated 
		between the release of b9 and b10
	*******************************************************/

	$ariadne="../../lib";
	require_once($ariadne."/configs/ariadne.phtml");
	require_once($ariadne."/configs/axstore.phtml");
	require_once($ariadne."/modules/mod_filestore.phtml");
	require_once("shared_functions.php");

	function convert_data(&$object) {
		$id=$object["id"];
		$data=&$object["data"];
		$templates=$data->templates;
		while (is_array($templates) && (list($type, $functions)=each($templates))) {
			while (is_array($functions) && (list($function, $languages)=each($functions))) {
				while (is_array($languages) && (list($language, $file)=each($languages))) {
					echo "templates[$type][$function][$language]=".$id."\n";
					$data->templates[$type][$function][$language]=$id;
				}
			}
		}

		$pinp=$data->pinp;
		while (is_array($pinp) && (list($type, $functions)=each($pinp))) {
			while (is_array($functions) && (list($function, $languages)=each($functions))) {
				while (is_array($languages) && (list($language, $file)=each($languages))) {
					echo "pinp[$type][$function][$language]=".$id."\n";
					$data->pinp[$type][$function][$language]=$id;
				}
			}
		}
	}


	function read_data($fp) {
		global $error;
		if ($fp) {
			$rline=fgets($fp, 3000);
			while (strlen($rline)==3000-1) {
				$line.=trim($rline);
				$rline=fgets($fp, 3000);
			}
			$line.=trim($rline);
			if ($line) {
				$eline=explode("&", $line);
				if (is_array($eline) && sizeof($eline)==7) {
					$data["path"]=$eline[0];
					$data["id"]=$eline[1];
					$data["type"]=$eline[2];
					$data["vtype"]=$eline[3];
					$data["lastchanged"]=$eline[4];
					$data["properties"]=unserialize(base64_decode($eline[5]));
					$data["data"]=unserialize(base64_decode($eline[6]));
				} else {
					$error="corrupted data in objects.data (".sizeof($line)." elements)";
					print_r($line);
				}
			}
		}
		return $data;
	}


	if ($argc>1) {
		$ax=$argv[1];
		if ($argc>2) {
			$new_ax=$argv[2];
		} else {
			$new_ax=$ax;
		}

		if (file_exists($ax)) {
			$tempdir=tempnam($ax_config["temp"], "convert");
			@unlink($tempdir);
			mkdir($tempdir, 0755);

			if (untar($ax, "", $tempdir)) {
				$fp_objects=fopen($tempdir."/new_objects.data", "w");
				if (!$fp_objects) {
					$error="could not create temporary objects.data file";
				} else {
					$fp_nodes=fopen($tempdir."/nodes.data", "w");
					if (!$fp_nodes) {
						$error="could not create temporary nodes.data file";
						fclose($fp_objects);
					}
				}
				if (!$error) {
					$fp_source=fopen($tempdir."/objects.data", "r");
					if ($fp_source) {
						if ($header=read_data($fp_source)) {
							while ($data=read_data($fp_source)) {
								if (!isset($base_path)) {
									$base_path=substr($data["path"], 0, -1);
								}
								$data["path"]=substr($data["path"], strlen($base_path));
								$path=$data["path"];
								$nodes[$path]=new object;
								$nodes[$path]->path=$path;
								$nodes[$path]->objectid=$data["id"];

								$node_line=$path;
								$node_line.="&".$data["id"];
								$node_line.="&0&0";
								$node_line.="\n";
								fwrite($fp_nodes, $node_line);

								if (!$objects[$data["id"]]) {
									convert_data($data);
									$objects[$data["id"]]=true;
									$object_line=$data["id"];
									$object_line.="&".$data["type"];
									$object_line.="&".$data["vtype"];
									$object_line.="&".$data["lastchanged"];
									$object_line.="&0";
									$object_line.="&".base64_encode(serialize($data["data"]));
									$object_line.="&".base64_encode(serialize($data["properties"]));
									$object_line.="\n";
									fwrite($fp_objects, $object_line);
								}

							}
							fclose($fp_objects);
							fclose($fp_nodes);
							@unlink("$tempdir/objects.data");
							rename("$tempdir/new_objects.data", "$tempdir/objects.data");
						}
						fclose($fp_source);

						// doing the filestore stuff
						// files
						$tgz=$tempdir."/files_".substr($base_path,1).".tgz";
						if (file_exists($tgz)) {
							$files="$tempdir/old_files";
							mkdir($files, 0755);
							if (untar($tgz, "", $files)) {
								unset($objects);
								ksort($nodes); // sort nodes array
								$fstore=new filestore("files", $tempdir."/");
								while (list($path, $node)=each($nodes)) {
									if (!$objects[$node->objectid]) {
										$objects[$node->objectid]=true;
										$file_path=$tempdir."/old_files".$path;
										if (file_exists($file_path)) {
											$dir=dir($file_path);
											while ($file=$dir->read()) {
												if (is_file($file_path.$file)) {
													switch ($file) {
														case 'view=':
															$target="file";
														break;
														case 'view.thumb=':
															$target="file.thumb";
														break;
														default:
															$target=$file;
													}
													$fstore->copy_to_store($file_path.$file, $node->objectid, $target);
												}
											}
											$dir->close();
										}
									}
								}
								$fstore->close();
								rm_dir($tempdir."/old_files/");
								unlink($tgz);
								// now make a tgz of it
								tar($tempdir."/files.tgz", "files", $tempdir);
								rm_dir($tempdir."/files/");
							}
						}

						// doing the filestore stuff
						// templates
						$tgz=$tempdir."/templates_".substr($base_path,1).".tgz";
						if (!$error && file_exists($tgz)) {
							$templates="$tempdir/old_templates";
							mkdir($templates, 0755);
							if (untar($tgz, "", $templates)) {
								unset($objects);
								ksort($nodes); // sort nodes array
								@reset($nodes); // reset it

								$fstore=new filestore("templates", $tempdir."/");
								while (list($path, $node)=each($nodes)) {
									if (!$objects[$node->objectid]) {
										$objects[$node->objectid]=true;
										$file_path=$tempdir."/old_templates".$path;
										$file_path.="class=/";
										if (file_exists($file_path)) {
											$dir=dir($file_path);
											while ($dir_entry=$dir->read()) {
												if (is_dir($file_path.$dir_entry) && ($dir_entry!="." && $dir_entry!="..")) {
													$type=$dir_entry;
													$dir_entry.="/";
													$dir_type=dir($file_path.$dir_entry);
													while ($file=$dir_type->read()) {
														if (is_file($file_path.$dir_entry.$file)) {
															$fstore->copy_to_store($file_path.$dir_entry.$file, $node->objectid, "$type"."."."$file");
														}
													}
												}
											}
											$dir->close();
										}
									}
								}
								$fstore->close();
								rm_dir($tempdir."/old_templates/");
								unlink($tgz);
								// now make a tgz of it
								tar($tempdir."/templates.tgz", "templates", $tempdir);
								rm_dir($tempdir."/templates/");
							}
						}

					} else {
						$error="can not open objects.data";
					}

					tar($new_ax, " . ", $tempdir);
					rm_dir($tempdir);
				}
			}
		} else {
			$error="($ax) does not exist";
		}
		if ($error) {
			echo $error."\n";
		}
	} else {
		echo "USAGE: convert_cvs  ax_file  [new ax_file]\n";
	}
?>