#!/h/COE/Comp/PERL5/bin/perl -I/h/COE/lib  -w
#-
 # Copyright (C) 2002, Federal Linux Systems
 # License: GNU Lesser General Public License, v2.1
#- 

use strict;
use Coe;

my $PRGM="COE_query_profile_lock";
my $VERSION="1.1.4.1";

if (@ARGV == 0) {
	&useage;
	exit 202;
} 

sub useage {
	print "Useage: COE_query_profile_lock [options] ";
	print "<\"ProfileName\">  [-who]\n";
}

my $arg1;
my $fn;
my $mode="dummy";
my $path;
my $ppath;
my $pscope;
my $flagfile="false";
my $lfile="";
my $profile;
my @profdata;

while (@ARGV > 0) {
	$arg1=cleanString(shift,1);
	if ("$arg1" eq "-h") {
		useage;
   		exit 0;
	} elsif ("$arg1" eq "-H") {
		coeMan("COE_COMP/UserMgmt/docs/man/$PRGM");
   		exit 0;
	} elsif ("$arg1" eq "-V") {
		print "VERSION: $VERSION\n";
   		exit 0;
	} elsif ("$arg1" eq "-who") {
		$mode="verbose";
	} else {
		$profile=$arg1;
		$pscope=cleanString(shift,1);
	}
}

if ("$pscope" eq "l" ) {
	$ppath="local";
} elsif ("$pscope" eq "g" ) {
	$ppath="global";
} else {
	useage;
  	exit 202;
}

# set the path to the locked profile directory
$path=coeFileConv "COE_USER/".$ppath."/Profiles/.LockedProfiles";

# get profile data
$_=coeFileConv("COE_HOME/bin/COE_get_profile_data");
$_=$_." -p \"$profile\" -s $pscope";
$_=coeExec($_);
$_=cleanString($_,3);
@profdata=split(':');

# if there is no such profile, exit
if ( @profdata < 5 ) {
	exit 204;
}

# set the locked file name
$fn="$profdata[4]";

# check to see if the lock file already exists
opendir DIR, ("$path") || die "Cannot open dir $path: $!\n";
$flagfile="false";
while ($lfile = readdir DIR ) { $_=$lfile; if (/^$fn/) { $flagfile="true"; } }
closedir DIR;

# if the lock file exists and mode is verbose, output the file info and exit 0
# else if the lock file exists and mode is quiet, exit 0
# else if the lock file does not exit, exit 211 (COE_NO_SUCH_DATA)
if ("$flagfile" eq "true") {
	if ( "$mode" eq "verbose") {
		open LOCKF, ("$path/$fn") || die "Cannot open lock file $fn: $!\n";
		while () { s/:.*$//; print "$_";}
		close LOCKF;
	}
	exit 0;
} else {
	exit 211;
}