<?php

/**
 * Implements hook_install() using the name of the country as the base of
 * the function name.
 */
function brunei_darussalam_install() {
  // Make the entry in the country table.
  // VALUES = Country ID, Country Name, 2-digit Code, 3-digit Code, File Version
  db_insert('uc_countries')
    ->fields(array(
      'country_id' => 96,
      'country_name' => 'Brunei Darussalam',
      'country_iso_code_2' => 'BN',
      'country_iso_code_3' => 'BRN',
      'version' => 3,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(96, 'BN-BE', 'Belait'),
    array(96, 'BN-BM', 'Brunei-Muara'),
    array(96, 'BN-TE', 'Temburong'),
    array(96, 'BN-TU', 'Tutong'),
  );

  $query = db_insert('uc_zones')->fields(array('zone_country_id', 'zone_code', 'zone_name'));
  foreach ($zones as $zone) {
    $query->values($zone);
  }
  $query->execute();

  // Set address format
  uc_set_address_format(
    96,
    "!company\r\n" .
    "!first_name !last_name\r\n" .
    "!street1\r\n" .
    "!street2\r\n" .
    "!city !postal_code\r\n" .
    "!zone_name\r\n" .
    "!country_name_if"
  );
}

/**
 * Implements hook_update() with $version being the only argument.
 * Add a new case for each version update, and be sure to always include the
 * latest changes in the install function.
 */
function brunei_darussalam_update($version) {
  switch ($version) {
    case 2:
      // Correct the address format
      uc_set_address_format(
        96,
        "!company\r\n" .
        "!first_name !last_name\r\n" .
        "!street1\r\n" .
        "!street2\r\n" .
        "!city !postal_code\r\n" .
        "!zone_name\r\n" .
        "!country_name_if"
      );
      break;

    case 3:
      // Correct ISO-3166-1 country name
      db_update('uc_countries')
        ->fields(array('country_name' => 'Brunei Darussalam'))
        ->condition('country_id', 96)
        ->execute();
      break;
  }
}
