#! /usr/local/bin/perl

push(@INC, "../src");

$ctime_perl = 'ctime.pl';
$hostname_prog = '/bin/hostname';

$DEBUG_LOCK = 1;

$options = "dl:n:";
$usage = "usage: lock-test [-d] [-l lock library] [-n number] [directory]\n";

require 'yagrip.pl';
require 'lib-log.perl';

$lock_library = "lib-lock.perl";
$long_timeout = 900;
$lock_dir = "/n/dinosaur/0/romig/tmp";
$iterations = 50;
$wait = 120;

die $usage if ! &getopt($options);

$opt_d = $DEBUG_LOCK if defined($opt_d);
$lock_library = $opt_l if defined($opt_l);
$iterations = $opt_n if defined($opt_n);

eval("require \"$lock_library\"");
die $@ if $@;

if ($#ARGV == 0) {
    $lock_dir = shift;
} elsif ($#ARGV > 0) {
    die $usage;
} 

$log_file = "$lock_dir/foobar";
$lock_file = "$lock_dir/a.lock";

$| = 1;
for ($i = 0; $i < $iterations; $i++) {
    print "lock...";
    $result = &lock($lock_file, $wait);
    if ($result != $LOCK_OK) {
	print "failed\n";
    } else {
	print "unlock...";
	&unlock($lock_file);
	print "\n";

	sleep(1);
    }
}
