<?php

// ----------------------------------------------------------------------
// Admin data download/uncompress
// ----------------------------------------------------------------------
$answer = promptYesNo("\nUpdating administrative dataset. Existing data will be deleted, continue?", true);
if (!$answer) {
    echo "Skipping administrative.\n";
    return;
}
$adminSql = configure('ADMIN_SQL', $defaultScriptDir . DIRECTORY_SEPARATOR . 'admin.sql', "Admin regions schema script");
$dbInstaller->runScript($adminSql);
echo "Success!!\n";
// download admin region data
echo "\nDownloading and loading admin region data:\n";
$url = configure('GLOBAL_ADMIN_URL', 'ftp://hazards.cr.usgs.gov/web/hazdev-geoserve-ws/admin/', "Admin download url");
$filenames = array('globaladmin.zip');
$download_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
// create temp directory
mkdir($download_path);
foreach ($filenames as $filename) {
    $downloaded_file = $download_path . $filename;
    downloadURL($url . $filename, $downloaded_file);
    // uncompress admin data
    if (pathinfo($downloaded_file)['extension'] === 'zip') {
        print 'Extracting ' . $downloaded_file . "\n";
        extractZip($downloaded_file, $download_path);
    }
}
// ----------------------------------------------------------------------
// Admin data load
<?php

// ----------------------------------------------------------------------
// Tectonic Summary data download/uncompress
// ----------------------------------------------------------------------
$answer = promptYesNo("\nUpdating Tectonic Summary dataset. Existing data will be deleted, " . " continue?", true);
if (!$answer) {
    echo "Skipping tectonic summary.\n";
    return;
}
$tectonicSql = configure('TECTONIC_SQL', $defaultScriptDir . DIRECTORY_SEPARATOR . 'tectonicsummary.sql', "Tectonic summary schema script");
$dbInstaller->runScript($tectonicSql);
echo "Success!!\n";
// download tectonic summary data
echo "\nDownloading and loading tectonic summary data:\n";
$url = configure('GLOBAL_TECTONIC_SUMMARY_URL', 'ftp://hazards.cr.usgs.gov/web/hazdev-geoserve-ws/tectonic/', "Tectonic summary download url");
$filenames = array('tectonicsummary_nc.dat', 'tectonicsummary_neic.dat', 'tectonicsummary_ut.dat');
$download_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tectonic_summary' . DIRECTORY_SEPARATOR;
// create temp directory
mkdir($download_path);
foreach ($filenames as $filename) {
    $downloaded_file = $download_path . $filename;
    downloadURL($url . $filename, $downloaded_file);
    // uncompress tectonic summary data
    if (pathinfo($downloaded_file)['extension'] === 'zip') {
        print 'Extracting ' . $downloaded_file . "\n";
        extractZip($downloaded_file, $download_path);
    }
}
// ----------------------------------------------------------------------
// Tectonic Summary data load
    define('NON_INTERACTIVE', false);
}
// ----------------------------------------------------------------------
// MAIN
//
// Run the interactive configuration and write configuration files to
// to file system (httpd.conf and config.ini).
//
// Edit this section if this application requires additional installation
// steps such as setting up a database schema etc... When editing this
// section, note the helpful install-funcs.inc.php functions that are
// available to you.
// ----------------------------------------------------------------------
include_once 'configure.php';
// output apache configuration
file_put_contents($APACHE_CONFIG_FILE, '
  # auto generated by ' . __FILE__ . ' at ' . date('r') . '
  Alias ' . $CONFIG['MOUNT_PATH'] . '/data ' . $CONFIG['DATA_DIR'] . '
  Alias ' . $CONFIG['MOUNT_PATH'] . ' ' . $CONFIG['APP_DIR'] . '/htdocs

  RewriteEngine On
  RewriteRule /hazws/staticcurve/1(.*)$ ' . $CONFIG['MOUNT_PATH'] . '/curve.ws.php?rewrite=$1 [L,PT]

  <Location ' . $CONFIG['MOUNT_PATH'] . '>
    Order Allow,Deny
    Allow from all
  </Location>
');
if (promptYesNo('Would you like to perform database installation', false)) {
    include_once 'db/setup.php';
}
$HELP_TEXT = array('MOUNT_PATH' => 'Url path to application', 'GEOSERVE_VERSION' => 'Webservice api version', 'DB_DSN' => 'Database connection DSN string', 'DB_SCHEMA' => 'Schema name for database installation', 'DB_USER' => 'Read/write username for database connections', 'DB_PASS' => 'Password for database user');
// Interactively prompts user for config. Writes CONFIG_FILE_INI
include_once 'configure.inc.php';
// Parse the configuration
$CONFIG = parse_ini_file($CONFIG_FILE_INI);
// Write the HTTPD configuration file
file_put_contents($HTTPD_CONF, '
  ## autogenerated at ' . date('r') . '

  Alias ' . $CONFIG['MOUNT_PATH'] . ' ' . $HTDOCS_DIR . '

  RewriteEngine on
  RewriteRule ^' . $CONFIG['MOUNT_PATH'] . '/(services|places|regions|layers)\\.(json)$ ' . $CONFIG['MOUNT_PATH'] . '/$1.php?format=$2 [L,PT,QSA]

  <Location ' . $CONFIG['MOUNT_PATH'] . '>
    Order allow,deny
    Allow from all

    <LimitExcept GET>
      deny from all
    </LimitExcept>

    ExpiresActive on
    ExpiresDefault "access plus 1 days"
  </Location>
');
// configure database
echo "\n";
if (promptYesNo('Would you like to setup the database or load data', true)) {
    include_once 'setup_database.php';
}
        $db->exec('GRANT SELECT ON ALL TABLES IN SCHEMA ' . $CONFIG['DB_SCHEMA'] . ' TO ' . $CONFIG['DB_USER']);
        $db->commit();
        echo 'Finished creating tables/views.' . PHP_EOL;
    }
    if (promptYesNo('Do you want to load metadata?', true)) {
        $db->beginTransaction();
        foreach ($imts as $imt) {
            $imtFactory->set($imt);
        }
        foreach ($vs30s as $vs30) {
            $vs30Factory->set($vs30);
        }
        foreach ($editions as $edition) {
            $editionFactory->set($edition);
        }
        foreach ($regions as $region) {
            $regionFactory->set($region);
        }
        $db->commit();
        echo 'Finished loading metadata.' . PHP_EOL;
    }
    if (promptYesNo('Do you want to load static curve data?', true)) {
        include_once './load.php';
    }
} catch (Exception $e) {
    if ($db->inTransaction()) {
        $db->rollBack();
    }
    print $e->getMessage();
}
chdir($oldDir);
}
// ----------------------------------------------------------------------
// Create User
// ----------------------------------------------------------------------
if (!$dbInstaller->userExists($CONFIG['DB_USER'])) {
    $answer = promptYesNo("Readonly user does not yet exist. Create it now?", true);
    if ($answer) {
        $dbInstaller->createUser(array('SELECT'), $CONFIG['DB_USER'], $CONFIG['DB_PASS']);
    }
}
// ----------------------------------------------------------------------
// Create Schema
// ----------------------------------------------------------------------
if (isset($CONFIG['DB_SCHEMA']) && $CONFIG['DB_SCHEMA'] !== '') {
    if (!$dbInstaller->schemaExists($CONFIG['DB_SCHEMA'])) {
        $answer = promptYesNo("Schema does not yet exist. Create it now?", true);
        if ($answer) {
            $dbInstaller->createSchema($CONFIG['DB_SCHEMA']);
        }
    }
    $dbInstaller->run('SET search_path = ' . $CONFIG['DB_SCHEMA'] . ', public');
}
// ----------------------------------------------------------------------
// Data downloads and database loads
// ----------------------------------------------------------------------
include_once 'load_geonames.php';
include_once 'load_fe.php';
include_once 'load_admin.php';
include_once 'load_authoritative.php';
include_once 'load_neic.php';
include_once 'load_tectonicsummary.php';
<?php

