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

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

sub useage {
 	print "Useage: $0 [options] {Profile} {Scope} {-f} {-f} ...\n";
}


my $arg1;
my $newstr;
my $newval;
my $oldval;
my $profile="0";
my $pscope="a";
my $coedat;
my $COEDAT;
my $i;
my @oldval;
my @newval;
my @profdat;
my @fieldval;

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);
		$i=0;
		while (@ARGV > 0) {
			# the argument is formatted "-f"
			# so I am parsing the first arg for N
			# and using that to define array postion N+3 with aflag
			
			# I should only allow for reserved num 1-6

			# The +3 normalizes the string positions 4-9 
			#   where 0 is first string field

			# zremq is just a nonsense string used as a flag

			$arg1=cleanString(shift,1);
			$_=$arg1;
			s/-f//;
			$i=$_;
			$fieldval[$i+3]="zremq";
		}
	}
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# 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;
}

# does this acct group exist?

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

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

# Profile.dat format:: profile:acctgrp:lockflag:abbrev::::::

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# cat out the User.dat COEDAT, removing the user string for alteration
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$i=0;
$COEDAT=readFile ($coedat);
while (<$COEDAT>) { 
	if (/^$profile:/) { 
		chomp();
		@oldval=split(':'); 
	} else {
		$profdat[$i++]=cleanString($_,3);
	}
}
close $COEDAT;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# construct a new user string to replace the old one nulling defined fields
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# if a new value is defined, use it, else default to the old value
		$newval[0]=$oldval[0];
		$newval[1]=$oldval[1];
		$newval[2]=$oldval[2];
		$newval[3]=$oldval[3];
$i=4;
while ($i < 10) {
	if ("$fieldval[$i]" eq "zremq") {
		$newval[$i]="";
	} else {
		$newval[$i]=$oldval[$i];
	}
	$i++;
}

# build the new user string
$i=1;
	$newstr=$newval[0];
while ($i < 10) {
	$newstr=$newstr.":".$newval[$i++];
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# rewrite the COEDAT with the old data and the new user string
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

open COEDAT, (">$coedat") || die "Cannot open COEDAT $coedat: $!\n";
foreach (@profdat) {
	print COEDAT "$_\n"; 
}
print COEDAT "$newstr\n"; 
close COEDAT;