#!/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 lib '../oma-bin/';
use OMA;
use DBI;


$solve = 1 if($ARGV[1] eq solve);

$db = OMA::opendb();

sub pstat {
	my $tab = shift;
	my $id = shift;
	my $location = shift;
	my @row;
	my $st;
	$st=$db->prepare("SELECT id,location FROM $tab" . "_stat
						WHERE id=$id AND location='$location'");
	$st->execute();
	@row=$st->fetchrow();
	$st->finish();
	if($row[0] = $id && $row[1] eq $location) {
		return(1);
	} else {
		return(0);
	}
}

sub pabstract {
    my $tab = shift;
    my $id = shift;
    my $location = shift;
    my @row;
    my $st;
    $st=$db->prepare("SELECT id,location FROM $tab" . "_abstract
                        WHERE id=$id AND location='$location'");
    $st->execute();
    @row=$st->fetchrow();
    $st->finish();
    if($row[0] = $id && $row[1] eq $location) {
        return(1);
    } else {
        return(0);
    }
}

sub puser {
    my $tab = shift;
    my $user = shift;
    my @row;
    my $st;
    $st=$db->prepare("SELECT username FROM users WHERE username='$user'");
    $st->execute();
    @row=$st->fetchrow();
    $st->finish();
    if($row[0] = $user) {
        return(1);
    } else {
        return(0);
    }
}


sub proof {
	my $tab = shift;
	my @row;
	my $st;
	$st = $db->prepare("SELECT * FROM $tab" . "_page");
	$st->execute();
	$st->bind_columns(\(@hashrow{@{$st->{NAME_lc}}}));
	while(@row=$st->fetchrow()) {
		if(0) {
			if(!pstat($tab,$hashrow{id},$hashrow{location})) {
				if($solve) {
					print "insert into $tab" . "_stat values ($row[0],'$row[1]',0,0,0);\n";
				} else {
					print "$tab $row[0] $row[1] : stat not found\n";
				}
			}
			if(!pabstract($tab,$hashrow{id},$hashrow{location})) {
				if($solve) {
#					print "insert into $tab" . "_abstract values ($row[0],'$row[1]','');\n";
				} else {
					print "$tab $row[0] $row[1] : abstract not found\n";
				}
			}
		if(!puser($tab,$hashrow{username})) {
			if($solve) {
			} else {
				print "$tab $row[0] $row[1] : user $row[2] not found\n";
			}
		}
		}
		@identifier = split(/\//,$hashrow{dc__identifier});
		$hashrow{dc__identifier} = $identifier[$#identifier];
		if(-f "/data/archive$hashrow{path}$hashrow{id}/$hashrow{dc__identifier}"
		&& $hashrow{location} eq "archive.reboot.fm") {
		}
		else {
#			print "$hashrow{id},$hashrow{location} doesn't have a $hashrow{dc__identifier}\n";
print "$hashrow{dc__identifier}\n";
if( -f "/tmp/$hashrow{dc__identifier}") {
	system("mkdir -p /data/archive/REBOOT-DATA/$hashrow{id}");
	system("mv /tmp/$hashrow{dc__identifier} /data/archive/REBOOT-DATA/$hashrow{id}");
}
		}
		if($tab eq "ova") {
		}
	}
	$st->finish();
}


proof($ARGV[0]);
