#!/usr/bin/env perl

use strict;
use warnings;
use warnings qw( FATAL );
use Encode qw( decode );

if ( grep /\P{ASCII}/ => @ARGV ) {
    @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
}

# UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/

# Improve warning/exception handling
use Carp qw( carp croak confess cluck );

# give a full stack dump on any untrapped exceptions
local $SIG{ __DIE__ } = sub {
    confess "Uncaught exception: @_" unless $^S;
};

# now promote run-time warnings into stackdumped exceptions
#   *unless* we're in an try block, in which
#   case just generate a clucking stackdump instead
local $SIG{ __WARN__ } = sub {
    if   ( $^S ) { cluck "Trapped warning: @_" }
    else         { confess "Deadly warning: @_" }
};

# Improve warning/exception handling

# Find libraries path (should be in lib/ directory of where binary is, or on system perl library directory
use FindBin;
use lib "$FindBin::RealBin/lib";

our $VERSION = '5.0';

# Find out whether current run should be treated as CGI or CLI
my $program;
if ( $ENV{ 'GATEWAY_INTERFACE' } ) {
    require pgFormatter::CGI;
    $program = pgFormatter::CGI->new();
}
else {
    require pgFormatter::CLI;
    $program = pgFormatter::CLI->new();
}

$program->run();

exit 0;
