#!/usr/bin/perl

use Const::Fast;
use IPC::Run3;
use Text::ParseWords qw(shellwords);
use Unicode::UTF8 qw(encode_utf8 decode_utf8);

const my $DOUBLE_QUOTE => q{"};

sub run_quiet {
    my (@command) = @_;

    my @command_bytes = map { encode_utf8($_) } @command;

    my $stdout_bytes;
    my $stderr_bytes;
    run3(\@command_bytes, \undef, \$stdout_bytes, \$stderr_bytes);

    my $wait_status = $?;
    my $exitcode = ($wait_status >> $WAIT_STATUS_SHIFT);

    # already in UTF-8
    die encode_utf8("Non-zero exit code $exitcode.")
      . $NEWLINE
      . $stdout_bytes
      . $stderr_bytes
      if $exitcode;

    my $output = decode_utf8($stdout_bytes // $EMPTY);
    chomp $output;

    return $output;
}

print "BLAH1:$ENV{DEB_SETUP_GHC_CONFIGURE_ARGS}\n";

my @blah = shellwords($ENV{DEB_SETUP_GHC_CONFIGURE_ARGS});
my @meow = shellwords($ENV{MEOW} // "");
my $ghc_configure_args = ($ENV{DEB_SETUP_GHC_CONFIGURE_ARGS} // $EMPTY);
$ghc_configure_args =~ s{"}{\\"}g;
print "BLAH2:$ghc_configure_args\n";
$ghc_configure_args = run_quiet(
    qw{sh -c},
    'echo -n '
    . $DOUBLE_QUOTE
    . $ghc_configure_args
    . $DOUBLE_QUOTE
    );

print "BLAH:$ghc_configure_args\n";
#$blah = split(q{ }, $ghc_configure_args);
for my $b (@blah) {
    print "BLAHA: $b\n";
}
for my $b (@meow) {
    print "BLAHA: $b\n";
}
print scalar @meow;
print "\n";
my @buzz = ();
print scalar @buzz;
print "\n";

#print ($ENV{DEB_SETUP_GHC_CONFIGURE_ARGS} // $EMPTY) ~= s/dog/cat/r;
