<?php

/**
 * @file
 * Handles AdvAgg Bundler installation and upgrade tasks.
 */

/**
 * Implements hook_install().
 */
function advagg_bundler_install() {
  drupal_install_schema('advagg_bundler');
}

/**
 * Implements hook_enable().
 */
function advagg_bundler_enable() {
  // Flush advagg caches.
  $cache_tables = advagg_flush_caches();
  foreach ($cache_tables as $table) {
    cache_clear_all('*', $table, TRUE);
  }
}

/**
 * Implements hook_disable().
 */
function advagg_bundler_disable() {
  // Flush advagg caches.
  $cache_tables = advagg_flush_caches();
  foreach ($cache_tables as $table) {
    cache_clear_all('*', $table, TRUE);
  }
}

/**
 * Implementation of hook_uninstall().
 */
function advagg_bundler_uninstall() {
  // Remove variables.
  variable_del('advagg_bundler_outdated');
  variable_del('advagg_bundler_max_css');
  variable_del('advagg_bundler_max_js');
  variable_del('advagg_bundler_active');

  drupal_uninstall_schema('advagg_bundler');
}

/**
 * Implementation of hook_requirements().
 */
function advagg_bundler_requirements($phase) {
  $requirements = array();
  // Ensure translations don't break at install time
  $t = get_t();

  if ($phase == 'runtime') {
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function advagg_bundler_schema() {
  $schema['advagg_bundler_selector_count'] = array(
    'description' => 'Keep track of when the files were modified.',
    'fields' => array(
      'filename' => array(
        'description' => 'Path of the file relative to Drupal webroot.',
        'type' => 'text',
        'size' => 'normal',
        'not null' => TRUE,
      ),
      'filename_md5' => array(
        'description' => 'MD5 hash of filename',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'selector_count' => array(
        'description' => 'CSS selector count of the file.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'timestamp' => array(
        'description' => 'Last modified timestamp of the file.',
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('filename_md5'),
  );

  return $schema;
}

/**
 * Create new database table {advagg_bundler_selector_count}.
 */
function advagg_bundler_update_6100() {
  $schema['advagg_bundler_selector_count'] = array(
    'description' => 'Keep track of when the files were modified.',
    'fields' => array(
      'filename' => array(
        'description' => 'Path of the file relative to Drupal webroot.',
        'type' => 'text',
        'size' => 'normal',
        'not null' => TRUE,
      ),
      'filename_md5' => array(
        'description' => 'MD5 hash of filename',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'selector_count' => array(
        'description' => 'CSS selector count of the file.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'timestamp' => array(
        'description' => 'Last modified timestamp of the file.',
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('filename_md5'),
  );
  $ret = array();
  db_create_table($ret, 'advagg_bundler_selector_count', $schema['advagg_bundler_selector_count']);
  return $ret;
}
