<?php
// $Id: image.install,v 1.1.2.4 2007/02/09 04:03:23 walkah Exp $

/**
 * Installing and updating image.module.
 */

function image_install() {
  //Nothing to do.
}

/**
 * Update 4.5 to 4.6 or later.
 */
function image_update_1() {
  if (db_table_exists('image')) {
    if ($result = db_query("SELECT * FROM {image}")) {
      $fields = array('thumb_path' => 'thumbnail',
                      'preview_path' => 'preview',
                      'image_path' => '_original');
      
      while ($old_image = db_fetch_object($result)) {
        foreach ($fields as $old => $new) {
          $old_file = '';
          if (file_exists($old_image->$old)) {
            $old_file = $old_image->$old;
          } else {
            $old_file = file_create_path($old_image->$old);
          }
          if ($old_file && $old_image->$old != '' && db_num_rows(db_query("SELECT fid FROM {files} WHERE nid=%d and filename='%s'", $old_image->nid, $new)) == 0) {
            _image_insert($old_image, $new, $old_file);
          }
        }
      }
    }
  }
  return array();
}

if (!function_exists('db_table_exists')) {
  function db_table_exists($table) {
    global $db_type;
    
    switch($db_type) {
      case 'mysql':
      case 'mysqli':
        return db_num_rows(db_query("SHOW TABLES LIKE '{" . db_escape_table($table) . "}'"));
        break;
      case 'pgsql':
        return db_num_rows(db_query("SELECT relname FROM pg_class WHERE relname = '{" . db_escape_table($table) . "}'"));
        break;
    }
  }
}