#! /usr/local/bin/perl

push(@INC, "/n/dinosaur/0/romig/Projects/backup-new/src");

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

require 'lib-lock2.perl';
require 'lib-log.perl';

$long_timeout = 120;
$lock_dir = "/foo";
$log_file = "/dev/tty";

#
# test get_lock_names
#
    ($full, $tmp) = &get_lock_names("/foo/a");
    print "full name from get_lock_names with '/foo/a' is $full\n" if $full ne "/foo/a";
    print "tmp name from get_lock_names with '/foo/a' is $tmp\n" if $tmp ne "/foo/a.$hostname.$$";

    ($full, $tmp) = &get_lock_names("a");
    print "full name from get_lock_names with 'a' is $full\n" if $full ne "$lock_dir/a";
    print "tmp name from get_lock_names with 'a' is $tmp\n" if $tmp ne "$lock_dir/a.$hostname.$$";

#
# test lock alone
#

    unlink </tmp/a.lock*>;

#
# try it once when it should work
#
    $result = &lock("/tmp/a.lock");
    if ($result != $LOCK_OK) {
	print "bad: lock failed\n";
    } else {
	print "good: got lock\n";
    }
    if (! -f "/tmp/a.lock") {
	print "bad: didn't make the lock file\n";
    } else {
	print "good: made the lock file\n";
    }
    unlink </tmp/a.lock*>;

#
# try it twice - first ok, second should fail
#
    $result = &lock("/tmp/a.lock", 30);
    if ($result != $LOCK_OK) {
	print "bad: lock failed\n";
    } else {
	print "good: got the lock\n";
    }

    $result = &lock("/tmp/a.lock", 30);
    if ($result == $LOCK_OK) {
	print "bad: second lock suceeded\n";
    } else {
	print "good: second lock failed\n";
    }

    unlink </tmp/a.lock*>;

#
# try it twice, but with long timeout so that we can test the old lock 
# handling
#
    $result = &lock("/tmp/a.lock", 30);
    if ($result != $LOCK_OK) {
	print "bad: lock failed\n";
    } else {
	print "good: got lock\n";
    }

    $result = &lock("/tmp/a.lock", 240);
    if ($result != $lock_OK) {
	print "bad: second lock failed (no long timeout)\n";
    } else {
	print "good: second lock succeeded (after long timeout)\n";
    }

    unlink </tmp/a.lock*>;

#
# try unlock...
#
    print "can't lock\n" if &lock("/tmp/a.lock");

    &unlock("/tmp/a.lock");
    if (-f "/tmp/a.lock") {
	print "bad: unlock failed\n";
    } else {
	print "good: unlock worked\n";
    }

