<?php
// $Id: ip2country.install,v 1.7 2010/10/17 17:02:07 tr Exp $


// Include function definition for ip2country_update_database(),
// which is needed to initially fill the database after creation.
include_once(drupal_get_path('module', 'ip2country') .'/ip2country.inc');


/**
 * Implementation of hook_requirements().
 */
function ip2country_requirements($phase) {
  $t = get_t();

  $has_curl = function_exists('curl_init');
  $requirements['ip2country_curl'] = array(
    'title' => $t('cURL'),
    'value' => $has_curl ? $t('Enabled') : $t('Not found'),
  );
  if (!$has_curl) {
    $requirements['ip2country_curl']['severity'] = REQUIREMENT_ERROR;
    $requirements['ip2country_curl']['description'] = $t("ip2country requires the PHP <a href='!curl_url'>cURL</a> library.", array('!curl_url' => 'http://php.net/manual/en/curl.setup.php'));
  }

  return $requirements;
}


/**
 * Implementation of hook_schema().
 */
function ip2country_schema() {
  $schema['ip2country'] = array(
    'description' => 'Association between IP range and Country',
    'fields' => array(
      'ip2country_id' => array(
        'description' => 'Row number (why is this needed?)',
        'type'        => 'serial',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
      ),
      'country' => array(
        'description' => 'ISO 3166 2-character country code',
        'type'        => 'char',
        'length'      => 3,
        'not null'    => TRUE,
      ),
      'registry' => array(
        'description' => 'Regional Internet Resgistry',
        'type'        => 'char',
        'length'      => 10,
        'not null'    => TRUE,
      ),
      'ip_range_first' => array(
        'description' => 'Lowest IP address in range',
        'type'        => 'int',
        'size'        => 'big',
        'not null'    => TRUE,
      ),
      'ip_range_last' => array(
        'description' => 'Highest IP address in range',
        'type'        => 'int',
        'size'        => 'big',
        'not null'    => TRUE,
      ),
      'ip_range_length' => array(
        'description' => 'Size of IP address block',
        'type'        => 'int',
        'not null'    => TRUE,
      ),
    ),
    'indexes' => array(
      'country_registry' => array('country', 'registry'),
    ),
    'primary key' => array('ip2country_id'),
  );

  return $schema;
}


/**
 * Implementation of hook_install().
 *
 * Creates database tables needed by this module.
 */
function ip2country_install() {

  /* Create tables */
  drupal_install_schema('ip2country');

  /* Populate the database */
  ip2country_update_database();
}


/**
 * Implementation of hook_uninstall().
 *
 * Removes all tables and variables inserted into the
 * database by this module.
 */
function ip2country_uninstall() {

  /* Remove all database tables created by this module */
  drupal_uninstall_schema('ip2country');

  /* Remove all module variables from the database */
  variable_del('ip2country_debug');
  variable_del('ip2country_test_type');
  variable_del('ip2country_test_ip_address');
  variable_del('ip2country_test_country');
  variable_del('ip2country_rir');
  variable_del('ip2country_last_update');
  variable_del('ip2country_lookup');
  variable_del('ip2country_lookup_button');
  variable_del('ip2country_update_interval');
  variable_del('ip2country_update_database');
  variable_del('ip2country_watchdog');
}


/**
 * Rename tables and variables, clean up database.
 */
function ip2country_update_1() {
  $ret = array();

  /* Remove all database tables created by this module */
  $ret[] = update_sql("ALTER TABLE {uc_ip2country} RENAME TO {ip2country}");

  /* Rename variables while  preserving setting values */
  variable_set('ip2country_debug',
               variable_get('uc_ip2country_debug', FALSE));
  variable_set('ip2country_test_type',
               variable_get('uc_ip2country_test_type', 0));
  variable_set('ip2country_test_ip_address',
               variable_get('uc_ip2country_test_ip_address', ''));
  variable_set('ip2country_test_country',
               variable_get('uc_ip2country_test_country', ''));
  variable_set('ip2country_rir',
               variable_get('uc_ip2country_rir', 'arin'));
  variable_set('ip2country_last_update',
               variable_get('uc_ip2country_last_update', 0));
  variable_set('ip2country_update_interval',
               variable_get('uc_ip2country_update_interval', 604800));
  variable_set('ip2country_update_database',
               variable_get('uc_ip2country_update_database', ''));
  variable_set('ip2country_watchdog',
               variable_get('uc_ip2country_watchdog', 1));

  /* Remove all old variables from the database */
  variable_del('uc_ip2country_debug');
  variable_del('uc_ip2country_test_type');
  variable_del('uc_ip2country_test_ip_address');
  variable_del('uc_ip2country_test_country');
  variable_del('uc_ip2country_rir');
  variable_del('uc_ip2country_last_update');
  variable_del('uc_ip2country_update_interval');
  variable_del('uc_ip2country_update_database');
  variable_del('uc_ip2country_watchdog');

  return $ret;
}


/**
 * Drop ip_range_date column as it is never used and just
 * takes up database space and processing time.
 */
function ip2country_update_2() {
  $ret = array();

  // Drop ip_range_date
  db_drop_field($ret, 'ip2country', 'ip_range_date');

  return $ret;
}
