#!/usr/bin/perl -w
#
# Zymonic Business Process and Information Management System
# Copyright Zednax Limited 2008 -
# For Authors and Changelog see the subversion history
use strict;

# Modules
use Zymonic::Config;
use Zymonic::DB;
use Zymonic::Importer;
use XML::Simple;
use Zymonic::Utils qw(clean);

# Clean the path
$ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin";

# Config file
my $system      = clean( $ARGV[0], "_" );
my $config_file = clean( $ARGV[1], "_\/" );
die "Please provide an import configuration XML file... Usage: importer.pl [system] [config file]" unless $config_file;
die "Please provide a system name... Usage: importer.pl [system] [config file]"                    unless $config_file;

# Get the password
print "Please enter the password for the import user: ";
my $password = <STDIN>;
chomp $password;

my $config = Zymonic::Config->new(
    system_name => $system,
    config_dir  => "/etc/zymonic",
    ip_address  => '127.0.0.1',
    protocol    => 'http'
);

# Load the importer
my $importer = Zymonic::Importer->new(
    xml      => XMLin( $config_file, ForceContent => 1 ),
    password => $password,
    config   => $config,
);

# Clean the mapping
$config->clean_sys_def( $importer->{xml} );

# Process the records from the local DB
$importer->process_records;

# Allow the user to review
$importer->interactive_review;

#ÊSend the reviewed records
$importer->post_to_remote;

