function prepareDirectory($directory) {
    // Because we may have an absolute path whose base folders are not writable
    // we can't use the standard recursive D6 pattern.
    $pieces = explode('/', $directory);

    // Work up through the folder's parentage until we find a directory that exists.
    // (Or in other words, backwards in the array of pieces.)
    $length = count($pieces);
    for ($i = 0; $i < $length; $i++) {
      //print $pieces[$length - $i];
      $slice = array_slice($pieces, 0, $length - $i);
      $path_slice = implode('/', $slice);
      if (file_exists($path_slice)) {
        $status = file_check_directory($path_slice, FILE_CREATE_DIRECTORY);
        break;
      }
    }

    // If we go right the way along to the base and still can't create a directory...
    if ($i == $length) {
      throw new \Exception(strtr("The directory !path cannot be created or is not writable", array(
        '!path' => htmlspecialchars($path_slice, ENT_QUOTES, 'UTF-8'),
      )));
    }
    // print "status: $status for $path_slice - i: $i\n";

    // Now work back down (or in other words, along the array of pieces).
    for ($j = $length - $i; $j < $length; $j++) {
      $slice[] = $pieces[$j];
      $path_slice = implode('/', $slice);
      //print "$path_slice\n";
      $status = file_check_directory($path_slice, FILE_CREATE_DIRECTORY);
    }

    if (!$status) {
      throw new \Exception("The hooks directory cannot be created or is not writable.");
    }
  }