Index: /extensions/SmartAlbums/include/install.inc.php
===================================================================
--- /extensions/SmartAlbums/include/install.inc.php	(revision 17716)
+++ /extensions/SmartAlbums/include/install.inc.php	(revision 17716)
@@ -0,0 +1,75 @@
+<?php
+if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
+
+function smart_albums_install() 
+{
+  global $conf, $prefixeTable;
+  
+  // configuration
+  if (!isset($conf['SmartAlbums']))
+  {
+    $smart_default_config = serialize(array(
+      'update_on_upload' => false,
+      'show_list_messages' => true,
+      'smart_is_forbidden' => true,
+      ));
+    
+    conf_update_param('SmartAlbums', $smart_default_config);
+    $conf['SmartAlbums'] = $smart_default_config;
+  }
+  else
+  {
+    $new_conf = unserialize($conf['SmartAlbums']);
+    // new param in 2.0.2
+    if (!isset($new_conf['smart_is_forbidden']))
+    {
+      $new_conf['smart_is_forbidden'] = true;
+      conf_update_param('SmartAlbums', serialize($new_conf));
+      $conf['SmartAlbums'] = serialize($new_conf);
+    }
+  }
+  
+  // new table
+	pwg_query(
+'CREATE TABLE IF NOT EXISTS `' . $prefixeTable . 'category_filters` (
+  `category_id` smallint(5) unsigned NOT NULL,
+  `type` varchar(16) NOT NULL,
+  `cond` varchar(16) NULL,
+  `value` text
+) ENGINE=MyISAM DEFAULT CHARSET=utf8
+;');
+  
+  // new column on image category table
+  $result = pwg_query('SHOW COLUMNS FROM `' . IMAGE_CATEGORY_TABLE . '` LIKE "smart";');
+  if (!pwg_db_num_rows($result))
+  {      
+    pwg_query('ALTER TABLE `' . IMAGE_CATEGORY_TABLE . '` ADD `smart` ENUM(\'true\', \'false\') NOT NULL DEFAULT \'false\';');
+  }
+  
+  // date filters renamed in 2.0
+  $query = '
+SELECT category_id
+  FROM `' . $prefixeTable . 'category_filters`
+  WHERE 
+    type = "date" AND
+    cond IN ("the","before","after","the_crea","before_crea","after_crea")
+;';
+
+  if (pwg_db_num_rows(pwg_query($query)))
+  {
+    $name_changes = array(
+      'the' => 'the_post',
+      'before' => 'before_post',
+      'after' => 'after_post',
+      'the_crea' => 'the_taken',
+      'before_crea' => 'before_taken',
+      'after_crea' => 'after_taken',
+      );
+    foreach ($name_changes as $old => $new)
+    {
+      pwg_query('UPDATE `' . $prefixeTable . 'category_filters` SET cond = "'.$new.'" WHERE cond = "'.$old.'";');
+    }
+  }
+}
+
+?>
Index: /extensions/SmartAlbums/main.inc.php
===================================================================
--- /extensions/SmartAlbums/main.inc.php	(revision 17715)
+++ /extensions/SmartAlbums/main.inc.php	(revision 17716)
@@ -12,18 +12,61 @@
 global $prefixeTable;
 
-define('SMART_PATH', PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
+define('SMART_PATH',    PHPWG_PLUGINS_PATH . 'SmartAlbums/');
 define('CATEGORY_FILTERS_TABLE', $prefixeTable . 'category_filters');
-define('SMART_ADMIN', get_root_url() . 'admin.php?page=plugin-' . basename(dirname(__FILE__)));
+define('SMART_ADMIN',   get_root_url() . 'admin.php?page=plugin-SmartAlbums');
+define('SMART_VERSION', '2.0.3');
+
 
 add_event_handler('invalidate_user_cache', 'smart_make_all_associations');
 add_event_handler('init', 'smart_init');
 
+if (defined('IN_ADMIN'))
+{
+  include_once(SMART_PATH.'include/cat_list.php');
+  add_event_handler('loc_begin_cat_list', 'smart_cat_list');
+  add_event_handler('tabsheet_before_select','smart_tab', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); 
+  add_event_handler('get_admin_plugin_menu_links', 'smart_admin_menu');
+  add_event_handler('delete_categories', 'smart_delete_categories');
+}
+
 include_once(SMART_PATH.'include/functions.inc.php');
 
+
+/**
+ * update plugin & unserialize conf & load language
+ */
 function smart_init()
 {
-  global $conf;
+  global $conf, $pwg_loaded_plugins;
   
-  load_language('plugin.lang', SMART_PATH);
+  if (
+    $pwg_loaded_plugins['SmartAlbums']['version'] == 'auto' or
+    version_compare($pwg_loaded_plugins['SmartAlbums']['version'], SMART_VERSION, '<')
+  )
+  {
+    include_once(SMART_PATH . 'include/install.inc.php');
+    smart_albums_install();
+    
+    if ($pwg_loaded_plugins['SmartAlbums']['version'] != 'auto')
+    {
+      $query = '
+UPDATE '. PLUGINS_TABLE .'
+SET version = "'. SMART_VERSION .'"
+WHERE id = "SmartAlbums"';
+      pwg_query($query);
+      
+      $pwg_loaded_plugins['SmartAlbums']['version'] = SMART_VERSION;
+      
+      if (defined('IN_ADMIN'))
+      {
+        $_SESSION['page_infos'][] = 'Smart Albums updated to version '. SMART_VERSION;
+      }
+    }
+  }
+  
+  if (defined('IN_ADMIN'))
+  {
+    load_language('plugin.lang', SMART_PATH);
+  }
   $conf['SmartAlbums'] = unserialize($conf['SmartAlbums']);
   
@@ -33,15 +76,9 @@
     include_once(SMART_PATH.'include/page_items.php');
   }
-  else if (script_basename() == 'admin')
-  {
-    include_once(SMART_PATH.'include/cat_list.php');
-    
-    add_event_handler('loc_begin_cat_list', 'smart_cat_list');
-    add_event_handler('tabsheet_before_select','smart_tab', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); 
-    add_event_handler('get_admin_plugin_menu_links', 'smart_admin_menu');
-    add_event_handler('delete_categories', 'smart_delete_categories');
-  }
 }
 
