#!/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_delete_user_data";
my $VERSION="1.1.4.1";

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

sub useage {
 	print "Useage: $0 {UserID} {Scope}\n";
}


my $userid="0";
my $uscope="a";
my $arg1;
my $flag="false";
my $coedat;
my $COEDAT;
my @userdat;
my @userprofdat;
my $i;
my $ERR;

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);
	}
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# do simple sanity checks
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# is this a legal scope ?
if ( "$uscope" eq "l" || "$uscope" eq "local" ) {
	$uscope="local";
} elsif ("$uscope" eq "g" || "$uscope" eq "global" )  {
	$uscope="global";
} else {
	&useage;
	exit 202;
}

# does this acct group exist?

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# define some stuff
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# (1) User.dat
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# path for the AppData data COEDAT
$coedat=coeFileConv "COE_USER/$uscope/Profiles/.User.dat";

# User.dat format:: userid:$username:$userdesc::::::

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# if the user id is found, remove it from the COEDAT
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

$i=0;
$COEDAT=readFile($coedat);
while (<$COEDAT>) { 
	if (!/^$userid:/) { 
		$userdat[$i++]=cleanString($_,3); 
	} else {
		$flag="true";
	}
}
close $COEDAT;
	
#- only rewrite if uid was found
if ("$flag" eq "true") {
	open COEDAT, (">$coedat") 
		|| die "Cannot open COEDAT $coedat: $!\n";
	foreach (@userdat){
		print COEDAT "$_\n"; 
	}
	close COEDAT;
	$ERR=0;
} else {
	$ERR=203;
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# (2) UserProfile.dat
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# path for the AppData data COEDAT
$coedat=coeFileConv "COE_USER/$uscope/Profiles/.UserProfile.dat";

# UserProfile.dat format:: userid:$scope:$profile

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# if the user id is found, remove it from the COEDAT
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

$i=0;
$flag="false";

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

#- only rewrite if uid was found
if ("$flag" eq "true" ) {
	open COEDAT, (">$coedat") 
		|| die "Cannot open COEDAT $coedat: $!\n";
	foreach (@userprofdat){
		print COEDAT "$_\n"; 
	}
	close COEDAT;
}

exit $ERR;