<?php

/**
 * First implement hook_install() using the name of the country as the base of
 * the function name.
 */
function india_install() {
  // Make the entry in the country table.
  // VALUES = Country ID, Country Name, 2-digit Code, 3-digit Code, File Version
  db_query("INSERT INTO {uc_countries} (country_id, country_name, country_iso_code_2, country_iso_code_3, version) VALUES ('356', 'India', 'IN', 'IND', 2)");

  // Make the entries in the zones table.
  $zones = array(
    array(356, 'AP', 'Andhra Pradesh'),
    array(356, 'AR', 'Arunachal Pradesh'),
    array(356, 'AS', 'Assam'),
    array(356, 'BR', 'Bihar'),
    array(356, 'CT', 'Chhattisgarh'),
    array(356, 'GA', 'Goa'),
    array(356, 'GJ', 'Gujarat'),
    array(356, 'HR', 'Haryana'),
    array(356, 'HP', 'Himachal Pradesh'),
    array(356, 'JK', 'Jammu and Kashmir'),
    array(356, 'JH', 'Jharkhand'),
    array(356, 'KA', 'Karnataka'),
    array(356, 'KL', 'Kerala'),
    array(356, 'MP', 'Madhya Pradesh'),
    array(356, 'MH', 'Maharashtra'),
    array(356, 'MN', 'Manipur'),
    array(356, 'ML', 'Meghalaya'),
    array(356, 'MZ', 'Mizoram'),
    array(356, 'NL', 'Nagaland'),
    array(356, 'OR', 'Orissa'),
    array(356, 'PB', 'Punjab'),
    array(356, 'RJ', 'Rajasthan'),
    array(356, 'SK', 'Sikkim'),
    array(356, 'TN', 'Tamil Nadu'),
    array(356, 'TR', 'Tripura'),
    array(356, 'UL', 'Uttarakhand'),
    array(356, 'UP', 'Uttar Pradesh'),
    array(356, 'WB', 'West Bengal'),
    array(356, 'AN', 'Andaman and Nicobar'),
    array(356, 'CH', 'Chandigarh'),
    array(356, 'DN', 'Dadra and Nagar Haveli'),
    array(356, 'DD', 'Daman and Diu'),
    array(356, 'DL', 'Delhi'),
    array(356, 'LD', 'Lakshadweep'),
    array(356, 'PY', 'Puducherry'),
  );

  foreach ($zones as $zone) {
    db_query("INSERT INTO {uc_zones} (zone_country_id, zone_code, zone_name) VALUES (%d, '%s', '%s')", $zone);
  }

  // Use uc_set_address_format() with the country ID as the first argument and
  // an address format string as the second.  Documentation on address formats
  // is available at:
  // http://www.ubercart.org/ubercart_users_guide/country_settings#address_format_docs
  uc_set_address_format(356,
    "!company\r\n!first_name !last_name\r\n!street1\r\n!street2"
   ."\r\n!city - !postal_code\r\n!zone_code\r\n!country_name_if");
}

function india_update($version) {
  switch ($version) {
    case 2:
      // Add some missing zones
      $zones = array(
        array(356, 'LD', 'Lakshadweep'),
        array(356, 'PY', 'Puducherry'),
      );

      foreach ($zones as $zone) {
        db_query("INSERT INTO {uc_zones} (zone_country_id, zone_code, zone_name) VALUES (%d, '%s', '%s')", $zone);
      }

      $zones = array(
        // Correct zone name misspelling and ISO-3166-2 code
        array(356, 'CG', 'CT', 'Chhattisgarh'),

        // Replace ampersand with 'and' for consistency within .cif
        // and for agreement with ISO-3166-2
        array(356, 'JK', 'JK', 'Jammu and Kashmir'),
        array(356, 'AN', 'AN', 'Andaman and Nicobar'),
        array(356, 'DD', 'DD', 'Daman and Diu'),

        // Correct ISO-3166-2 code
        array(356, 'UA', 'UL', 'Uttarakhand'),
      );

      foreach ($zones as $zone) {
        db_query("UPDATE {uc_zones} SET zone_name = '%s', zone_code = '%s' WHERE zone_country_id = %d AND zone_code = '%s'", $zone[3], $zone[2], $zone[0], $zone[1]);
      }

      break;
  }
}