+/**
+ * new tab on album properties page
+ */
 function smart_tab($sheets, $id)
 {
@@ -61,4 +98,8 @@
 }
 
+
+/**
+ * admin plugins menu link
+ */
 function smart_admin_menu($menu) 
 {
Index: /extensions/SmartAlbums/maintain.inc.php
===================================================================
--- /extensions/SmartAlbums/maintain.inc.php	(revision 17715)
+++ /extensions/SmartAlbums/maintain.inc.php	(revision 17716)
@@ -2,69 +2,25 @@
 if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
 
-global $prefixeTable;
-define('smart_table', $prefixeTable . 'category_filters');
-
-define('smart_default_config', serialize(array(
-    'update_on_upload' => false,
-    'show_list_messages' => true,
-    'smart_is_forbidden' => true,
-    )));
+include_once(PHPWG_PLUGINS_PATH . 'SmartAlbums/include/install.inc.php');
 
 function plugin_install() 
 {
-  /* create table to store filters */
-	pwg_query(
-'CREATE TABLE IF NOT EXISTS `' . smart_table . '` (
-  `category_id` smallint(5) unsigned NOT NULL,
-  `type` varchar(16) NOT NULL,
-  `cond` varchar(16) NULL,
-  `value` text
-) ENGINE=MyISAM DEFAULT CHARSET=utf8
-;');
-  
-  /* add a collumn to image_category_table */
-  pwg_query('ALTER TABLE `' . IMAGE_CATEGORY_TABLE . '` ADD `smart` ENUM(\'true\', \'false\') NOT NULL DEFAULT \'false\';');
-      
-  /* config parameter */
-  conf_update_param('SmartAlbums', smart_default_config);
+  smart_albums_install();
+  define('smart_albums_installed', true);
 }
 
 function plugin_activate()
 { 
-  global $conf;
-  
-  if (!isset($conf['SmartAlbums']))
+  if (!defined('smart_albums_installed'))
   {
-    conf_update_param('SmartAlbums', smart_default_config);
-  }
-  else
-  {
-    $new_conf = unserialize($conf['SmartAlbums']);
-    // new param in 2.0.2
-    if (!isset($new_conf['smart_is_forbidden']))
-    {
-      $new_conf['smart_is_forbidden'] = true;
-      conf_update_param('SmartAlbums', smart_default_config);
-    }
-  }
-  
-  // some filters renamed in 2.0
-  $name_changes = array(
-    'the' => 'the_post',
-    'before' => 'before_post',
-    'after' => 'after_post',
-    'the_crea' => 'the_taken',
-    'before_crea' => 'before_taken',
-    'after_crea' => 'after_taken',
-    );
-  foreach ($name_changes as $old => $new)
-  {
-    pwg_query('UPDATE ' . smart_table . ' SET cond = "'.$new.'" WHERE cond = "'.$old.'";');
+    smart_albums_install();
   }
 }
 
 function plugin_uninstall() 
-{  
-  pwg_query('DROP TABLE `' . smart_table . '`;');
+{
+  global $prefixeTable;
+  
+  pwg_query('DROP TABLE `' . $prefixeTable . 'category_filters`;');
   pwg_query('ALTER TABLE `' . IMAGE_CATEGORY_TABLE . '` DROP `smart`;');
   pwg_query('DELETE FROM `' . CONFIG_TABLE . '` WHERE param = \'SmartAlbums\' LIMIT 1;');