// ----------------------------------------------------------------------
// Geonames data download/uncompress
// ----------------------------------------------------------------------
// TODO:: prompt user to download geoname data (cities1000.zip, US.zip)
$answer = promptYesNo("\nUpdating geonames dataset. Existing data will be deleted, continue?", true);
if (!$answer) {
    echo "Skipping geonames.\n";
    return;
}
$geonamesSql = configure('GEONAMES_SQL', $defaultScriptDir . DIRECTORY_SEPARATOR . 'geonames.sql', "Geonames schema script");
$dbInstaller->runScript($geonamesSql);
echo "Success!!\n";
// download geoname data
echo "\nDownloading and loading geonames data:\n";
$url = configure('GEONAMES_URL', 'ftp://hazards.cr.usgs.gov/web/hazdev-geoserve-ws/geonames/', "Geonames download url");
$filenames = array('cities1000.zip', 'US.zip', 'admin1CodesASCII.txt', 'countryInfo.txt');
$download_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'geonames' . DIRECTORY_SEPARATOR;
// create temp directory
mkdir($download_path);
foreach ($filenames as $filename) {
    $downloaded_file = $download_path . $filename;
    downloadURL($url . $filename, $downloaded_file);
    // uncompress geonames data
    if (pathinfo($downloaded_file)['extension'] === 'zip') {
        print 'Extracting ' . $downloaded_file . "\n";
        extractZip($downloaded_file, $download_path);
    }
}
// ----------------------------------------------------------------------
<?php

