#! /usr/bin/perl
# Remove old exported files.
# 
# Copyright (C) 2005 Mathieu Roy <yeupou--gnu.org>
# Copyright (C) 2018 Ineiev
#
# This file is part of Savane.
# 
# Savane is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# Savane 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 Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
                                                                               
use strict;
use Savane;
use Getopt::Long;
use Term::ANSIColor qw(:constants);
use POSIX qw(strftime);
use Time::Local;
use File::Basename;
use File::Find::Rule;

my $default_logfile = "/var/log/sv_export_cleaner.log";
my $script = "sv_export_cleaner";
my $logfile = $default_logfile;

my $getopt;
my $help;
my $xml_path;
my $debug;

eval {
    $getopt = GetOptions("help" => \$help,
                         "xml-path=s" => \$xml_path,
                         "log-file=s" => \$logfile,
                         "debug" => \$debug);
};
 
if($help) {
    print STDERR <<EOF;
usage: $0
 
   Remove xml exports that were done and removed
   by the user via the frontend.
   Basically, it gets the list of exports known in the database and remove
   files that are not in this list.

   FIXME: it could remove files that are older than 2 weeks, but currently
   it does not.
   FIXME: it could remove empty directories, but currently it does not.
 
        --help                  print this help
        --xml-path=/            path of the generated xml file
        --log-file=$default_logfile
                                path for log file
 
Author: yves.perrin\@cern.ch, yeupou\@gnu.org

Copyright (C) 2005 Mathieu Roy <yeupou--gnu.org>
Copyright (C) 2018 Ineiev

License AGPLv3+: GNU AGPL version 3 or (at your option)
any later version <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF
  exit(1);
}

# Log: Starting logging
open (LOG, ">>$logfile");
print LOG strftime "[$script] %c - starting\n", localtime;

# Obtain the list of exports, build a hash
my $export_table = 'trackers_export';
my $fields = 'export_id';
my $criteria = "1";
my @jobs = GetDB($export_table, $criteria, $fields);
my %known_jobs;
foreach my $job (@jobs) {
    $known_jobs{"$job"} = 1 unless $known_jobs{"$job"};
}

# Look at the list of files
my @current_files = File::Find::Rule->file()
    ->name("*.xml", "*.xsd")
    ->in("$xml_path");

my @to_be_removed;
foreach my $file (@current_files) {
    my ($file, $path, $suffix) = fileparse($file, (".xml", ".xsd"));
    push(@to_be_removed, "$path/$file.xml", "$path/$file.xsd") 
	unless $known_jobs{$file};
}

# Remove orphans
unlink(@to_be_removed);

print LOG strftime "[$script] %c - removed ".scalar(@to_be_removed)." old xml files.\n", localtime;


# Final exit
print LOG strftime "[$script] %c - work finished\n", localtime;
print LOG "[$script] ------------------------------------------------------\n";

# EOF
