#!/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;
$Zymonic::system  = '';
$Zymonic::session = '';

use XML::Simple;

# Library path
BEGIN
{
    use Zymonic::Utils qw(clean death_handler debug get_array);
    $main::SIG{__DIE__} = \&death_handler;
}

$Zymonic::system = clean( $ARGV[0], '_' );
my $type     = clean( $ARGV[1], '_\*' );
my $zname    = clean( $ARGV[2], '_\*' );
my $log_file = clean( $ARGV[3], '-_./\\' );
unless ( $Zymonic::system and $type and $zname )
{
    print "get_def.pl [system] [type] [zname] [logfile (optional)]\n";
    print "type and zname will allow wildcard '*', but it must be quoted to avoid shell expansion\n";
    exit(1);
}

# make sure user understands the risks of running this
if ($log_file)
{
    my $fh;
    if ($log_file)
    {
        if ( $log_file eq '-' )
        {
            open $fh, ">&STDOUT" or die "Unable to open STDOUT for writing";
        }
        else
        {
            open $fh, '>', $log_file or die "Unable to open $log_file for writing";
        }
        $Zymonic::Utils::debugfile = $fh;
    }
    else
    {
        $Zymonic::Utils::debugs_written = 1;
    }
}

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

print XMLout(
    { map { $_->{ZName}->{content} => $_ } get_array( $Zymonic::ZCONFIG{$Zymonic::system}->get_def( $type, $zname ) ) },
    RootName => 'Zymonic',
    KeyAttr  => []
);
