#!/usr/bin/perl


use lib '/opt/oma/reboot';
use REBOOTPARSE;
use REBOOT;
use lib '/opt/oma/oma-bin';
use OMA;
use lib '/opt/oma/transact';
use XSQL;

sub deleteit {
	$db = OMA::opendb;
    print "delete all related with $id\n";
    if(-d "/data/archive/REBOOT-RECORDINGS/$id") {
        system("rm -rf /data/archive/REBOOT-RECORDINGS/$id");
    }

    $st = $db->prepare("
        SELECT  *
        FROM    reboot_conn
        WHERE   reboot_conn.reboot_page=$id
        AND     reboot_conn.location='100tage.reboot.fm'");
    $st->execute();

    while(@row=$st->fetchrow()) {
        XSQL::transact("
			DELETE FROM item
			WHERE item.sid = $row[0]
			AND item.type='reboot'
		");
    }
    $st->finish();

    XSQL::transact("
        DELETE FROM reboot_conn
        WHERE   reboot_conn.reboot_page=$id
        AND     reboot_conn.location='100tage.reboot.fm'
    ");

    XSQL::transact("
        DELETE FROM reboot_page
        WHERE   id=$id
        AND     location='100tage.reboot.fm'
    ");

    XSQL::transact("
        DELETE FROM reboot_relation
        WHERE   id=$id
        AND     location='100tage.reboot.fm'
    ");

    XSQL::transact("
        DELETE FROM reboot_contributor
        WHERE   id=$id
        AND     location='100tage.reboot.fm'
    ");

    XSQL::transact("
        DELETE FROM reboot_creator
        WHERE   id=$id
        AND     location='100tage.reboot.fm'
    ");
}

#$id = $ARGV[0];
#deleteit();

opendir(D,"/data/archive/REBOOT-RECORDINGS/");
while($_=readdir(D)) {
	if($_ !~ /^\./ && $_>0) {
		$id = $_;
		deleteit();
	}
}
closedir(D);

#@x = (
#1075658400,
#1075662000,
#1076778000,
#1076783400,
#1076806800
#);
#
#for($i=0;$i<=$#x;$i++) {
#	$id = $x[$i];
#	deleteit;
#}
