#!/usr/local/bin/perl
# ==================================================================
# Gossamer Links - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,071,086,086,085      
#   Revision : $Id: admin.cgi,v 1.94 2009/05/12 01:09:13 brewt Exp $
#
# Copyright (c) 2005 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

use strict;
use lib '/var/home/slowtwitch/slowtwitch.com/cgi-bin/articles/admin';
use Links qw/:objects/;
use GT::File::Tools qw/:all/;

$| = 1;
local $SIG{__DIE__} = \&Links::fatal;
Links::init('/var/home/slowtwitch/slowtwitch.com/cgi-bin/articles/admin');
Links::init_admin();

my $tab_files = $DB->table('Links_Files');
my $files     = $tab_files->select()->fetchall_hashref;
for (@$files) {
    my $fletter =  ( reverse split //, $_->{ID} )[0];
    my $path = "$_->{File_Directory}/$fletter";
    my $escaped_name = $IN->escape($_->{File_Name});
    unless(-f "$path/$_->{ID}-$escaped_name") {
        if (-f "$path/$_->{ID}-$_->{File_Name}") {
            $tab_files->update({ File_Name => $IN->unescape($_->{File_Name}) }, { ID => $_->{ID} });
        }
        elsif ($_->{File_Directory} eq '/var/home/slowtwitch/slowtwitch.com/www/articles/images') {
            if (-f "/var/home/slowtwitch/slowtwitch.com/www/articles/images.bad/$fletter/$_->{ID}-$_->{File_Name}") {
                copy("/var/home/slowtwitch/slowtwitch.com/www/articles/images.bad/$fletter/$_->{ID}-$_->{File_Name}", "$path/$_->{ID}-$_->{File_Name}");
                print "not escape $_->{File_Name}: FOUND\n";
            }
            elsif (-f "/var/home/slowtwitch/slowtwitch.com/www/articles/images.bad/$fletter/$_->{ID}-$escaped_name") {
                print "escaped $escaped_name: FOUND\n";
            }
            elsif ("/var/home/slowtwitch/slowtwitch.com/www/missing_files/$_->{File_Name}") {
                print "$path/$_->{ID}-$_->{File_Name}\n";
                move("/var/home/slowtwitch/slowtwitch.com/www/missing_files/$_->{File_Name}", "$path/$_->{ID}-$_->{File_Name}");
            }
        }
        else {
 #           print "$path/$_->{ID}-$_->{File_Name}\n";
        }
    }
}

