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

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

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


my $profile="dummy";
my $pscope="all";
my $arg1;
my $coedat;
my $COEDAT;
my $COEERR=0;
my $flag="false";
my $i=0;
my @readline;

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

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

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

# does this profile exist?

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# (1) Delete Info from Profile.dat
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# path for the Profile data COEDAT
$coedat=coeFileConv "COE_USER/$pscope/Profiles/.Profile.dat";

# Profile.dat format:: profilename:acctgrp:lockflag:profileabbrev::::::

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#     if the profile name and acct group exist, delete the line
#     ( 20020703rb: need to specify acctgrp if I want to search on it! )
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#- set COEERR to no profiles found
$COEERR=204;


$COEDAT=readFile($coedat);
while (<$COEDAT>) { 
	if (!/^$profile:/) { 
		$readline[$i++]=cleanString($_,3); 
	} else {
		$flag="true";
		# set COEERR to success;
		$COEERR=0;
	}
}
close $COEDAT;

#- only rewrite the file if the profile:acctgrp was found in it
if ("$flag" eq "true" ) {
	open COEDAT, (">$coedat") 
		|| die "Cannot write to file $coedat: $!\n";
	foreach (@readline) {
		print COEDAT "$_\n"; 
	}
	close COEDAT;
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# (2) Delete Info from ProfileApp.dat
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

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

# ProfileApp.dat format:: profilename:appname

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#     if the first field is the profile name, delete the line
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

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

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

#- only write to the file if the profile:app data was found in it
if ("$flag" eq "true") {
	open COEDAT, (">$coedat") 
		|| die "Cannot write to file $coedat: $!\n";
	foreach (@readline) {
		print COEDAT "$_\n"; 
	}
	close COEDAT;
}


# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# (3) Delete Info from UserProfile.dat
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

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

# UserProfile.dat format:: userid:scope:profilename

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#     if the profile name shows up in the first field, delete the line
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

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


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

if ("$flag" eq "true" ) {
	open COEDAT, (">$coedat") 
		|| die "Cannot write to file $coedat: $!\n";
	foreach (@readline) {
		print COEDAT "$_\n"; 
	}
	close COEDAT;
}

exit $COEERR;