<?php

/**
 * @file
 * Installation and uninstallation functions for the GeSHi filter.
 */

/**
 * Implementation of hook_install
 */
function geshifilter_install() {
  // Enable some popular languages and set their language tags by default
  $languages = array('php', 'drupal5', 'drupal6', 'javascript', 'java', 'c', 'cpp', 'python', 'ruby');
  foreach ($languages as $language) {
    variable_set('geshifilter_language_enabled_' . $language, TRUE);
    variable_set('geshifilter_language_tags_' . $language, '<' . $language . '>');
  }

  // what to do next?
  drupal_set_message(t('GeSHi filter is installed. You should now <a href="!geshi_admin">configure the GeSHi filter</a> and enable it in the desired <a href="!input_formats">input formats</a>.',
    array('!geshi_admin' => url('admin/settings/geshifilter'), '!input_formats' => url('admin/settings/filters'))
  ));
}

/**
 * On uninstall: remove module variables and clear variable cache
 */
function geshifilter_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE 'geshifilter_%'");
  cache_clear_all('variables', 'cache');
}

/**
 * Implementation of hook_update_N().
 */
function geshifilter_update_1() {
  // clear the cache of available languages
  variable_del('geshifilter_available_languages');
  return array();
}

/**
 * Implementation of hook_update_N().
 *
 * Upgrade path from usage of geshifilter_brackets variable
 * to geshifilter_tag_styles variable.
 */
function geshifilter_update_6001() {
  _geshifilter_update_6001_brackets_to_tag_styles(NULL);
  foreach (filter_formats() as $format => $input_format) {
    _geshifilter_update_6001_brackets_to_tag_styles($format);
  }
  return array();
}

/**
 * Helper function for updating the old geshifilter_brackets variables
 * to geshifilter_tag_styles variables.
 * @param $format NULL (for global variable) or input format id.
 */
function _geshifilter_update_6001_brackets_to_tag_styles($format) {
  // Make $format a suffix for usage in variable name.
  $format = isset($format) ? "_{$format}" : '';
  // Try to fetch the variable value and convert it if found.
  $geshifilter_brackets = variable_get('geshifilter_brackets'. $format, NULL);
  if (isset($geshifilter_brackets)) {
    switch ($geshifilter_brackets) {
      case GESHIFILTER_BRACKETS_ANGLE:
      case GESHIFILTER_BRACKETS_SQUARE:
        variable_set('geshifilter_tag_styles'. $format, array(
          $geshifilter_brackets => $geshifilter_brackets
        ));
        break;
      case GESHIFILTER_BRACKETS_BOTH:
        variable_set('geshifilter_tag_styles'. $format, array(
          GESHIFILTER_BRACKETS_ANGLE => GESHIFILTER_BRACKETS_ANGLE,
          GESHIFILTER_BRACKETS_SQUARE => GESHIFILTER_BRACKETS_SQUARE,
        ));
        break;
    }
    // Remove old variable.
    variable_del('geshifilter_brackets'. $format);
  }
}

/**
 * Implementation of hook_update_N().
 *
 * Upgrade path for merging of geshifilter_enable_php_delimiters
 * into to the geshifilter_tag_styles variable.
 */
function geshifilter_update_6002() {
  _geshifilter_update_6002_php_delimter_into_tag_styles(NULL);
  foreach (filter_formats() as $format => $input_format) {
    _geshifilter_update_6002_php_delimter_into_tag_styles($format);
  }
  return array();
}

/**
 * Helper function for merging old geshifilter_enable_php_delimiters
 * into geshifilter_tag_styles variables.
 * @param $format NULL (for global variable) or input format id.
 */
function _geshifilter_update_6002_php_delimter_into_tag_styles($format) {
  // Load geshifilter.inc because we need _geshifilter_tag_styles().
  require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.inc';

  // Make $format a suffix for usage in variable name.
  $format = isset($format) ? "_{$format}" : '';
  // Try to fetch the variable value and convert it if found.
  $geshifilter_enable_php_delimiters = variable_get('geshifilter_enable_php_delimiters'. $format, NULL);
  if (isset($geshifilter_enable_php_delimiters)) {
    // First get the current value of the geshifilter_tag_styles variable.
    $geshifilter_brackets = _geshifilter_tag_styles($format);
    // Insert value for PHP style code blocks
    if ($geshifilter_enable_php_delimiters) {
      $geshifilter_brackets[GESHIFILTER_BRACKETS_PHPBLOCK] = GESHIFILTER_BRACKETS_PHPBLOCK;
    }
    else {
      $geshifilter_brackets[GESHIFILTER_BRACKETS_PHPBLOCK] = 0;
    }
    // Save the new settings.
    variable_set('geshifilter_tag_styles'. $format, $geshifilter_brackets);
    // Remove old variable.
    variable_del('geshifilter_enable_php_delimiters'. $format);
  }
}

/**
 * Implementation of hook_update_N().
 *
 * (Re)generate the languages CSS file to be sure it exists.
 * (References: http://drupal.org/node/269140, http://drupal.org/node/682068)
 */
function geshifilter_update_6003() {
  if (GESHIFILTER_CSS_CLASSES_AUTOMATIC == variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE)) {
    require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.admin.inc';
    _geshifilter_generate_languages_css_file();
  }
  return array();
}

