From b9bae91405f281e2850e4c9ee942e2d9e2482e91 Mon Sep 17 00:00:00 2001
From: Kyah Rindlisbacher <kyah101@hotmail.com>
Date: Fri, 20 Jan 2012 21:15:18 +1000
Subject: [PATCH] Issue #1400360 by L-four: Renamed campaignmonitor.make,
 added a drush command to download the createsend-php
 library.

---
 campaignmonitor.info            |    1 +
 campaignmonitor.make            |    8 --
 campaignmonitor.make.example    |    8 ++
 drush/campaignmonitor.drush.inc |  162 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 171 insertions(+), 8 deletions(-)
 delete mode 100644 campaignmonitor.make
 create mode 100644 campaignmonitor.make.example
 create mode 100644 drush/campaignmonitor.drush.inc

diff --git a/campaignmonitor.info b/campaignmonitor.info
index 8f58cbf..82e767f 100644
--- a/campaignmonitor.info
+++ b/campaignmonitor.info
@@ -8,6 +8,7 @@ dependencies[] = libraries
 
 files[] = campaignmonitor.module
 files[] = campaignmonitor.install
+files[] = drush/campaignmonitor.drush.inc
 files[] = includes/campaignmonitor.admin.inc
 files[] = includes/campaignmonitor_list.admin.inc
 files[] = includes/campaignmonitor_stats.admin.inc
