Index: trunk/bin/init_codebase_tables.cli.php
===================================================================
--- trunk/bin/init_codebase_tables.cli.php	(revision 572)
+++ trunk/bin/init_codebase_tables.cli.php	(revision 573)
@@ -24,18 +24,7 @@
 *
 * @author   Quinn Comendant <quinn@strangecode.com>
-* @version  1.0
-* @since    12 Jul 2015 22:44:58
+* @version  2.0
+* @since    05 Mar 2016 19:12:09
 */
-
-/********************************************************************
-* CONFIG
-********************************************************************/
-
-require_once dirname(__FILE__) . '/_config.inc.php';
-require_once CODEBASE_PATH . '/lib/ACL.inc.php';
-require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
-require_once CODEBASE_PATH . '/lib/Lock.inc.php';
-require_once CODEBASE_PATH . '/lib/Prefs.inc.php';
-require_once CODEBASE_PATH . '/lib/Version.inc.php';
 
 /********************************************************************
@@ -44,7 +33,7 @@
 
 // Process command line options.
-$opt = getopt('fh');
+$opt = getopt('fhc:');
 
-// Get class name.
+// Get name of class to initialize (or 'all' for all).
 $class = strtolower(end($_SERVER['argv']));
 
@@ -55,5 +44,9 @@
 }
 
-$app->logMsg(sprintf('Running initDB on %s codebase tables in db %s', $class, $db->getParam('db_name')), LOG_INFO, __FILE__, __LINE__);
+if (isset($opt['c']) && is_file($opt['c'])) {
+    require_once $opt['c'];
+    $app->logMsg(sprintf('Loaded custom config file: %s', $opt['c']), LOG_INFO, __FILE__, __LINE__);
+}
+require_once dirname(__FILE__) . '/_config.inc.php';
 
 if ('all' == $class) {
@@ -63,8 +56,11 @@
     initClassDB('prefs');
     initClassDB('version');
+    initClassDB('session');
 } else {
     initClassDB($class);
 }
 
+$app->stop();
+die;
 
 /********************************************************************
@@ -92,6 +88,7 @@
 OPTIONS
 
-    -f  Force recreation of tables, if they already exist.
-    -h  Show this help message.
+    -c FILE Path to include custom site _config.inc.php file.
+    -f      Force recreation of tables, if they already exist. WARNING: this will delete existing records.
+    -h      Show this help message.
 
 CLASSNAME is one of:
@@ -103,4 +100,5 @@
     prefs       Create tables for Prefs
     version     Create tables for Version
+    session     Create tables for DBSessionHandler
 <?php
 }
@@ -119,8 +117,13 @@
 {
     global $opt;
+    $app =& App::getInstance();
+    $db =& DB::getInstance();
+
+    $app->logMsg(sprintf('Running %s->initDB() on database %s', $class, $db->getParam('db_name')), LOG_INFO, __FILE__, __LINE__);
 
     switch ($class) {
         case 'acl':
             // ACL!
+            require_once CODEBASE_PATH . '/lib/ACL.inc.php';
             $acl =& ACL::getInstance();
             $acl->setParam(array('create_table' => true));
@@ -130,9 +133,11 @@
         case 'auth':
             // Auth_SQL!
+            require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
             $auth = new Auth_SQL('codebase');
-            $auth->setParam(array(
-                'create_table' => true,
-                'abuse_detection' => true,
-            ));
+            $auth->setParam(array('create_table' => true));
+            if (!isset($opt['c'])) {
+                // User didn't provide custom config. Use sane defaults.
+                $auth->setParam(array('abuse_detection' => true));
+            }
             $auth->initDB(isset($opt['f']));
             break;
@@ -140,4 +145,5 @@
         case 'lock':
             // Lock!
+            require_once CODEBASE_PATH . '/lib/Lock.inc.php';
             $lock =& Lock::getInstance(new Auth_SQL('codebase'));
             $lock->setParam(array('create_table' => true));
@@ -147,4 +153,5 @@
         case 'prefs':
             // Prefs!
+            require_once CODEBASE_PATH . '/lib/Prefs.inc.php';
             $prefs = new Prefs('codebase');
             $prefs->setParam(array('create_table' => true));
@@ -154,4 +161,5 @@
         case 'version':
             // Version!
+            require_once CODEBASE_PATH . '/lib/Version.inc.php';
             $version = Version::getInstance(new Auth_SQL('codebase'));
             $version->setParam(array('create_table' => true));
@@ -159,6 +167,29 @@
             break;
 
+        case 'session':
+            // DBSessionHandler!
+
+            // We need to hack the app to allow sessions to run in a CLI.
+            $app->stop();
+            $app->cli = false;
+            $app->setParam(array('enable_session' => true));
+            $app->start();
+
+            if (!isset($opt['c'])) {
+                // User didn't provide custom config. Use sane defaults.
+                require_once CODEBASE_PATH . '/lib/DBSessionHandler.inc.php';
+                $db_session = new DBSessionHandler($app->db, array(
+                    'db_table' => 'session_tbl',
+                    'create_table' => true,
+                ));
+                $db_session->initDB(isset($opt['f']));
+            } else if (true === $app->getParam('enable_db_session_handler') && true === $app->getParam('enable_db') && isset($app->db_session)) {
+                // Only init if db_session is enabled in config.
+                $app->db_session->initDB(isset($opt['f']));
+            }
+            break;
+
         default:
-            echo "The class $class is not setup to initClassDB.\n";
+            echo "The class $class is not setup to initClassDB().\n";
             exit(1);
     }
