#!/usr/bin/perl
#
# Name		: dr-cleanup
# 
# Version	: $Header: /cvsroot/ipfc/ipfc/src/dr-server/cleanup/dr-cleanup,v 1.1 2001/12/05 14:20:53 adulau Exp $
#
# Purpose	: Daemon for cleanup processed xml files on the dr-server
#      
# Author	: Alexandre Dulaunoy <alex@thinkingsecure.com>

# Copyright (C) 2000,2001 Alexandre Dulaunoy <adulau@thinkingsecure.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Alexandre Dulaunoy <adulau@conostix.com>

# 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 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
#

use File::Copy;

my ($dir,@agent,$dir_agent_processed,$exclude,$ok);

$exclude = "";


$IPFC_DRSERVER_ROOT="/opt/ipfc/htdocs/ipfc";
$IPFC_DRSERVER_BACKUP=$IPFC_DRSERVER_ROOT."/backup";

opendir (DRSERVERROOT, $IPFC_DRSERVER_ROOT);

## get all existing name of agent
while (defined ($dir = readdir (DRSERVERROOT)))
{
	if (!($dir =~ m/\.$|\..$/))
	{
	push @agent, $dir;
	}
}

## get all processed data by the db-backend "fetcher" from for each agent
## move files to the backup directory 
foreach $agentexist (@agent)
{
	print "Now starting processing of $agentexist ";
	$dir_agent_processed = $IPFC_DRSERVER_ROOT . "/".$agentexist."/processed/";
	opendir (DIRAGENT ,"$dir_agent_processed");
	print "dir agent processed is $dir_agent_processed \n";
	while (defined ($dir = readdir (DIRAGENT)))
	{
         if (!($dir =~ m/\.$|\..$/))
	 {
		push @processed, $dir;
	 }
	}

	foreach $line (@processed)
	{
		@data = split (/\./, $line);
		#print "transac : $data[0]";
		#print "type    : $data[1]";
		#print "\n";
		$backupdir=substr($data[0],0,10);
		$dir_agent_backup = $IPFC_DRSERVER_ROOT . "/".$agentexist."/backup/";
		if (! (-e $dir_agent_backup)){ mkdir ($dir_agent_backup, 0700); }
		$dir_agent_backup_hourbyhour = $dir_agent_backup . "/".$backupdir;
		if (! (-e $dir_agent_backup_hourbyhour)){ mkdir ($dir_agent_backup_hourbyhour, 0700); }
		print "backupdir : $backupdir\n";
		$file_agent_source = $IPFC_DRSERVER_ROOT. "/".$agentexist."/".$data[1]."/".$data[0].".".$data[1];
		$file_agent_destination = $dir_agent_backup . $data[0].".".$data[1];
		print "Move from $file_agent_source TO $file_agent_destination \n";
		move ($file_agent_source,$file_agent_destination) or print "Move NOK";
		print "Remove $file_agent_source \n";
		unlink($file_agent_source);
		$file_agent_source = $file_agent_source.".ok";
		print "Remove $file_agent_source \n";
		unlink($file_agent_source);
		$file_processed = $dir_agent_processed."/".$line;
		print "Remove $file_processed";
		unlink($file_processed);

	}
	close (DIRAGENT);

}


closedir (DRSERVERROOT);

