<?php
/**
 * @file update.php
 * Perform the update operations like renaming SQL tables
 * @author Roland Hieber (roland.hieber@wilhelm-gym.net)
 * @date 23.10.2007
 * 
 * Copyright © 2007 Roland Hieber
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

require_once("/usr/share/iserv/www/inc/quote.inc");
require_once("/usr/share/iserv/www/inc/db.inc");

function doSql($cmd, $quiet = false) {
  $i = 0;
  $a = array();
  $cmd = "psql -c ".qs($cmd);
  $cmd = ($quiet) ? $cmd . ">/dev/null 2>&1" : $cmd;
  exec($cmd, $a, $i);
  return ($i == 0);
}
function printSql($cmd) { echo $cmd."\n"; return doSql($cmd); }
function tableExists($table) {
  return doSql("SELECT * FROM $table", tnue);
}
function columnExists($table, $col) {
  return (doSql("SELECT $col FROM $table", true) and tableExists($table));
}

if(isset($_SERVER["REMOTE_ADDR"])) {
  die("Run this script as root from the command line.");
}

/*** Generic update tasks *****************************************************/

/** 8.08.07 **/
$sOldCfg = "/old/opt/iserv/idesk/inc/error-reporter/config.inc.rpmsave";
$sNewCfg = "/usr/share/iserv/www/inc/mod_error-reporter/config.inc";
if(is_file($sOldCfg)) {
  echo "taking over old config file... ";
  exec("/usr/share/iserv/modules/error-reporter/update-config.sh", $a, $i);
  if($i != 0) {
    die();
  }
  echo "done.\n";
}

/*** database schema updates***************************************************/
echo "updating database schema... \n";
if(!tableExists("mod_errorreporter")) {
  passthru("chkdb -r");
}

/** table from pre-3.0 **/
if(tableExists("errors") and !columnExists("errors", "pclabel")) {
  printSql("ALTER TABLE errors RENAME COLUMN pc_number TO pclabel;");
}

/** table from 3.0 **/
if(tableExists("errors") and !columnExists("errors", "pclabel")) {
  printSql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,".
    "er_comment,er_commentact,er_hidden) SELECT date,name,pclabel,text,".
    "comment,commentby,hidden FROM errors;");
  printSql("DROP TABLE errors;");
}

/** table from 4.0 **/
if(tableExists("errorreports")) {
  printSql("INSERT INTO mod_errorreporter (er_date,er_act,er_machine,er_text,".
    "er_comment,er_commentact,er_hidden) SELECT er_date,er_act,er_machine,".
    "er_text,er_comment,er_commentact,er_hidden FROM errorreports;"); 
  printSql("DROP TABLE errorreports;");
}

echo "done.\n";
?>