#!/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',
));

$args->addOption(array('rel-type-id'), array(
  'description' => 'The numeric ID of the relationship to create for the contact',
));

$args->addOption(array('rel-ext-id-col'), array(
  'description' => 'Column containing the external ID of the contact not imported in this run',
));

$args->addOption(array('rel-AB'), array(
  'default' => 'A',
  'description' => 'The side of the relationship (A or B) for the contact imported in this run',
));

//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->addOption(array('event-type', 'e'), array(
  // Note: 'Conference' is the machine name for Missions since some doofus renamed an existing type
  'default' => (@$config['event_type']) ? $config['event_type'] : 'Conference',
  'description' => 'The type of event to create',
));

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

$args->addFlag('check-for-dupes', array(
  'description' => 'If set, check for dupe by first, last, email before importing.',
));


$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');
} 
$inputFile = getOption('input-file', $args);
if (file_exists($inputFile)) {
  echo("Input file exists\n");
} else {
  die("input file $inputFile does not exist\n");
}

$funcName = $action."_run";

if(function_exists($funcName)) {
  $funcName($args);
} else {
  die("function $funcName not found\n");
}
