<?php
// $Id$

function mapi_derivatives_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
        // media derivatives definitions
        db_query("CREATE TABLE {mapi_derivatives} (
          did int unsigned NOT NULL default '0',
          parent int unsigned NOT null default '0',
          name varchar(128) NOT NULL default '',
          exts text NOT NULL,
          PRIMARY KEY (did)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");

        // media transformations
        db_query("CREATE TABLE {mapi_transforms} (
          tid int unsigned NOT NULL default '0',
          did int unsigned NOT NULL default '0',
          name varchar(128) NOT NULL default '',
          weight int unsigned NOT NULL default '0',
          settings text,
          PRIMARY KEY (tid),
          KEY did (did)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");

				db_query("CREATE TABLE {mapi_generated} (
					source text default '',
          style varchar(128) NOT NULL default '',
          dest text default '',
					dimensions text,
					KEY source (source(255))
				) /*!40100 DEFAULT CHARACTER SET utf8 */;");

      break;
    case 'pgsql':
      break;
  }
}

function mapi_derivatives_uninstall() {
  // remove all the generated files.
  $results = db_query("SELECT * FROM {mapi_generated}");
  while ($object = db_fetch_object($results)) {
    if (is_file($object->dest)) {
      @unlink($object->dest);
    }
  }

  db_query("DROP TABLE {mapi_derivatives}");
  db_query("DROP TABLE {mapi_transforms}");
	db_query("DROP TABLE {mapi_generated}");

  // delete the variables
  $results = db_query("SELECT name FROM {variable} WHERE name LIKE 'mapi_ext_%_page' OR name LIKE 'mapi_ext_%_teaser' OR name LIKE 'mapi_ext_%_full'");
  while ($object = db_fetch_object($results)) {
    variable_del($object->name);
  }
}
