#!/usr/bin/env perl

#
# Set standard CHPL_* environment variables, if they are not already set.
#
my $env = `$ENV{CHPL_HOME}/util/printchplenv --sh`;
my @lines = split(/\n/, $env);
for my $line (@lines) {
  $line =~ s/export\s+//;
  my ($key,$value) = split(/=/, $line, 2);
  next if $key eq "CHPL_HOME";
  # Now remove the single quotes in the value.
  if( $value =~ /^'.+'$/ ) {
    $value =~ s/^'//;
    $value =~ s/'$//;
  }
  $ENV{$key} = $value;
}

$envfile = $ARGV[0];

open ENVFILE, "$envfile" or die "can't open $envfile $!";
my @envlist = <ENVFILE>;
close (ENVFILE);

$skiptest = 0;

foreach my $envsetting (@envlist) {
    chomp($envsetting);
    
    if ($envsetting =~ m/^\s*$/) {
        # blank
    } elsif ($envsetting =~ m/\#(.*)/) {
        # comment
    } elsif ($envsetting =~ m/(\w*)\s*(.)=\s*(\S*)/) {
#        print "checking whether $1 $2 $3\n";
        if ($2 eq "=") {
            if ($ENV{$1} eq $3) {
#                print "yep\n";
                $skiptest = 1;
            } else {
#                print "nope\n";
            }
        } elsif ($2 eq "!") {
            if ($ENV{$1} eq $3) {
#                print "nope\n";
            } else {
#                print "yep\n";
                $skiptest = 1;
            }
        } elsif ($2 eq "<") {
            if ($ENV{$1} =~ m/$3/) {
#                print "yep\n";
                $skiptest = 1;
            } else {
#                print "nope\n";
            }
        } else {
#            print "ERROR: badly formatted line: $envsetting\n";
            exit(1);
        }
    } else {
#        print "ERROR: badly formatted line: $envsetting\n";
        exit(1);
    }
}

print "$skiptest\n";

exit(0);
