function env($name)
{
    if (isset($_ENV[$name])) {
        return $_ENV[$name];
    }
    if (isset($_SERVER[$name])) {
        return $_SERVER[$name];
    }
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
    appdeck_error("missing {$name} env var from xcode", $backtrace[0]['file'], $backtrace[0]['line']);
    exit(1);
}
// set beacon if needed
// this beacon is used to clear cache on startup after a clean build
$beacon_output_file_path = $output_dir_path . "/beacon";
if (defined('FORCE_DOWNLOAD') && FORCE_DOWNLOAD === true && file_exists($beacon_output_file_path)) {
    unlink($beacon_output_file_path);
}
if (!file_exists($beacon_output_file_path)) {
    file_put_contents($beacon_output_file_path, mt_rand());
}
list($json_headers, $json_data) = ezcurl($app_json_url, $error);
if ($json_data == false) {
    appdeck_error("failed to download: {$app_json_url}: {$error}", $app_plist_path);
}
$info = json_decode(json_minify($json_data));
if ($info == false) {
    appdeck_error("{$app_json_url}: invalid json data: {$json_data}");
}
$base_url = isset($info->base_url) ? $info->base_url : $app_json_url;
if (!isset($info->embed_url) && !isset($info->embed)) {
    appdeck_warning("no embed ressources defined in app.json");
}
// by default we always embed app.json
appdeck_add_ressource($app_json_url, $json_data, true);
// try to add files from embed url
if (isset($info->embed_url)) {
    $embed_url = resolve_url($info->embed_url, $base_url);
    embed_url($embed_url);
}
if (isset($info->embed_url_tablet)) {
    $embed_url = resolve_url($info->embed_url_tablet, $base_url);
    embed_url($embed_url);
define('FORCE_DOWNLOAD', false);
define('APPDECK_APPDATA_URL', 'http://appdata.static.appdeck.mobi/res/android/');
define('EMBED_SUFFIX', '.png');
require 'embedresources.lib.php';
$project_path = realpath('.');
$app_manifest = openxml($project_path . '/app/src/main/AndroidManifest.xml');
$app_json_url = $app_manifest->xpath("//meta-data[@android:name='AppDeckJSONURL']/@android:value");
/*
$app_package = $app_manifest->xpath("//manifest/@package");
$app_package = $app_package[0];

var_dump(shell_exec("/Applications/Android/sdk/platform-tools/adb shell \"pm uninstall {$app_package}\""));
var_dump(shell_exec("/Applications/Android/sdk/platform-tools/adb shell \"rm -rf /data/app/{$app_package}-*\""));
*/
if ($app_json_url == false) {
    appdeck_error('missing AppDeckJSONURL entry in AndroidManifest.xml (add <meta-data android:name="AppDeckJSONURL" android:value="..."></meta-data> in root of your AndroidManifest.xml file)', $app_plist_path);
}
$app_json_url = (string) $app_json_url[0]['value'];
define('APPDECK_JSON_URL', $app_json_url);
define('APPDECK_OUTPUT_DIR', $project_path . '/app/src/main/assets/httpcache/');
if (isset($argv[1]) && $argv[1] == 'clean') {
    print "Clean downloaded assets ...";
    $count = 0;
    foreach (glob(APPDECK_OUTPUT_DIR . '/*') as $file) {
        unlink($file);
        $count++;
    }
    print "{$count} files deleted\r\n";
    exit;
}
include 'embedresources.php';
#!/usr/bin/php
<?php 
define('EMBED_TYPE', 'ios7');
define('FORCE_DOWNLOAD', false);
define('APPDECK_APPDATA_URL', 'http://appdata.static.appdeck.mobi/res/iphone/');
require 'embedresources.lib.php';
// Init dir
$output_dir = env('CONFIGURATION_BUILD_DIR') . '/' . env('UNLOCALIZED_RESOURCES_FOLDER_PATH') . '/embedresource/';
// AppDeck - Embed resources script
$app_plist_path = env('SRCROOT') . '/' . env('INFOPLIST_FILE');
$app_plist = openxml($app_plist_path);
$app_api_key = plistget($app_plist, 'AppDeckApiKey');
if ($app_api_key != false) {
    $app_api_key = preg_replace('/#.*/', '', $app_api_key);
}
$app_json_url = plistget($app_plist, 'AppDeckJSONURL');
if ($app_api_key == false && $app_json_url == false) {
    appdeck_error("missing AppDeckApiKey entry", $app_plist_path);
}
if ($app_json_url == false) {
    $app_json_url = 'http://config.appdeck.mobi/json/' . $app_api_key;
}
define('APPDECK_API_KEY', $app_json_url);
define('APPDECK_JSON_URL', $app_json_url);
define('APPDECK_OUTPUT_DIR', $output_dir);
require 'embedresources.php';