diff --git a/cdn.install b/cdn.install
index bd91f68..2e3352e 100644
--- a/cdn.install
+++ b/cdn.install
@@ -1,31 +1,47 @@
 <?php
+// $Id: cdn.install,v 1.9 2011/01/19 09:29:04 wimleers Exp $
 
 /**
  * @file
- * Install, update and uninstall functions for the CDN module.
+ * Install file.
  */
 
 
 /**
- * Implements hook_uninstall().
+ * Implementation of hook_install().
+ */
+function cdn_install() {
+  // CDN must run after JavaScript aggregator (weight 9999) in fallback mode.
+  db_query("UPDATE {system} SET weight = 10000 WHERE name = 'cdn'");
+  drupal_rebuild_theme_registry();
+}
+
+/**
+ * Implementation of hook_uninstall().
  */
 function cdn_uninstall() {
   db_query("DELETE FROM {variable} WHERE name LIKE 'cdn_%%'");
 }
 
 /**
- * Implements hook_requirements().
+ * Implementation of hook_requirements().
  */
 function cdn_requirements($phase) {
-  module_load_include('module', 'cdn');
-  
   $requirements = array();
   $t = get_t();
 
+  // We can't use module_load_include() when we're in the install phase (i.e.
+  // when the module is being installed through an install profile).
+  include_once dirname(__FILE__) . '/cdn.requirements.inc';
+
+  // Detect whether the fallback should be enabled or not. Next, get the
+  // integration that's being used: fallback, PressFlow or core patch.
+  _cdn_requirements_detect_fallback();
+  $integration_method = _cdn_requirements_get_integration_mechanism();
+
   switch ($phase) {
-    case 'install' :
     case 'runtime' :
-      cdn_load_include('requirements');
+      module_load_include('module', 'cdn');
 
       // CDN  status.
       $status = variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED);
@@ -148,9 +164,61 @@ function cdn_requirements($phase) {
             $requirements['cdn']['severity'] = REQUIREMENT_ERROR;
           }
 
-          $requirements['cdn']['description'] .= '<br />' . theme('item_list', array('items' => $items));
+          $requirements['cdn']['description'] .= '<br />' . theme('item_list', $items);
         }
       }
+
+      // Finally, add very brief info about the integration mechanism in use.
+      switch ($integration_method) {
+        case 'fallback':
+          $requirements['cdn']['value'] .= ' — ' . $t('Fallback mechanism');
+          $requirements['cdn']['description'] .= '<br /><br />' . t('If you want complete CDN coverage, you should either apply the included Drupal core patch or switch to <a href="http://pressflow.org/">Pressflow</a>.');
+          break;
+        case 'pressflow':
+          $requirements['cdn']['value'] .= ' — ' . $t('Pressflow');
+          break;
+        case 'core patch':
+          $requirements['cdn']['value'] .= ' — ' . $t('Core patch');
+          break;
+      }
+    case 'install' :
+      // Only check if the CDN Drupal core patch is properly applied if the
+      // integration methis is *not* 'pressflow'. Show as a warning when the
+      // integration method is 'fallback'.
+      if (!_cdn_requirements_is_pressflow()) {
+        _cdn_requirements_generate_requirement_for_patch(
+          $requirements,
+          'core',
+          ($integration_method == 'fallback') ? $t('CDN — Drupal core patch (for complete CDN coverage)') : $t('CDN — Drupal core patch'),
+          REQUIREMENT_WARNING
+        );
+      }
+
+      // Only check if the CDN ImageCache patch is properly applied if the
+      // integration method is *not* 'fallback' (thus either 'pressflow' or
+      // 'core patch') *and* when the ImageCache module is enabled.
+      if ($integration_method != 'fallback' && module_exists('imagecache')) {
+        
+        //Imagecahce 6.x-2.0-beta12 and newer doesn't need a patch. Check to see if the fuction file_create_url() is in the module file.
+        if(!_cdn_requirements_check_file_url_alter()){
+        
+          // Check if either of the patches is applied, then remove the
+          // requirements for the last one if neither patch is applied.
+          _cdn_requirements_generate_requirement_for_patch(
+            $requirements,
+            'imagecache',
+            $t('CDN — ImageCache patch')
+          );
+          _cdn_requirements_generate_requirement_for_patch(
+            $requirements,
+            'imagecache_6_2',
+            $t('CDN — ImageCache patch')
+          );
+          if ($requirements['cdn_imagecache_patch']['value'] == $requirements['cdn_imagecache_6_2_patch']['value']) {
+            unset($requirements['cdn_imagecache_6_2_patch']);
+          }
+        }  
+      }
   }
 
   return $requirements;
