#!/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: nph-import.cgi,v 1.22 2005/09/19 23:11:16 brewt Exp $
# 
# Copyright (c) 2001 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

use 5.004_04;
use strict;
use lib '/var/home/slowtwitch/slowtwitch.com/cgi-bin/articles/admin';
use vars qw/$Int $Error_FH $Critical_Warnings $Show_Mild_Warnings/;
use DBI;
use Links qw/$IN $CFG/;
use GT::SQL;

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

main();

sub get_rec (\*\%$;\@);

sub main {
# -----------------------------------------------------------------------------
# Load either the CGI interface or Text interface, and turn off all buffering.
#
    select((select(STDERR),$|=1)[0]),$|=1;
    if ($ENV{REQUEST_METHOD}) {
        require Links::Import::Interface::CGI;
        $Int = new Links::Import::Interface::CGI;
    }
    else {
        require Links::Import::Interface::Text;
        $Int = new Links::Import::Interface::Text;
    }
    $Error_FH = \do { local *FH; *FH };

# Get Import options.
    my %option = $Int->get_options();
    unless (keys %option) {
        return $Int->start_page();
    }
    if ($option{help}) {
        return $Int->show_help();
    }
    if ($option{transfer} eq "S1S2") { # Links SQL 1 to Links SQL 2
        $option{source}         or $Int->usage("You must provide the path to the Links SQL 1 def files","--source");
        -d $option{source}      or $Int->usage("The path to the Links SQL 1 def files that you gave does not exist", "--source");
        $option{destination}    or $Int->usage("You must provide the path to the Links SQL 2 def files","--destination");
        -d $option{destination} or $Int->usage("The path to the Links SQL 2 def files that you gave does not exist", "--destination");
    }
    elsif ($option{transfer} eq "L1S2") { # Links 1.x to Links SQL 2
        $option{source}         or $Int->usage("You must provide the path to the Links 1.x def and db files","--source");
        -d $option{source}      or $Int->usage("The path to the Links 1.x def and db files that you gave does not exist", "--source");
        $option{destination}    or $Int->usage("You must provide the path to the Links SQL 2 def files","--destination");
        -d $option{destination} or $Int->usage("The path to the Links SQL 2 def files that you gave does not exist", "--destination");
    }
    elsif ($option{transfer} eq "L2S2") { # Links 2.x to Links SQL 2
        $option{source}         or $Int->usage("You must provide the path to the Links 2.x def and db files","--source");
        -d $option{source}      or $Int->usage("The path to the Links 2.x def and db files that you gave does not exist", "--source");
        $option{destination}    or $Int->usage("You must provide the path to the Links SQL 2 def files","--destination");
        -d $option{destination} or $Int->usage("The path to the Links SQL 2 def files that you gave does not exist", "--destination");
    }
    elsif ($option{transfer} eq "BKS2") { # Backup File to Links SQL 2
        $option{source}         or $Int->usage("You must provide the path and filename of the backup file to restore","--source");
        $option{destination}    or $Int->usage("You must provide the path to the Links SQL 2 def files","--destination");
        -d $option{destination} or $Int->usage("The path to the Links SQL 2 def files that you gave does not exist","--destination");
        -f $option{source}      or $Int->usage("The backup filename that you gave does not exist","--source");
        $option{clear_tables}   or $Int->usage("The 'Clear Tables' option must be enabled when performing a restoration from a backup file", "--clear-tables");
    }
    elsif ($option{transfer} eq "S2BK") { # Links SQL 2 to Backup File
        $option{source}         or $Int->usage("You must provide the path to the Links SQL 2 def files","--source");
        -d $option{source}      or $Int->usage("The path to the Links SQL 2 def files that you gave does not exist","--source");
        $option{destination}    or $Int->usage("You must provide a destination filename for the backup file","--destination");
        $option{delimiter}      = "|"; # Fixed delimiter
    }
    elsif ($option{transfer} eq "RDFS2" and ref $Int eq "Links::Import::Interface::Text") { # RDF import - can ONLY be done from shell
        if (not $option{source}) {
            $Int->usage("You must provide the path and filename to the RDF file","--source");
        }
        elsif (not -r $option{source}) {
            $Int->usage("The path and filename to the RDF file that you gave does not exist, or does not have read permissions set correctly", "--source");
        }
        $option{rdf_category}   or $Int->usage("You must specify an RDF category to import","--rdf-category");
        if (not $option{rdf_add_date} or $option{rdf_add_date} !~ /^\d{4}-\d\d-\d\d$/) {
            $Int->usage("You must specify a valid RDF Add_Date in the format `YYYY-MM-DD'","--rdf-add-date");
        }
    }
    elsif (!$option{transfer}) {
        $Int->usage("You did not specify the import format or backup/restore function","--import, --backup, or --restore");
        $Int->show_usage;
        critical("You did not specify the import format or backup/restore function");
    }
    else {
        $Int->usage("Invalid import format specified","--import");
        $Int->show_usage;
        critical("Invalid import format specified");
    }
    if ($option{straight_import} and not $option{clear_tables}) {
        $Int->usage("the `Straight Import' option can only be used if `Clear Tables' has been turned on","--straight-import --clear-tables");
        $Int->show_usage;
        critical("the `Straight Import' option not allowed unless `Clear Tables' has been turned on");
    }
    $Int->has_usage() and $Int->show_usage, exit;
    if ($option{error_file}) {
        if (ref $option{error_file} eq "CODE") {
            $Error_FH = $option{error_file};
        }
        elsif (uc $option{error_file} eq 'STDOUT') {
            open $Error_FH, ">&STDOUT";
            select +(select($Error_FH),$|=1)[0];
        }
        elsif (uc $option{error_file} eq 'STDERR') {
            open $Error_FH, ">&STDERR";
            select +(select($Error_FH),$|=1)[0];
        }
        else {
            unless (open $Error_FH,"> $option{error_file}") {
                $Int->usage("Invalid error file: Cannot open $option{error_file}: $!");
                $Int->show_usage;
                exit;
            }
            select +(select($Error_FH),$|=1)[0];
        }
    }
    else {
        open $Error_FH, ">&STDERR";
        select +(select($Error_FH),$|=1)[0];
    }
    $Critical_Warnings = delete $option{critical_warnings};
    $Show_Mild_Warnings = delete $option{show_mild_warnings};

# Switch delims from windows to unix
    $option{source}      =~ s,\\,/,g;
    $option{destination} =~ s,\\,/,g;


    do_import(%option);
    $Int->finished();
}

