#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2020, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
##
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
##
##    http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use CondorTest;
use CondorUtils;

my $iswindows = CondorUtils::is_windows();

my $reldir = `condor_config_val release_dir`; chomp $reldir;
my $pathsep = ':';
my $relpy = "$reldir/lib/python3";
my $pypgm = "python3";
my $pyd = "htcondor.pyd";
if ($iswindows) {
    $relpy = "$reldir\\lib\\python"; $pathsep = ';';
    my $pyver = "3.9";
    $pyd = "htcondor.cp39-win_amd64.pyd";
    if (-f "$relpy\\htcondor\\htcondor.cp38-win_amd64.pyd") {
        $pyver = "3.8";
        $pyd = "htcondor.cp38-win_amd64.pyd";
    } elsif (-f "$relpy\\htcondor\\htcondor.cp36-win_amd64.pyd") {
        $pyver = "3.6";
        $pyd = "htcondor.cp36-win_amd64.pyd";
    }
    my @reglines = `reg query HKLM\\Software\\Python\\PythonCore\\$pyver\\InstallPath /ve`;
    for my $line (@reglines) {
       if ($line =~ /REG_SZ\s+(.+)$/) {
          print "\tfound python $pyver install dir : '$1'\n";
          $pypgm = "$1python.exe";
          last;
       }
    }
    $relpy .= ";$reldir\\bin";
    print "\tsetting PYTHONPATH=$relpy\n";
    $ENV{PYTHONPATH} = $relpy;
}

my $pythonpath = "";
if (exists($ENV{PYTHONPATH})) {
    $pythonpath = $ENV{PYTHONPATH};
    print "\texisting PYTHONPATH=$pythonpath\n";
    if (index($reldir,$pythonpath) < 0) {
        print "\tadding $relpy to PYTHONPATH\n";
        $ENV{PYTHONPATH} = "$relpy$pathsep$pythonpath";
    }
} else {
    print "\tsetting PYTHONPATH to $relpy\n";
    $ENV{PYTHONPATH} = $relpy;
}

$ENV{ON_BATLAB} = 'true';

if ($iswindows) {
   #my $sleep = `sleep 10m`;
   #print "sleep returned $sleep\n";
   my $rvt = runCommandCarefully( {die_on_failed_expectation => FALSE}, "$reldir\\bin\\win_install_probe.exe", '-load', "$reldir\\lib\\python\\htcondor\\$pyd" );
   print "probe load of bindings : $rvt\n";
}

my $rv = runCommandCarefully( undef, $pypgm, '-m', 'pytest', 'test_htcondor_dags' );
exit $rv->{"exitcode"}
