#!/usr/local/bin/perl

use strict;
use lib '/var/home/slowtwitch/slowtwitch.com/cgi-bin/articles/admin';
use vars  qw/$USE_HTML $TIME_START $TOTAL_TIME @CARP_NOT $GRAND_TOTAL/;
use Links qw/:objects :payment/;
use Links::Build;

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

main();

sub main {
# -------------------------------------------------------------------
#
    _build_backup();
}

sub _build_backup {
# ------------------------------------------------------------------
# Create a backup file in our backup directory.
#
    print "Creating backup file...\n";
    require Links::Import::S2BK;

    my $max_keep = 7;
    my $root     = $CFG->{admin_root_path} . '/backup';
    my $filename = 'BACKUP';

    for my $n (reverse 0 .. $max_keep) {
        my $oldname = join '.', $filename, $n || ();
        my $newname = join '.', $filename, $n+1;
        if (-e "$root/$oldname") {
            rename "$root/$oldname", "$root/$newname" or print "\tCouldn't rename '$root/$oldname' -> '$root/$newname': $!";
        }
    }
    Links::Import::S2BK::import({ source => "$CFG->{admin_root_path}/defs", destination => "$root/$filename", delimiter => "\t" }, sub { print "\n\tWARNING: @_\n" }, sub { die @_ }, sub { print "\n\tWARNING: @_\n" }, sub { });
    print "Done\n";
}