diff --git a/campaignmonitor.make b/campaignmonitor.make
deleted file mode 100644
index 2fa0aff..0000000
--- a/campaignmonitor.make
+++ /dev/null
@@ -1,8 +0,0 @@
-core = 7.x
-api = 2
-
-libraries[campaignmonitor][download][type] = "git"
-libraries[campaignmonitor][download][url] = "http://github.com/campaignmonitor/createsend-php.git"
-libraries[campaignmonitor][download][tag] = "1.0.13"
-
-projects[libraries] = "1.0"
diff --git a/campaignmonitor.make.example b/campaignmonitor.make.example
new file mode 100644
index 0000000..2fa0aff
--- /dev/null
+++ b/campaignmonitor.make.example
@@ -0,0 +1,8 @@
+core = 7.x
+api = 2
+
+libraries[campaignmonitor][download][type] = "git"
+libraries[campaignmonitor][download][url] = "http://github.com/campaignmonitor/createsend-php.git"
+libraries[campaignmonitor][download][tag] = "1.0.13"
+
+projects[libraries] = "1.0"
diff --git a/drush/campaignmonitor.drush.inc b/drush/campaignmonitor.drush.inc
new file mode 100644
index 0000000..8eb4eac
--- /dev/null
+++ b/drush/campaignmonitor.drush.inc
@@ -0,0 +1,162 @@
+<?php
+
+/**
+ * @file
+ *   campaignmonitor drush command.
+ *
+ *   To run this *fun* command, execute `sudo drush --include=./examples mmas`
+ *   from within your drush directory.
+ *
+ *   See `drush topic docs-commands` for more information about command authoring.
+ *
+ *   You can copy this file to any of the following
+ *     1. A .drush folder in your HOME folder.
+ *     2. Anywhere in a folder tree below an active module on your site.
+ *     3. /usr/share/drush/commands (configurable)
+ *     4. In an arbitrary folder specified with the --include option.
+ *     5. Drupal's sites/all/drush folder.
+ */
+
+define('CREATESEND_DOWNLOAD_URI', 'https://github.com/campaignmonitor/createsend-php/zipball/1.0.13');
+define('CREATESEND_FOLDER', 'campaignmonitor-createsend-php-1981587');
+
+/**
+ * Implementation of hook_drush_command().
+ *
+ * In this hook, you specify which commands your
+ * drush module makes available, what it does and
+ * description.
+ *
+ * Notice how this structure closely resembles how
+ * you define menu hooks.
+ *
+ * See `drush topic docs-commands` for a list of recognized keys.
+ *
+ * @return
+ *   An associative array describing your command(s).
+ */
+function campaignmonitor_drush_command() {
+  $items = array();
+
+  $items['download-createsend-lib'] = array(
+  	'callback' => 'campaignmonitor_download_createsend_lib',
+    'description' => dt('Downloads the Createsend php library from github.'),
+    'arguments' => array(
+      'path' => dt('Optional. Download the library to an alternative path.'),
+    ),
+    'aliases' => array('dlcreatesend'),
+    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
+  );
+
+  return $items;
+}
+
+
+
+/**
+ * Implementation of hook_drush_help().
+ *
+ * This function is called whenever a drush user calls
+ * 'drush help <name-of-your-command>'. This hook is optional. If a command
+ * does not implement this hook, the command's description is used instead.
+ *
+ * This hook is also used to look up help metadata, such as help
+ * category title and summary.  See the comments below for a description.
+ *
+ * @param
+ *   A string with the help section (prepend with 'drush:')
+ *
+ * @return
+ *   A string with the help text for your command.
+ */
+function campaignmonitor_drush_help($section) {
+  switch ($section) {
+    case 'drush:download-createsend-lib':
+      return dt("Downloads the Createsend php library from github.");
+  }
+}
+
+/**
+ * Example drush command callback. This is where the action takes place.
+ *
+ * The function name should be same as command name but with dashes turned to
+ * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
+ * taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'.
+ * Note also that a simplification step is also done in instances where
+ * the commandfile name is the same as the beginning of the command name,
+ * "drush_example_example_foo" is simplified to just "drush_example_foo".
+ * To also implement a hook that is called before your command, implement
+ * "drush_hook_pre_example_foo".  For a list of all available hooks for a
+ * given command, run drush in --debug mode.
+ *
+ * If for some reason you do not want your hook function to be named
+ * after your command, you may define a 'callback' item in your command
+ * object that specifies the exact name of the function that should be
+ * called.  However, the specified callback function must still begin
+ * with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute")
+ * if you want that all hook functions are still called (e.g.
+ * drush_example_pre_foo_execute, and so on).
+ *
+ * In this function, all of Drupal's API is (usually) available, including
+ * any functions you have added in your own modules/themes.
+ *
+ * @see drush_invoke()
+ * @see drush.api.php
+ */
+function campaignmonitor_download_createsend_lib($path = '') {
+ if (!drush_shell_exec('type unzip')) {
+    return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
+  }
+
+  $args = func_get_args();
+  if (!empty($args[0])) {
+    $path = $args[0];
+  }
+  else {
+    $path = 'sites/all/libraries';
+  }
+
+  // Create the path if it does not exist.
+  if (!is_dir($path)) {
+    drush_op('mkdir', $path);
+    drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
+  }
+
+  // Set the directory to the download location.
+  $olddir = getcwd();
+  chdir($path);
+
+  $dirname  = 'campaignmonitor';
+  $filename = basename(CREATESEND_DOWNLOAD_URI);
+
+  // Remove any existing Createsend library directory
+  if (is_dir($dirname)) {
+    drush_log(dt('A existing Createsend library was overwritten at @path', array('@path' => $path)), 'notice');
+  }
+  // Remove any existing Createsend library zip archive
+  if (is_file($filename)) {
+    drush_op('unlink', $filename);
+  }
+  
+  // Download the zip archive
+  if (!drush_shell_exec('wget ' . CREATESEND_DOWNLOAD_URI)) {
+    drush_shell_exec('curl -O ' . CREATESEND_DOWNLOAD_URI);
+  }
+
+  if (is_file($filename)) {
+    // Decompress the zip archive
+    drush_shell_exec('unzip -qq -o ' . $filename . '; mv '. CREATESEND_FOLDER . ' ' . $dirname);
+    // Remove the zip archive
+    drush_op('unlink', $filename);
+  }
+
+  // Set working directory back to the previous working directory.
+  chdir($olddir);
+
+  if (is_dir($path . '/' . $dirname)) {
+    drush_log(dt('Createsend library has been downloaded to @path', array('@path' => $path)), 'success');
+  }
+  else {
+    drush_log(dt('Drush was unable to download the Createsend library to @path', array('@path' => $path)), 'error');
+  }
+}
\ No newline at end of file
-- 
1.7.5.4