@@ -177,52 +245,54 @@ function cdn_update_6200() {
   variable_del('cdn_basic_url');
   variable_del('cdn_basic_extensions');
 
-  return t('Updated variables to allow for multiple Origin Pull CDN URLs instead of just one.');
+  $ret[] = array('success' => TRUE, 'query' => 'Updated variables to allow for multiple Origin Pull CDN URLs instead of just one.');
+
+  return array();
 }
 
 /**
  * More consistent variable names: upgrade path.
  */
 function cdn_update_6201() {
-  $cdn_exception_path_blacklist = variable_get('cdn_exclusion');
+  $ret = array();
+
+  $cdn_exception_path_blacklist = variable_get('cdn_exclusion', NULL);
   variable_del('cdn_exclusion');
   if (isset($cdn_exception_path_blacklist)) {
     variable_set('cdn_exception_path_blacklist', $cdn_exception_path_blacklist);
   }
 
-  $cdn_exception_auth_users_blacklist = variable_get('cdn_exclusion_logged_in');
+  $cdn_exception_auth_users_blacklist = variable_get('cdn_exclusion_logged_in', NULL);
   variable_del('cdn_exclusion_logged_in');
   if (isset($cdn_exception_auth_users_blacklist)) {
     variable_set('cdn_exception_auth_users_blacklist', $cdn_exception_auth_users_blacklist);
   }
 
-  return t('Updated variables (internal name changes only, for improved consistency).');
+  $ret[] = array('success' => TRUE, 'query' => 'Updated variables (internal name changes only, for improved consistency).');
+
+  return $ret;
 }
 
 /**
  * CDN must run after JavaScript aggregator (weight 9999) in fallback mode.
  */
 function cdn_update_6202() {
-  db_query("UPDATE {system} SET weight = 10000 WHERE name = 'cdn'");
+  $ret = array();
+
+  $ret[] = update_sql("UPDATE {system} SET weight = 10000 WHERE name = 'cdn'");
   // update.php runs drupal_rebuild_theme_registry();
 
-  return t('Ensured the CDN module runs after the JavaScript aggregator module.');
+  return $ret;
 }
 
 /**
  * More consistent variable names: upgrade path.
  */
 function cdn_update_6203() {
-  db_query("UPDATE {variable} SET name = 'cdn_exception_file_path_blacklist' WHERE name = 'cdn_exception_path_blacklist'");
-  db_query("UPDATE {variable} SET name = 'cdn_exception_file_path_whitelist' WHERE name = 'cdn_exception_path_whitelist'");
+  $ret = array();
 
-  return t('Updated variables (internal name changes only, for improved consistency).');
-}
+  $ret[] = update_sql("UPDATE {variable} SET name = 'cdn_exception_file_path_blacklist' WHERE name = 'cdn_exception_path_blacklist'");
+  $ret[] = update_sql("UPDATE {variable} SET name = 'cdn_exception_file_path_whitelist' WHERE name = 'cdn_exception_path_whitelist'");
 
-/**
- * Fallback mode is not needed in Drupal 7, hence also no more need for an
- * altered module weight.
- */
-function cdn_update_7200() {
-  db_query("UPDATE {system} SET weight = 0 WHERE name = 'cdn'");
+  return $ret;
 }
diff --git a/cdn.requirements.inc b/cdn.requirements.inc
index ab7b1b3..5993456 100644
--- a/cdn.requirements.inc
+++ b/cdn.requirements.inc
@@ -1,4 +1,5 @@
 <?php
