<?php

/**
* Implements hook_install() using the name of the country as the base of
* the function name.
*/
function turkey_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' => 792,
      'country_name' => 'Turkey',
      'country_iso_code_2' => 'TR',
      'country_iso_code_3' => 'TUR',
      'version' => 3,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(792, 'TR-01', 'Adana'),
    array(792, 'TR-02', 'Adıyaman'),
    array(792, 'TR-03', 'Afyon'),
    array(792, 'TR-04', 'Ağrı'),
    array(792, 'TR-68', 'Aksaray'),
    array(792, 'TR-05', 'Amasya'),
    array(792, 'TR-06', 'Ankara'),
    array(792, 'TR-07', 'Antalya'),
    array(792, 'TR-75', 'Ardahan'),
    array(792, 'TR-08', 'Artvin'),
    array(792, 'TR-09', 'Aydın'),
    array(792, 'TR-10', 'Balıkesir'),
    array(792, 'TR-74', 'Bartın'),
    array(792, 'TR-72', 'Batman'),
    array(792, 'TR-69', 'Bayburt'),
    array(792, 'TR-11', 'Bilecik'),
    array(792, 'TR-12', 'Bingöl'),
    array(792, 'TR-13', 'Bitlis'),
    array(792, 'TR-14', 'Bolu'),
    array(792, 'TR-15', 'Burdur'),
    array(792, 'TR-16', 'Bursa'),
    array(792, 'TR-17', 'Çanakkale'),
    array(792, 'TR-18', 'Çankırı'),
    array(792, 'TR-19', 'Çorum'),
    array(792, 'TR-20', 'Denizli'),
    array(792, 'TR-21', 'Diyarbakır'),
    array(792, 'TR-81', 'Düzce'),
    array(792, 'TR-22', 'Edirne'),
    array(792, 'TR-23', 'Elazığ'),
    array(792, 'TR-24', 'Erzincan'),
    array(792, 'TR-25', 'Erzurum'),
    array(792, 'TR-26', 'Eskişehir'),
    array(792, 'TR-27', 'Gaziantep'),
    array(792, 'TR-28', 'Giresun'),
    array(792, 'TR-29', 'Gümüşhane'),
    array(792, 'TR-30', 'Hakkâri'),
    array(792, 'TR-31', 'Hatay'),
    array(792, 'TR-76', 'Iğdır'),
    array(792, 'TR-32', 'Isparta'),
    array(792, 'TR-33', 'İçel'),
    array(792, 'TR-34', 'İstanbul'),
    array(792, 'TR-35', 'İzmir'),
    array(792, 'TR-46', 'Kahramanmaraş'),
    array(792, 'TR-78', 'Karabük'),
    array(792, 'TR-70', 'Karaman'),
    array(792, 'TR-36', 'Kars'),
    array(792, 'TR-37', 'Kastamonu'),
    array(792, 'TR-38', 'Kayseri'),
    array(792, 'TR-71', 'Kırıkkale'),
    array(792, 'TR-39', 'Kırklareli'),
    array(792, 'TR-40', 'Kırşehir'),
    array(792, 'TR-79', 'Kilis'),
    array(792, 'TR-41', 'Kocaeli'),
    array(792, 'TR-42', 'Konya'),
    array(792, 'TR-43', 'Kütahya'),
    array(792, 'TR-44', 'Malatya'),
    array(792, 'TR-45', 'Manisa'),
    array(792, 'TR-47', 'Mardin'),
    array(792, 'TR-48', 'Muğla'),
    array(792, 'TR-49', 'Muş'),
    array(792, 'TR-50', 'Nevşehir'),
    array(792, 'TR-51', 'Niğde'),
    array(792, 'TR-52', 'Ordu'),
    array(792, 'TR-80', 'Osmaniye'),
    array(792, 'TR-53', 'Rize'),
    array(792, 'TR-54', 'Sakarya'),
    array(792, 'TR-55', 'Samsun'),
    array(792, 'TR-56', 'Siirt'),
    array(792, 'TR-57', 'Sinop'),
    array(792, 'TR-58', 'Sivas'),
    array(792, 'TR-63', 'Şanlıurfa'),
    array(792, 'TR-73', 'Şırnak'),
    array(792, 'TR-59', 'Tekirdağ'),
    array(792, 'TR-60', 'Tokat'),
    array(792, 'TR-61', 'Trabzon'),
    array(792, 'TR-62', 'Tunceli'),
    array(792, 'TR-64', 'Uşak'),
    array(792, 'TR-65', 'Van'),
    array(792, 'TR-77', 'Yalova'),
    array(792, 'TR-66', 'Yozgat'),
    array(792, 'TR-67', 'Zonguldak'),
  );

  $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(
    792,
    "!company\r\n".
    "!first_name !last_name\r\n".
    "!street1\r\n".
    "!street2\r\n".
    "!postal_code, !city\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 turkey_update($version) {
  switch ($version) {
    case 2:
      db_update('uc_countries')
        ->fields(array('country_iso_code_2' => 'TR'))
        ->condition('country_id', 792)
        ->execute();
      break;

    case 3:
      $zones = array(
        array(792, 'TR-01', 'Adana'),
        array(792, 'TR-02', 'Adıyaman'),
        array(792, 'TR-03', 'Afyon'),
        array(792, 'TR-04', 'Ağrı'),
        array(792, 'TR-68', 'Aksaray'),
        array(792, 'TR-05', 'Amasya'),
        array(792, 'TR-06', 'Ankara'),
        array(792, 'TR-07', 'Antalya'),
        array(792, 'TR-75', 'Ardahan'),
        array(792, 'TR-08', 'Artvin'),
        array(792, 'TR-09', 'Aydın'),
        array(792, 'TR-10', 'Balıkesir'),
        array(792, 'TR-74', 'Bartın'),
        array(792, 'TR-72', 'Batman'),
        array(792, 'TR-69', 'Bayburt'),
        array(792, 'TR-11', 'Bilecik'),
        array(792, 'TR-12', 'Bingöl'),
        array(792, 'TR-13', 'Bitlis'),
        array(792, 'TR-14', 'Bolu'),
        array(792, 'TR-15', 'Burdur'),
        array(792, 'TR-16', 'Bursa'),
        array(792, 'TR-17', 'Çanakkale'),
        array(792, 'TR-18', 'Çankırı'),
        array(792, 'TR-19', 'Çorum'),
        array(792, 'TR-20', 'Denizli'),
        array(792, 'TR-21', 'Diyarbakır'),
        array(792, 'TR-81', 'Düzce'),
        array(792, 'TR-22', 'Edirne'),
        array(792, 'TR-23', 'Elazığ'),
        array(792, 'TR-24', 'Erzincan'),
        array(792, 'TR-25', 'Erzurum'),
        array(792, 'TR-26', 'Eskişehir'),
        array(792, 'TR-27', 'Gaziantep'),
        array(792, 'TR-28', 'Giresun'),
        array(792, 'TR-29', 'Gümüşhane'),
        array(792, 'TR-30', 'Hakkâri'),
        array(792, 'TR-31', 'Hatay'),
        array(792, 'TR-76', 'Iğdır'),
        array(792, 'TR-32', 'Isparta'),
        array(792, 'TR-33', 'İçel'),
        array(792, 'TR-34', 'İstanbul'),
        array(792, 'TR-35', 'İzmir'),
        array(792, 'TR-46', 'Kahramanmaraş'),
        array(792, 'TR-78', 'Karabük'),
        array(792, 'TR-70', 'Karaman'),
        array(792, 'TR-36', 'Kars'),
        array(792, 'TR-37', 'Kastamonu'),
        array(792, 'TR-38', 'Kayseri'),
        array(792, 'TR-71', 'Kırıkkale'),
        array(792, 'TR-39', 'Kırklareli'),
        array(792, 'TR-40', 'Kırşehir'),
        array(792, 'TR-79', 'Kilis'),
        array(792, 'TR-41', 'Kocaeli'),
        array(792, 'TR-42', 'Konya'),
        array(792, 'TR-43', 'Kütahya'),
        array(792, 'TR-44', 'Malatya'),
        array(792, 'TR-45', 'Manisa'),
        array(792, 'TR-47', 'Mardin'),
        array(792, 'TR-48', 'Muğla'),
        array(792, 'TR-49', 'Muş'),
        array(792, 'TR-50', 'Nevşehir'),
        array(792, 'TR-51', 'Niğde'),
        array(792, 'TR-52', 'Ordu'),
        array(792, 'TR-80', 'Osmaniye'),
        array(792, 'TR-53', 'Rize'),
        array(792, 'TR-54', 'Sakarya'),
        array(792, 'TR-55', 'Samsun'),
        array(792, 'TR-56', 'Siirt'),
        array(792, 'TR-57', 'Sinop'),
        array(792, 'TR-58', 'Sivas'),
        array(792, 'TR-63', 'Şanlıurfa'),
        array(792, 'TR-73', 'Şırnak'),
        array(792, 'TR-59', 'Tekirdağ'),
        array(792, 'TR-60', 'Tokat'),
        array(792, 'TR-61', 'Trabzon'),
        array(792, 'TR-62', 'Tunceli'),
        array(792, 'TR-64', 'Uşak'),
        array(792, 'TR-65', 'Van'),
        array(792, 'TR-77', 'Yalova'),
        array(792, 'TR-66', 'Yozgat'),
        array(792, 'TR-67', 'Zonguldak'),
      );

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