#! /usr/bin/perl -w
##**************************************************************
##
## 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.
##
##**************************************************************


##********************************************************************
## Determines whether another program has exited by checking the 
## link lock file.  Exits 0 if the process is dead.  1 otherwise.
##********************************************************************

######################################################################
######################################################################
## WARNING!!!!
## This program is deprecated.  It does not work in versions of AFS
## installed on Linux 2.6.  It may not work in all versions of NFS.
## Test before using, if you use it at all.
######################################################################
######################################################################

#***
# Uses
#***
use strict;
use FindBin;
use lib ($FindBin::Bin, "$FindBin::Bin/lib", "$FindBin::Bin/../lib");
use FileLock;
use Getopt::Long;

#***
# Constant Static Variables
#***
my $USAGE = 
    "Usage: linklock_undertaker [--file file][--block]\n";

#***
# Non-constant Static Variables
#***
my $lockfile = 'lock.file';
my $help = 0;
my $block = 0;

#***
# Main Function
#***

# Parse the command line options
GetOptions('file=s'=>\$lockfile,
	   'block'=>\$block,
	   'help'=>\$help,
	   );

# Process command line arguments
die $USAGE if( $help );

if( $block ){
    &Blocking();
} else{
    &NoBlocking();
}

#*********************************************************************
# Tests whether the lock is held in a non-blocking manner
# Exits 0 if the lock is not held
# Exits 1 if the lock is held
#*********************************************************************
sub NoBlocking{

    # See if the lock is held
    if( TestLinkLock($lockfile) ){
	# no, success
	exit 0;
    } else {
	# yes, failure
	exit 1;
    }

}

#*********************************************************************
# Blocks until the lock is released (using exponential backoff).
# Exits 0.
#*********************************************************************
sub Blocking{
    # Block until we acquire the lock
    my $lock_handle = AcquireLinkLock($lockfile);

    # Release the lock
    &ReleaseLinkLock($lock_handle, $lockfile);

    # exit
    exit 0;
}
