<?php
/**
 * @file
 * Main module functionality for the module_install module.
 */

/**
 * Implements hook_module_install_exceptions().
 */
function module_install_module_install_exceptions() {
  return array(
    'sites/all/modules/features',
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function module_install_form_update_manager_install_form_alter(&$form, &$form_state) {

  module_load_include('inc', 'module_install', 'module_install.api');

  if (isset($form_state['values']['destination'])) {
    $destination = $form_state['values']['destination'];
  }
  else {
    $destination = module_install_get_destination_choice('modules');
  }

  $searchdir = module_install_get_search_directories('modules');
  $options = $searchdir;

  foreach ($searchdir as $dir) {
    module_install_get_destination_directories($dir, $options, 'module');
  }

  asort($options);

  $form['destination'] = array(
    '#type' => 'select',
    '#title' => t('Destination folder'),
    '#options' => $options,
    '#default_value' => $destination,
  );
  $form['#submit'][0] = 'module_install_update_manager_install_form_submit';

}

/**
 * Form sumbit handler for the module install form.
 *
 * @see module_install_form_alter()
 */
function module_install_update_manager_install_form_submit($form, &$form_state) {
  module_load_include('inc', 'module_install', 'module_install.api');
  module_install_set_destination_choice($form_state['values']['destination']);
  update_manager_install_form_submit($form, $form_state);
}

/**
 * Implements hook_updater_info_alter().
 */
function module_install_updater_info_alter(&$updaters) {
  $updaters['module']['class'] = 'ModuleInstallUpdater';
}