sub mild_warning {
# -----------------------------------------------------------------------------
# Display mild warnings.
#
    if ($Show_Mild_Warnings) {
        for (@_) { error("Warning: $_") }
    }
    0;
}

sub warning {
# -----------------------------------------------------------------------------
# Display warnings.
#
    if ($Critical_Warnings) {
        goto &critical;
    }
    else {
        for (@_) { error("WARNING: $_") }
    }
    0;
}

sub critical {
# -----------------------------------------------------------------------------
# Display critical warnings.
#
    my ($prog,$line) = (caller(1))[1,2];
    for (@_) { error("CRITICAL ERROR OCCURED: $_ at $prog line $line\n\n") }
    print STDERR join('\n',map "CRITICAL ERROR OCCURED: $_", @_)." at $prog line $line\n";
    exit 0xff; # mimics die()
}

sub error {
# -----------------------------------------------------------------------------
# Display error messages.
#
    local ($,,$\);
    if (ref $Error_FH eq "CODE") {
        $Error_FH->(@_);
    }
    else {
        local $,="\n";
        local $\="\n";
        if (defined fileno $Error_FH) {
            print $Error_FH @_;
        }
        else {
            print @_;
        }
    }
    return;
}

sub do_import (\%) {
# -----------------------------------------------------------------------------
# Do the actual import.
#
    my $opt = @_ == 1 ? shift : {@_};
    $Int->pre_import();
    if ($$opt{transfer} eq "S1S2") {
        require Links::Import::S1S2;
        return Links::Import::S1S2::import($opt,\&warning,\&critical,\&mild_warning);
    }
    elsif ($$opt{transfer} eq "S2BK") {
        require Links::Import::S2BK;
        return Links::Import::S2BK::import($opt,\&warning,\&critical,\&mild_warning);
    }
    elsif ($$opt{transfer} eq "BKS2") {
        require Links::Import::BKS2;
        return Links::Import::BKS2::import($opt,\&warning,\&critical,\&mild_warning);
    }
    elsif ($$opt{transfer} eq "L1S2") {
        require Links::Import::L1S2;
        return Links::Import::L1S2::import($opt,\&warning,\&critical,\&mild_warning);
    }
    elsif ($$opt{transfer} eq "L2S2") {
        require Links::Import::L2S2;
        return Links::Import::L2S2::import($opt,\&warning,\&critical,\&mild_warning);
    }
    elsif ($$opt{transfer} eq "RDFS2") {
        require Links::Import::RDFS2;
        return Links::Import::RDFS2::import($opt,\&warning,\&critical,\&mild_warning);
    }
}
