#!/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
#- 
 # Warnings still generated

use strict;
use Coe;

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

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

sub useage {
 	print "Useage: $PRGM [options]   ";
	print "-f \"\" [-f \"\"]*\n";
}


my $i=0;
my $arg1;
my $newstr;
my $newval;
my $oldval;
my $userid="0";
my $uscope="a";
my @fieldval=("","","","","","","","","","");
my @oldval=("","","","","","","","","","");
my @newval;
my @userdat;

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

			# The +2 normalizes the string positions 3-8 
			#   where 0 is first string field

			$arg1=cleanString(shift,1);
			$_=$arg1;
			s/-f//;
			$i=$_;
			$fieldval[$i+2]=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
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

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

# User.dat format:: userid:$username:$userdesc::::::
#$userstr="$userid:$username:$userdesc:$fieldval[1]:$fieldval[2]:$fieldval[3]:$fieldval[4]:$fieldval[5]:$fieldval[6]\n";

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# cat out the User.dat COEDAT, removing the user string for alteration
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$i=0;
#open COEDAT, "$coedat" || die "Cannot open COEDAT $coedat: $!\n";
my $COEDAT = readFile ($coedat);
while (<$COEDAT>) { 
	if (/^$userid:/) { 
		@oldval=split(':'); 
	} else {
		$userdat[$i++]=$_;
	}
}
close $COEDAT;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# if no such user is found, exit with COE_NO_SUCH_USER
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

if ("$oldval[0]" eq "" ) {
	exit 203;
}

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

# 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=@oldval;
$i=3;
while ($i < 9) {
	if ("$fieldval[$i]" ne "") {
		$newval[$i]=$fieldval[$i];
	} else {
		$newval[$i]=$oldval[$i];
	}
	$i++;
}

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

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

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

exit 0;