# Misc routines

# Variable $no_invoke controls whether or not commands are executed

# Vaiable $show_invoke, if true, causes invoke to display the command
# to be run before running it.

$no_invoke = 0;
$show_invoke = 0;
$hold_on_error = 1;

sub cmp_reverse {
    $b cmp $a
}

sub member {
    local ( $element, @list ) = @_;
    local ( $tmp );

    foreach $tmp (@list) {
	if ($tmp eq $element) {
	    return 1;
	}
    }

    return 0;
}

sub abs {
    local ($i) = @_;

    $i = -$i if ($i < 0);
    return $i;
}

sub invoke {
    local ($command) = @_;
    local ( $ret );

    if ($show_invoke || $rh_testing) {
	&rhs_msgbox("invoke", $command, 75);
    }

    if ($rh_testing) {
	return 0;
    }

    $ret = 0;
    if (! $no_invoke) {
	$ret = system($command) / 256;
    }

    if ($ret && $hold_on_error) {
	print "\nError.  Press enter for more info\n";
	<STDIN>;
	print "The following command:\n\n";
	print "$command\n";
	print "\nexited with code $ret\n";
	print "\nPress enter to continue.\n";
	<STDIN>;
    }
    return $ret;
}

sub rhs_error {
    print "\nAn error of some kind occurred.\n";
    print "Press enter to continue: ";
    <STDIN>;
}

sub invoke_no_output {
    local ( $command ) = @_;
    local ( $ret );

    if ($show_invoke) {
	&rhs_msgbox("invoke", $command, 75);
    }

    open(SAVEERR, ">&STDERR");
    open(SAVEOUT, ">&STDOUT");
    open(STDERR, ">/dev/null");
    open(STDOUT, ">/dev/null");
    $ret = system($command) unless ($no_invoke);
    open(STDERR, ">&SAVEERR");
    open(STDOUT, ">&SAVEOUT");

    return $ret;
}

sub asciitime {
    local ( $tval ) = @_;
    local ( $secs, $mins, $hours );

    $hours = $tval / 3600;
    $tval %= 3600;
    $mins = $tval / 60;
    $secs = $tval % 60;

    return sprintf("%02d:%02d:%02d", $hours, $mins, $secs);
}

######################
1;
