Index: media_mover_api.drush.inc
===================================================================
RCS file: media_mover_api.drush.inc
diff -N media_mover_api.drush.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ media_mover_api.drush.inc	3 Dec 2009 02:20:22 -0000
@@ -0,0 +1,84 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Drush integration for Media Mover. 
+ */
+
+/**
+ * Implementation of hook_drush_help().
+ */
+function media_mover_api_drush_help($section) {
+  switch ($section) {
+    case 'drush:mm run':
+      return dt("Run a Media Mover configuration.");
+  }
+}
+
+/**
+ * Implementation of hook_drush_command().
+ */
+function media_mover_api_drush_command() {
+  $items['mm run'] = array(
+    'callback' => 'media_mover_api_drush_run_config',
+    'description' => 'Run a Media Mover configuration.',
+    'arguments' => array(
+      'mm_config' => 'Media Mover configuration to run.',
+    ),
+  );
+  return $items;
+}
+
+/**
+ * Run a Media Mover configuration
+ */
+function media_mover_api_drush_run_config() {
+  $args = func_get_args();
+
+  if (count($args) == 1) {
+    $config_name = array_shift($args);
+
+    $cid = _media_mover_api_drush_config_exists($config_name);
+    if ($cid) {
+      $output = media_mover_api_run_configuration($cid);
+      // Clean up output so it looks ok as stdout
+      $output = strip_tags(str_replace("<br />","\n", $output));
+      drush_print($output);
+    } 
+    else {
+      drush_print();
+      drush_print(dt('"' . $config_name . '" does not appear to be a valid Media Mover configuration'));
+      drush_print();
+      _media_mover_api_drush_list_configs();
+    }  
+  }
+  else {
+    _media_mover_api_drush_list_configs();
+  }
+}
+
+function _media_mover_api_drush_config_exists($config_name) {
+  // Get names of configs, create machine names to compare against drush arg.
+  $resource = db_query('SELECT cid, name FROM media_mover_config_list');
+  $mm_confs = array();
+  while ($row = db_fetch_array($resource)) $mm_confs[] = $row;
+
+  foreach($mm_confs as $i => $conf) {
+    $machine_name = strtolower(preg_replace('/[ -]+/', '_', $conf['name']));
+    if ($config_name == $machine_name) {
+      $cid = $conf['cid'];
+    }
+  }
+  return $cid ? $cid : FALSE;
+}
+
+function _media_mover_api_drush_list_configs() {
+  drush_print(dt('Available configurations'));
+  $rows = array(dt('Available configurations'));
+  foreach (media_mover_api_get_configurations() as $configuration) {
+    $config_name = strtolower(preg_replace('/[ -]+/', '_', $configuration->name));
+    $rows[] = array($config_name);
+  }
+  drush_print_table($rows, TRUE);
+}
