#!/h/COE/Comp/PERL5/bin/perl -T -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_deassign_user_from_profile";
my $VERSION="1.1.4.1";

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

sub useage {
	print "Usage: $PRGM [options]   ";
 	print "\"\" \n";
}

my $flag="false";
my $pscope="a";
my $profile="all";
my $userid="all";
my $uname="dummy";
my $username="dummy";
my $uscope="a";
my $arg1;
my $i;
my $coedat;
my $coedatstr;
my $COEDAT;
my @userprof;

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;
	} else {
   		$userid=$arg1;
		$uscope=cleanString(shift,1);
		$profile=cleanString(shift,1);
		$pscope=cleanString(shift,1);
	}
}


# verify the user scope 
if ("$uscope" eq "g" || "$uscope" eq "global") {
	$uscope="global";
} elsif ("$uscope" eq "l" || "$uscope" eq "local" ) {
	$uscope="local";
} else {
	print "User scope must be one of \"g\" or \"l\"\n";
	exit 201;
}

# verify the profile scope 
if ("$pscope" eq "g" || "$pscope" eq "global") {
	$pscope="global";
} elsif ("$pscope" eq "l" || "$pscope" eq "local" ) {
	$pscope="local";
} else {
	print "Profile scope must be one of \"g\" or \"l\"\n";
	exit 201;
}

$coedat=coeFileConv "COE_USER/$pscope/Profiles/.UserProfile.dat";

# remove entry from userprofile.dat
# USERPROFILEDAT format:: userid:userscope:profilename
$coedatstr="$userid:$uscope:$profile";

$i=0;
$COEDAT=readFile($coedat);
while (<$COEDAT>) { 
	if (!/^$coedatstr/) { 
		$userprof[$i++]=cleanString($_,3); 
	} else {
		$flag="true";
	}
}
close $COEDAT;

# only rewrite the file if entry exists
if ("$flag" eq "true") {
	open COEDAT, ">$coedat" || die "Cannot write to file $coedat: $!\n";
	foreach (@userprof) {
		print COEDAT "$_\n";
	}
	close COEDAT;
	exit 0;
} else {
	exit 208;
}