<?php
require_once('../env.php');
require_once(LIB.'common.inc.php');
include LIB.'db.inc.php';
include 'tables.inc.php';
$master_data = array('auths', 'user_types', 'user_auth', 'boxes', 'layouts', 'block_tables');
foreach ($tables as $table) {
	$t = $db->getTableName($table);
	$create_table = $db->getAll('show create table `'.$t.'`');
	if (empty($create_table))
		die($table. ' error');


	$raw = $create_table[0]['Create Table'];
	$sql = 'CREATE TABLE `%%%TABLE_NAME%%%`'.substr($raw, strlen($t)+strlen('CREATE TABLE ``'));
	if (in_array($table, $master_data, TRUE)) {
		$data = $db->select($table);
		file_put_contents($table.'.data.sql', serialize($data));

		file_put_contents($table.'.sql', $sql);
	} else {
		$p = strpos($sql, ' AUTO_INCREMENT=');
		if ($p !== FALSE) {
			$p2 = strpos($sql, ' ', $p+1);
			$sql = substr($sql, 0, $p).' AUTO_INCREMENT=1'.substr($sql,$p2);
		}
		file_put_contents($table.'.sql', $sql);
	}
}
echo 'finish';
?>