<?php

// THIS IS NOT MEANT TO BE RUN FROM A SERVER!
// THIS IS MEANT TO BE RUN FROM A SHELL SCRIPT
// PLACING THIS FILE ON A SERVER IS A SERIOUS SECURITY RISK

// App Name

define('SHORT_APP_NAME', 'countrylookup');
define('LONG_APP_NAME', 'Country Lookup');

// Leave blank if a folder doesn't exist
define('CONTENT', 'content');
define('LOCALE', 'locale');
define('SKIN', 'skin');

// Define branch/trunk:

define('BRANCH', $_SERVER['argv'][1]);

shell_exec('xcopy /E /I ' . BRANCH . '\\' . SHORT_APP_NAME . ' Releases\\' . SHORT_APP_NAME);

$file = file_get_contents('Releases\\' . SHORT_APP_NAME . '\\chrome.manifest');

$file = explode("\n", $file);

foreach($file as $index => $value){
	$file[$index] = explode(' ', $file[$index]);
	if($file[$index][0] == 'content'){
		$file[$index][2] = 'jar:chrome/' . SHORT_APP_NAME . '.jar!/' . $file[$index][2];
	} elseif($file[$index][0] == "locale" || $file[$index][0] == "skin"){
		$file[$index][3] = 'jar:chrome/' . SHORT_APP_NAME . '.jar!/' . $file[$index][3];
	}
	$file[$index] = implode(' ', $file[$index]);
}

$file = implode("\n", $file);

file_put_contents('Releases\\' . SHORT_APP_NAME . '\\chrome.manifest', $file);

shell_exec('xcopy /E /I Releases\\' . SHORT_APP_NAME . ' Releases\\' . SHORT_APP_NAME . '-mozillaAddons');

$file = file_get_contents('Releases\\' . SHORT_APP_NAME . '-mozillaAddons\\install.rdf');

$file = explode("\n", $file);

$applications = array();
$currentApp = null;

foreach($file as $index => $value){
	if(strpos($value, '<em:updateURL>http://' . SHORT_APP_NAME . '.fvds.frih.net/update.rdf</em:updateURL>') !== false){
		$file[$index] = "";
	}
	if(preg_match('/<em:version>([0-9\.A-Za-z]+)<\/em:version>/', $value, $matches) == 1){
		$version = $matches[1];
	}
	if(strpos($value, '<em:targetApplication>') !== false){
		$currentApp = array();
	}
	if(strpos($value, '</em:targetApplication>') !== false){
		$applications[] = $currentApp;
		$currentApp = null;
	}
	if(preg_match('/<em:id>(\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}|[^<@]+@[^<]+)<\/em:id>/', $value, $matches) == 1 && $currentApp !== null){
		$currentApp['id'] = $matches[1];
	}
	if(preg_match('/<em:minVersion>([0-9\.*+A-Za-z]+)<\/em:minVersion>/', $value, $matches) == 1 && $currentApp !== null){
		$currentApp['minVersion'] = $matches[1];
	}
	if(preg_match('/<em:maxVersion>([0-9\.*+A-Za-z]+)<\/em:maxVersion>/', $value, $matches) == 1 && $currentApp !== null){
		$currentApp['maxVersion'] = $matches[1];
	}
}

$file = implode("\n", $file);

file_put_contents('Releases\\' . SHORT_APP_NAME . '-mozillaAddons\\install.rdf', $file);

if(preg_match('/[A-Za-z]/', $version) === 1){
	$officialVersion = false;
} else {
  $officialVersion = true;
}

// Do some checks
$stop = false;
if(file_exists('Releases\\' . SHORT_APP_NAME . '-' . $version . '.xpi')){
  echo 'This file version already exists. Are you sure you want to overwrite it? Y/N: ';
  $response = substr(trim(fgets(STDIN)), 0, 1);
  if($response != 'Y' && $response != 'y'){
    $stop = true;
  } else {
    echo "File will be overwritten.\n";
  }
}

$file = file_get_contents('Releases\\' . SHORT_APP_NAME . '\\chrome.manifest');