// ----------------------------------------------------------------------
// Timezones data download
// ----------------------------------------------------------------------
$answer = promptYesNo("\nUpdating timezone dataset. Existing data will be deleted, continue?", true);
if (!$answer) {
    echo "Skipping timezone.\n";
    return;
}
$timezoneSql = configure('TIMEZONE_SQL', $defaultScriptDir . DIRECTORY_SEPARATOR . 'timezone.sql', "Timezone regions schema script");
$dbInstaller->runScript($timezoneSql);
echo "Success!!\n";
// download timezone region data
echo "\nDownloading and loading timezone region data:\n";
$url = configure('GLOBAL_ADMIN_URL', 'ftp://hazards.cr.usgs.gov/web/hazdev-geoserve-ws/timezones/', "Timezone download url");
$filenames = array('timezones.dat');
$download_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'timezone' . DIRECTORY_SEPARATOR;
// create temp directory
mkdir($download_path);
foreach ($filenames as $filename) {
    $downloaded_file = $download_path . $filename;
    downloadURL($url . $filename, $downloaded_file);
}
// ----------------------------------------------------------------------
// timezone data load
// ----------------------------------------------------------------------
// TIMEZONE
echo "\nLoading timezone data ... ";
$dbInstaller->copyFrom($download_path . 'timezones.dat', 'timezone', array('NULL AS \'\'', 'CSV', 'HEADER'));
echo "SUCCESS!!\n";
if (!is_dir($CONF_DIR)) {
    mkdir($CONF_DIR, 0755, true);
}
// Interactively prompts user for config. Writes CONFIG_FILE_INI
include_once 'configure.inc.php';
// Parse the configuration
$CONFIG = parse_ini_file($CONFIG_FILE_INI);
// Write the HTTPD configuration file
file_put_contents($HTTPD_CONF, '
  ## autogenerated at ' . date('r') . '

  Alias ' . $CONFIG['MOUNT_PATH'] . ' ' . $HTDOCS_DIR . '

  <Location ' . $CONFIG['MOUNT_PATH'] . '>
    Order allow,deny
    Allow from all

    <LimitExcept GET>
      deny from all
    </LimitExcept>

    ExpiresActive on
    ExpiresDefault "access plus 1 days"
  </Location>
');
include_once './functions.inc.php';
if (!file_exists($CONFIG['PDL_JAR_FILE'])) {
    if (promptYesNo('Download PDL Jar File?')) {
        downloadURL('http://ehppdl1.cr.usgs.gov/ProductClient/ProductClient.jar', $CONFIG['PDL_JAR_FILE']);
    }
}
<?php

// ----------------------------------------------------------------------
// Offshore polygon data download/uncompress
// ----------------------------------------------------------------------
$answer = promptYesNo("\nUpdating offshore region dataset. Existing data will be deleted, continue?", true);
if (!$answer) {
    echo "Skipping offshore regions.\n";
    return;
}
$offshoreSql = configure('ADMIN_SQL', $defaultScriptDir . DIRECTORY_SEPARATOR . 'offshore.sql', "offshore regions schema script");
$dbInstaller->runScript($offshoreSql);
echo "Success!!\n";
// download admin region data
echo "\nDownloading and loading offshore region data:\n";
$url = configure('OFFSHORE_URL', 'ftp://hazards.cr.usgs.gov/web/hazdev-geoserve-ws/offshore/', "Offshore download url");
$filenames = array('feoffshore.dat');
$download_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'fe' . DIRECTORY_SEPARATOR;
// create temp directory
mkdir($download_path);
foreach ($filenames as $filename) {
    $downloaded_file = $download_path . $filename;
    downloadURL($url . $filename, $downloaded_file);
    // uncompress offshore data
    if (pathinfo($downloaded_file)['extension'] === 'zip') {
        print 'Extracting ' . $downloaded_file . "\n";
        extractZip($downloaded_file, $download_path);
    }
}
// ----------------------------------------------------------------------
// Offshore data load
<?php

// ----------------------------------------------------------------------
// FE/FE Renames data download/uncompress
// ----------------------------------------------------------------------
$answer = promptYesNo("\nUpdating FE (and renames) dataset. Existing data will be deleted, " . 'continue?', true);
if (!$answer) {
    echo "Skipping FE.\n";
    return;
}
$feSql = configure('FE_SQL', $defaultScriptDir . DIRECTORY_SEPARATOR . 'fe.sql', "FE regions schema script");
$dbInstaller->runScript($feSql);
echo "Success!!\n";
// download FE data
echo "\nDownloading and loading FE data:\n";
$url = configure('FE_URL', 'ftp://hazards.cr.usgs.gov/web/hazdev-geoserve-ws/FE/', "FE download url");
$filenames = array('fe.dat', 'ferenames.dat');
$download_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FE' . DIRECTORY_SEPARATOR;
// create temp directory
mkdir($download_path);
foreach ($filenames as $filename) {
    $downloaded_file = $download_path . $filename;
    downloadURL($url . $filename, $downloaded_file);
    // uncompress FE data
    if (pathinfo($downloaded_file)['extension'] === 'zip') {
        print 'Extracting ' . $downloaded_file . "\n";
        extractZip($downloaded_file, $download_path);
    }
}
// ----------------------------------------------------------------------
// FE data load