#!/usr/bin/php
<?php

require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/includes/base.php';
@extract(getConfig());

$args = new \cli\Arguments();

$args->addOption(array('civi-root', 'r'), array(
  'default' => (@$config['CIVICRM_ROOT']) ? $config['CIVICRM_ROOT'] : __DIR__,
  'description' => 'Path to CiviCRM',
  ));

$args->addOption(array('input-file', 'f'), array(
  'default' => STDIN,
  'description' => 'File to act on. Alternate to pipe to STDIN',
));

$args->addOption(array('id-col', 'i'), array(
  'default' => 'Internal Contact ID',
  'description' => 'CiviCRM Contact ID Column',
));

//Todo: Make a way to dynamically list actions.
$args->addOption(array('action', 'a'), array(
  'default' => (@$config['Action']) ? $config['Action'] : null,
  'description' => 'The action to Take',
));
$args->addOption(array('contact-type', 't'), array(
  'default' => (@$config['contact_type']) ? $config['contact_type'] : 'Organization',
  'description' => 'The type of contact to create',
));
$args->addOption(array('contact-sub-type', 's'), array(
  'default' => (@$config['contact_sub_type']) ? $config['contact_sub_type'] : null,
  'description' => 'The subtype of the contact(s) to be created',
));

$args->addFlag(array('undelete', 'ctrl-z', 'z'), 'Remove contacts from the trash is_deleted => false');

$msg = <<<USAGE
Show this help screen

Example
  ./clean --input-file my.csv --civi-root /var/www/sites/all/modules/civicrm --id-col 'Internal Contact ID;

USAGE;

$args->addFlag(array('help', 'h', 'usage', 'u'), $msg);
$args->parse();
if ($args['help']) {
	die($args->getHelpScreen()."\n");
}

/**
 * END CONFIG
 */

define('CIVICRM_ROOT', getOption('civi-root', $args));
bootstrap_civicrm();
validateApiConfig();


$action = getOption('action', $args);

if (file_exists(__DIR__.'/includes/'.$action.'.php')) {
  require(__DIR__.'/includes/'.$action.'.php');
}

$funcName = $action."_run";
if(function_exists($funcName)) {
  $funcName($args);
} else {
  die("Unknown Action");
}









