#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
#
# 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; either version 2, or (at your option)
# any later version.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.


use DBI;
use OMA;

$db=OMA::opendb();
$st=$db->prepare("select * from node");
$st->execute();
$i=0;
while(@row=$st->fetchrow()) {
	$nodes[$i] = $row[0];
	$prevs[$i++] = $row[2];
}
$st->finish();

for($i=0;$i<=$#nodes;$i++) {
	$newsid = 0;
	if($nodes[$i] > 1) {
		$newsid = $nodes[$i] * 1000 + 1;
	}
	if($nodes[$i]  == 1) {
		$newsid = 1;
	}
	$prev=0;
	if($prevs[$i] > 1) {
		$prev = $prevs[$i] * 1000 + 1;
	}
	if($prevs[$i] == 1) {
		$prev = 1;
	}

	print "update node set sid=$newsid, prev_sid=$prev  where sid=$nodes[$i];\n";
	print "update item set node_sid=$newsid where node_sid=$nodes[$i];\n";
	print "update template set node_sid=$newsid where node_sid=$nodes[$i];\n";
	print "update treemap set sid=$newsid where sid=$nodes[$i];\n\n\n";
}

$st=$db->prepare("select * from item order by sid desc");
$st->execute();
$i=0;
while(@row=$st->fetchrow()) {
	$items[$i] = $row[0];
	$link[$i++] = $row[4];
}
$st->finish();

for($i=0;$i<=$#items;$i++) {
	$newitem = $items[$i] * 1000 + 1;
	$linkitem=0;
	if($link[$i] > 0) {
		$linkitem = $link[$i] * 1000 + 1;
	}

	$st2=$db->prepare("select * from audio_conn where item_sid=$items[$i]");
	$st2->execute();
	while(@row2=$st2->fetchrow()) {
		$linked = $row2[4] * 1000 + 1;
    	print "update audio_conn set linked=$linked where item_sid=$row2[0];\n";
	}
	$st2->finish();

	$st2=$db->prepare("select * from text_conn where item_sid=$items[$i]");
	$st2->execute();
	while(@row2=$st2->fetchrow()) {
		$linked = $row2[4] * 1000 + 1;
    	print "update text_conn set linked=$linked where item_sid=$row2[0];\n";
	}
	$st2->finish();

	$st2=$db->prepare("select * from video_conn where item_sid=$items[$i]");
	$st2->execute();
	while(@row2=$st2->fetchrow()) {
		$linked = $row2[4] * 1000 + 1;
    	print "update video_conn set linked=$linked where item_sid=$row2[0];\n";
	}
	$st2->finish();

	$st2=$db->prepare("select * from picture_conn where item_sid=$items[$i]");
	$st2->execute();
	while(@row2=$st2->fetchrow()) {
		$linked = $row2[4] * 1000 + 1;
    	print "update picture_conn set linked=$linked where item_sid=$row2[0];\n";
	}
	$st2->finish();

    print "update audio_conn set item_sid=$newitem where item_sid=$items[$i];\n";
    print "update text_conn set item_sid=$newitem where item_sid=$items[$i];\n";
    print "update picture_conn set item_sid=$newitem where item_sid=$items[$i];\n";
    print "update video_conn set item_sid=$newitem where item_sid=$items[$i];\n";

    print "update item set link_item=$linkitem where sid=$items[$i];\n";
    print "update item set sid=$newitem where sid=$items[$i];\n";
}