+// $Id: cdn.requirements.inc,v 1.14 2011/01/19 09:29:04 wimleers Exp $
 
 /**
  * @file
@@ -38,7 +39,7 @@ function _cdn_requirements_generate_requirement_for_patch(&$requirements, $patch
   }
   else {
     $requirements[$key] += array(
-      'description' => $t('This patch has not been properly applied to the following files (or the patch has been updated):') . '<br />' . theme('item_list', array('items' => $unpatched_files)) . '<br />' . t('Please consult the installation instructions in the included README.txt.'),
+      'description' => $t('This patch has not been properly applied to the following files (or the patch has been updated):') . '<br />' . theme('item_list', $unpatched_files) . '<br />' . t('Please consult the installation instructions in the included README.txt.'),
       'severity'    => $not_properly_applied_severity,
       'value'       => $t('Not/incompletely applied patch or updated patch.'),
     );
@@ -154,3 +155,102 @@ function _cdn_requirements_generate_patterns_for_patch($full_path) {
 
   return $patterns;
 }
+
+/**
+ * Generates the patterns for the core patch.
+ */
+function _cdn_requirements_core_patch_patterns() {
+  return _cdn_requirements_generate_patterns_for_patch(dirname(__FILE__) . '/patches/drupal6.patch');
+}
+
+/**
+ * Generates the patterns for the ImageCache patch.
+ */
+function _cdn_requirements_imagecache_patch_patterns() {
+  return _cdn_requirements_generate_patterns_for_patch(dirname(__FILE__) . '/patches/imagecache.patch');
+}
+
+/**
+ * Generates the patterns for the ImageCache 6.2 branch patch.
+ */
+function _cdn_requirements_imagecache_6_2_patch_patterns() {
+  return _cdn_requirements_generate_patterns_for_patch(dirname(__FILE__) . '/patches/imagecache_6--2.patch');
+}
+
+
+/**
+ * Checks if the environment is Pressflow or not.
+ */
+function _cdn_requirements_is_pressflow() {
+  $profile = realpath('.') . '/profiles/default/default.profile';
+
+  if (!file_exists($profile)) {
+    return FALSE;
+  }
+
+  include_once $profile;
+  $default = default_profile_details();
+  return $default['name'] == 'Pressflow';
+}
+
+/**
+ * Detect whether the fallback mechanism (to alter file URLs, the best
+ * mechanism is the core patch, but a fallback to the theme layer is included)
+ * should be enabled or disabled, and actually enable/disable it, too.
+ */
+function _cdn_requirements_detect_fallback() {
+  // Check if the core patch is applied properly (i.e. no core files don't
+  // properly match the core patch). If it is properly applied, then the
+  // fallback will be (or remain) disabled, if not, then the fallback will be
+  // (or remain) enabled.
+  $fallback_currently_enabled = variable_get(CDN_THEME_LAYER_FALLBACK_VARIABLE, FALSE);
+  if (!_cdn_requirements_is_pressflow() && count(_cdn_requirements_check_patch_applied(_cdn_requirements_core_patch_patterns())) > 0) {
+    if (!$fallback_currently_enabled) {
+      variable_set(CDN_THEME_LAYER_FALLBACK_VARIABLE, TRUE);
+      drupal_rebuild_theme_registry();
+      drupal_set_message(t("Drupal CDN integration status changed: core patch not (properly) applied; fallback mechanism enabled."), 'warning');
+      watchdog('cdn', "Drupal CDN integration status changed: core patch not (properly) applied; fallback mechanism enabled.");
+    }
+  }
+  else {
+    if ($fallback_currently_enabled) {
+      variable_set(CDN_THEME_LAYER_FALLBACK_VARIABLE, FALSE);
+      drupal_rebuild_theme_registry();
+      drupal_set_message(t("Drupal CDN integration status changed: core patch properly applied; fallback mechanism disabled."), 'warning');
+      watchdog('cdn', "Drupal CDN integration status changed: core patch properly applied; fallback mechanism disabled.");
+    }
+  }
+}
+
+/**
+ * Get the integration mechanism that is currently being used by the CDN
+ * integration module.
+ *
+ * @return
+ *   One of the following: 'fallback', 'pressflow', 'core patch'.
+ */
+function _cdn_requirements_get_integration_mechanism() {
+  if (variable_get(CDN_THEME_LAYER_FALLBACK_VARIABLE, FALSE)) {
+    return 'fallback';
+  }
+  elseif (_cdn_requirements_is_pressflow()) {
+    return 'pressflow';
+  }
+  else {
+    return 'core patch';
+  }
+}
+
+/**
+ * Check to see if the imagecache module already implements file_create_url()
+ * 
+ * @return
+ *    True or False
+ */
+function _cdn_requirements_check_file_url_alter(){
+  $drupal_root = realpath('.');
+  $path = $drupal_root . '/' . drupal_get_path('module', 'imagecache') . '/imagecache.module'; 
+
+  return preg_match('/PressFlow/', file_get_contents($path));
+} 
+ 
