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

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

sub useage {
   	print "Useage: COE_get_profile_data [options] ";
	print "[{-f} {-p \"\"} {-s }]\n";
}

my $pscope="a";
my $pscopeflag="all";
my $scope;
my $profile="*";
my $arg1;
my $i;
my $s;
my $coedat;
my $COEDAT;
my $COEERR=0;
my $flagreserved="false";
my @prof;
my @p=("","","","","","","","","","");;

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 "-f") {
		$flagreserved="true";
	} elsif ("$arg1" eq "-s") {
		$pscope=cleanString(shift,1);
	} elsif ("$arg1" eq "-p") {
		$profile=cleanString(shift,3);
	} else {
		useage;
   		exit 202;
	}
}


if ("$pscope" eq "l"  || "$pscope" eq "local") {
	$pscope="local";
	$s="l";
} elsif ("$pscope" eq "g"  || "$pscope" eq "global") {
	$pscope="global";
	$s="g";
#- default to local for now
} elsif ("$pscope" eq "a" || "$pscope" eq "all" ) {
	$pscope="local";
	$s="l";
} else {
	useage;
	exit 202;
}
	
# format wildcards "*" for regexp ".*"
$_=$profile;
if (/\*/) {
        s/\*/.*/g;
        $profile=$_;
} 

# profilename:accountgroup:lockable:abbr

# get the Profiles
$i=0;
$coedat=coeFileConv "COE_USER/$pscope/Profiles/.Profile.dat";
$COEDAT=readFile($coedat);


if ("$profile" eq "all"){
        while (<$COEDAT>) { $prof[$i++]=$_; }
} else {
        while (<$COEDAT>){ if (/^$profile:/ ) { $prof[$i++]=$_; }}
}
close $COEDAT;


foreach (@prof) {
	@p=split(':');
	if ("$flagreserved" eq "true") {
   		print "$p[0]:$p[1]:$s:$p[2]:$p[3]";
		print ":$p[4]:$p[5]:$p[6]:$p[7]:$p[8]:$p[9]";
	} else {
   		print "$p[0]:$p[1]:$s:$p[2]:$p[3]\n";
	}
	#- reset error exit for success
}	

if ($i == 0) {
	$COEERR=204;
}

exit $COEERR;