<?php
  /*

      upgrade.php

      Upgrades database to the newest version

      Changelog:
        2005-07-15 arne_sf: Buffers output in array instead of displaying it immediately
        2005-07-15 arne_sf: Added support for v0.9.3 (including database schema changes by Stephan Kaufhold)
        2005-07-02 arne_sf: Added support for v0.9.2
        2005-04-30 arne_sf: Made upgrade page conform to page standard (include header, menu a.s.o.)
                            Extended script to handle upgrade code for upgrading several versions to the newest
        2005-04-27 arne_sf: Initial work on upgrade script, new function rebuild_sequence
        
      ----

      Copyright (C) 2005  Arne (arne_sf@users.sourceforge.net)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */
    
        
    // rebuild_sequence: Synchronize a sequence with the database contents
    function rebuild_sequence($table, $field, $sequence)
    {
        global $db;
        
        // Find current value to synchronize sequence to
        $id =& $db->getOne("SELECT $field FROM $table ORDER BY $field DESC LIMIT 1");

        // Check that result is not an error
        if (PEAR::isError($id)) return $id->getMessage();

        // Drop existing sequence (if it exists, otherwise ignore error and continue)
        $res = $db->dropSequence($sequence);

        // Rebuild sequence
        while ($db->nextId($sequence) < $id)
            ;
        
        return TRUE;
    }

    
    // updateversion: Updates the version field in the database
    function updateversion($newversion)
    {
        // Update version field
        global $db;
        $res = $db->query("UPDATE tbl_System_Preferences SET value='$newversion' WHERE identifier='version'");
        return $res;
    }

        
    // Array of supported database backends
    $DBMS = array('pgsql', 'mysql', 'mssql');
    
    // Only continue for supported database backends
    if (in_array($db->phptype, $DBMS))
    {
        // Determine version to upgrade to
        switch ($global_prefs['version'])
        {
            case '0.9':
                // From official release v0.9
                $output[] = "<div class=\"maindark\">
                <p>Upgrade from v" . $global_prefs['version'] . " to " . SCRIPTVERSION . "</p>
                </div>
                <div class=\"main\">
                <p>";

                include 'system/upgrade.0.9.1.inc.php';
                include 'system/upgrade.0.9.2.inc.php';
                include 'system/upgrade.0.9.3.inc.php';
                include 'system/upgrade.0.9.9.inc.php';
//                include 'system/upgrade.1.0.inc.php';
                $res = updateversion(SCRIPTVERSION);
                $output[] = "Updating version field to " . SCRIPTVERSION . " ....... " . (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";

                $output[] = "</p>\n";
                break;

            case '0.9.1':
                // From development version v0.9.1
                $output[] = "<div class=\"maindark\">
                <p>Upgrade from v" . $global_prefs['version'] . " to " . SCRIPTVERSION . "</p>
                </div>
                <div class=\"main\">
                <p>";

                include 'system/upgrade.0.9.2.inc.php';
                include 'system/upgrade.0.9.3.inc.php';
                include 'system/upgrade.0.9.9.inc.php';
//                include 'system/upgrade.1.0.inc.php';
                $res = updateversion(SCRIPTVERSION);
                $output[] = "Updating version field to " . SCRIPTVERSION . " ....... " . (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";
                
                $output[] = "</p>\n";
                break;

            case '0.9.2':
                // From development version v0.9.2
                $output[] = "<div class=\"maindark\">
                <p>Upgrade from v" . $global_prefs['version'] . " to " . SCRIPTVERSION . "</p>
                </div>
                <div class=\"main\">
                <p>";

                include 'system/upgrade.0.9.3.inc.php';
                include 'system/upgrade.0.9.9.inc.php';
//                include 'system/upgrade.1.0.inc.php';
                $res = updateversion(SCRIPTVERSION);
                $output[] = "Updating version field to " . SCRIPTVERSION . " ....... " . (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";

                $output[] = "</p>\n";
                break;

            case '0.9.3':
                // From development version v0.9.3
                $output[] = "<div class=\"maindark\">
                <p>Upgrade from v" . $global_prefs['version'] . " to " . SCRIPTVERSION . "</p>
                </div>
                <div class=\"main\">
                <p>";

                include 'system/upgrade.0.9.9.inc.php';
//                include 'system/upgrade.1.0.inc.php';
                $res = updateversion(SCRIPTVERSION);
                $output[] = "Updating version field to " . SCRIPTVERSION . " ....... " . (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";

                $output[] = "</p>\n";
                break;

            case '0.9.9':
                // From development version v0.9.9
                $output[] = "<div class=\"maindark\">
                <p>Upgrade from v" . $global_prefs['version'] . " to " . SCRIPTVERSION . "</p>
                </div>
                <div class=\"main\">
                <p>";

//                include 'system/upgrade.1.0.inc.php';
                $res = updateversion(SCRIPTVERSION);
                $output[] = "Updating version field to " . SCRIPTVERSION . " ....... " . (!PEAR::isError($res) ? "Success!" : $res->getMessage()) . "<BR />\n";

                $output[] = "</p>\n";
                break;

            case SCRIPTVERSION:
                // Nothing to upgrade, system already up to date        
                $output[] = "<div class=\"maindark\">
                <p>The system is already up-to-date (version " . $global_prefs['version'] . "). Nothing to do.</p>";
                break;

            default:
                // Current version not recognized, do nothing
                $output[] = "<div class=\"maindark\">
                <p>Installed version not supported by upgrade routine, aborting upgrade.</p>";
                break;
        }
    } else {
        // Database backend not supported by upgrade routine, abort upgrade!
        $output[] = "<div class=\"maindark\">
        <p>ERROR: Database system " . $db->phptype . " not yet supported by upgrade routine, aborting upgrade.</p>";
    }
    
    $output[] = "</div>";
    
