#!perl

# --8<--8<--8<--8<--
#
# Copyright (C) 2008-2012 Smithsonian Astrophysical Observatory
#
# This file is part of sysarch
#
# sysarch is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# -->8-->8-->8-->8--

package SysArch;

use 5.006;

use strict;
use warnings;
use Carp;

our $VERSION = '@VERSION@';

## no critic (ProhibitStringyEval)
my $have_YAML_TINY = eval "use YAML::Tiny qw( read_string ); 1;";


our %Vars;

if ( 0 == keys( %Vars ) ) {

    local %ENV = %ENV;

    # clean up environment so Perl is taint happy
    delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};

    my $program = exists $ENV{SYSARCH_TEST_EXE} ? $ENV{SYSARCH_TEST_EXE}  : "@bindir_EVAL@/sysarch";
    my @options = ( $have_YAML_TINY ? '-yaml' : '-perl_hash' );

    local $/;

    # use 3 argument version of open for security reasons
    open( my $fh, '-|', $program, @options );

    my $code = <$fh>;

    close( $fh ) ||
      croak( "error running $program: $?\n" );

    # untaint $code
    $code =~ m/(.*)/s;
    $code = $1;

    %Vars = $have_YAML_TINY
             ? %{YAML::Tiny->read_string($code)->[0]}
             : eval( $code );
}

1;