$locales = opendir('Releases\\' . SHORT_APP_NAME . '\\locale\\');
while(!$stop && false !== ($currentlocale = readdir($locales))){
  if($currentlocale != '..' && $currentlocale != '.' && strpos($file, $currentlocale) === false){
    echo "Locale $currentlocale not found in chrome.manifest.\n";
    $stop = true;
  }
}
closedir($locales);

if($stop){
	shell_exec('cd Releases & rmdir /S /Q ' . SHORT_APP_NAME . ' & rmdir /S /Q ' . SHORT_APP_NAME . '-mozillaAddons');
  echo "Build process stopped.\n";
  exit;
}

shell_exec('  cd Releases\\' . SHORT_APP_NAME . ' & 7za a -tzip ' . SHORT_APP_NAME . '.jar ' . CONTENT . ' ' . LOCALE . ' ' . SKIN . ' & rmdir /S /Q ' . CONTENT . ' ' . LOCALE . ' ' . SKIN . ' & mkdir chrome & move ' . SHORT_APP_NAME . '.jar chrome\\');
shell_exec('  cd Releases\\' . SHORT_APP_NAME . '               & 7za a -tzip ..\\' . SHORT_APP_NAME . '-' . $version . '.xpi               * & cd .. & rmdir /S /Q ' . SHORT_APP_NAME . '');
if($officialVersion){
	shell_exec('cd Releases\\' . SHORT_APP_NAME . '-mozillaAddons & 7za a -tzip ..\\' . SHORT_APP_NAME . '-' . $version . '-mozillaAddons.xpi * & cd .. & rmdir /S /Q ' . SHORT_APP_NAME . '-mozillaAddons');
} else {
	shell_exec('cd Releases & rmdir /S /Q ' . SHORT_APP_NAME . '-mozillaAddons');
}

shell_exec('copy /Y Releases\\' . SHORT_APP_NAME . '-' . $version . '.xpi Web\\releases\\ ');

if($officialVersion){

$file = '<?xml version="1.0"?>
<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://www.mozilla.org/2004/em-rdf#">
	<!-- ' . LONG_APP_NAME . ' -->
	<RDF:Description about="urn:mozilla:extension:' . SHORT_APP_NAME . '@fvds.frih.net">
	  <updates>
		<RDF:Seq>
		  <RDF:li>
			<RDF:Description>
			  <version>' . $version . '</version>';

			  foreach($applications as $index => $value){
			  $file .= '
				<targetApplication>
					<RDF:Description>
						<id>' . $value['id'] . '</id>
						<minVersion>' . $value['minVersion'] . '</minVersion>
						<maxVersion>' . $value['maxVersion'] . '</maxVersion>
						<updateLink>http://' . SHORT_APP_NAME . '.fvds.frih.net/releases/' . SHORT_APP_NAME . '-' . $version . '.xpi</updateLink>
					</RDF:Description>
				</targetApplication>';
			  }

$file .= '
			</RDF:Description>
		  </RDF:li>
		</RDF:Seq>
	  </updates>
	  <version>' . $version . '</version>
	  <updateLink>http://' . SHORT_APP_NAME . '.fvds.frih.net/releases/' . SHORT_APP_NAME . '-' . $version . '.xpi</updateLink>
	</RDF:Description>
</RDF:RDF>';

	file_put_contents('Web\update.rdf', $file);

	$file = file_get_contents('Web\index.php');

	$file = explode("\n", $file);

	foreach($file as $index => $value){
		if(strpos($value, '<!-- This line is auto-generated by build.php -->') === 0){
			$file[$index] = '<!-- This line is auto-generated by build.php --><p><a href="releases/' . SHORT_APP_NAME . '-' . $version . '.xpi" title="The latest version of ' . LONG_APP_NAME . '">Click here to download ' . LONG_APP_NAME . ' v' . $version . '</a></p>';
		}
	}

	$file = implode("\n", $file);

	file_put_contents('Web\index.php', $file);

	echo "Remember to manually:\n\t- Upload index.php, update.rdf and the new release to the web\n\t- Submit the new release to addons.mozilla.org\n\t- Change the version number\n\n";
	
} else {
	echo "Remember to manually:\n\t- Upload the new release to the web\n\t- Change the version number\n\n";
}

?>