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

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

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


my $abbrev="a";
my $acctgrp="all";
my $plock="0";
my $profile="dummy";
my $pscope="a";
my $arg1;
my $flag="false";
my $COEERR=0;

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;
	} elsif ("$arg1" eq "-l") {
		# this is a plocking profile
		$plock="1";
	} else {
		$profile=$arg1;
		$acctgrp=cleanString(shift,1);
		$abbrev=cleanString(shift,1);
		$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 acct group exist?

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

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

# Profile.dat format:: profilename:acctgrp:pscope:plockflag:profileabbrev::::::
my $coedatstr=$profile.":".$acctgrp.":".$plock.":".$abbrev."::::::\n";

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# if the profile name and acct group are unique, append it to the file 
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

#open file, "$PROFDAT" || die "Cannot open file $PROFDAT: $!\n";
my $COEDAT = readFile ($coedat);
while (<$COEDAT>) { if (/^$profile:$acctgrp/) { $flag="true"; }}
close $COEDAT;

if ("$flag" eq "false") {
	open COEDAT, (">>$coedat") || die "Cannot open file $coedat: $!\n";
	print COEDAT "$coedatstr"; 
	close COEDAT;
} else {
	#- a duplicate profile error
	$COEERR=213;
}

exit $COEERR;