<?php
// $Id $
/**
 * Implementation of hook_install()
 */
function avatar_selection_install() {
  if (!variable_get('user_pictures', 0)) {
    drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting at the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  }

  drupal_install_schema('avatar_selection');
}

/**
 * Implementation of hook_uninstall().
 */
function avatar_selection_uninstall() {
  // delete the variables we created.
  variable_del('avatar_selection_disable_user_upload');
  variable_del('avatar_selection_force_user_avatar_reg');
  variable_del('avatar_selection_force_user_avatar');
  variable_del('avatar_selection_avatar_per_page');

  // delete the images
  $dir = file_create_path('avatar_selection') .'/';
  $listings = file_scan_directory($dir, '.*\.(gif|GIF|Gif|jpg|JPG|Jpg|jpeg|JPEG|Jpeg|png|PNG|Png)', array('.', '..', 'CVS'), 0, FALSE);
  if ($listings) {
    foreach ($listings as $listing) {
      file_delete($dir . $listing->basename);
    }
  }

  // drop the avatar_selection table
  drupal_uninstall_schema('avatar_selection');

  // clear the cache tables
  cache_clear_all(null, 'cache');
  cache_clear_all(null, 'cache_filter');
  cache_clear_all(null, 'cache_menu');
  cache_clear_all(null, 'cache_page');
 
  watchdog('Avatar Selection', 'avatar_selection module removed');
}

function avatar_selection_update_1() {
  $schema['avatar_selection'] = array(
    'fields' => array(
      'avatar' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
      'access' => array('type' => 'varchar', 'length' => 255),
    ),
    'primary key' => array('avatar'),
    'unique keys' => array('avatar' => array('avatar')),
  );

  $ret = array();
  db_create_table($ret, 'avatar_selection', $schema['avatar_selection']);

  return $ret;
}

function avatar_selection_update_2() {
  $ret = array();

  db_add_field($ret, 'avatar_selection', 'og_access', array('type' => 'varchar', 'length' => 255));
  return $ret;
}
