#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, 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;
use strict;
use warnings;

my $corename = "cmd_status_shows-help";
my $debuglevel = 1;

print scalar localtime() . "\n";

my $mypid = $$;
my $mysaveme = $corename . ".saveme";
CreateDir("$mysaveme");

if($mypid == 0) {
    die "Saveme setup for test failed\n";
}

my $found_usage = 0;
my @adarray = ();

print "Looking for Usage lines from condor_status -help - \n";

#my $cmd = "condor_status -help";
my $helpfile = "statushelp$mypid";
#my $cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>1});
@adarray = `condor_status -help >$helpfile 2>&1`;
my $line = "";
open(HF,"<$helpfile") or die "Could not gather condor_status help messages:$!\n";
while (<HF>) {
	$line = $_;
	fullchomp($line);
	push @adarray, $line;
}
close(HF);
my $outputsz = @adarray;


print "Back from collecting help messages($outputsz)";

CondorTest::debug("Looking at condor_status -help \n", $debuglevel);

# looking for.....  <Usage: condor_status>
foreach my $line (@adarray) {
	fullchomp($line);
    if($line =~ /^\s*Usage:\s+(\/[^\/]+)*\/?condor_status.*/) {
        CondorTest::debug("found usage: $line\n", $debuglevel);
	print "Found usage\n";
        $found_usage = 1;
        print "ok\n";
        last;
    }
    else {
        CondorTest::debug("no match: $line\n", $debuglevel);
	print "no match: $line\n";
    }
}

if($found_usage == 0) {
    print "bad\n";
}

CondorTest::debug("Saving things and cleaning up....\n", $debuglevel);

# Fails due to directory of same name.
runcmd("cp *cmd_status_shows-help* $mysaveme", {expect_result=>\&ANY});

print scalar localtime() . "\n";

if($found_usage == 0) {
    # actual test failed but we allowed cleanup
    exit(1);
}

CondorTest::debug("cmd_status_shows-help SUCCESS\n", $debuglevel);
exit(0);
