#!/h/COE/Comp/PERL5/bin/perl -T -I/h/COE/lib  
#-
 # Copyright (C) 2002, Federal Linux Systems
 # License: GNU Lesser General Public License, v2.1
#- 
 # Suppress warnings on all "get" calls

use strict;
use Coe;

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

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

sub useage {
	print "[options] [{-p \"\"} ";
	print "{-sp } {-su } {-u }]\n";
}

my $uscope="all";
my $pscope="all";
my $profile="all";
my $userid="all";
my $arg1;
my $i;
my $coedat;
my $coedatstr;
my $COEDAT;
my $flaguser="false";
my $flagprofile="false";
my $flaguserprof="false";

sub readUserProf;

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 "-sp") {
		$pscope=cleanString(shift,1);
	} elsif ("$arg1" eq "-su") {
		$uscope=cleanString(shift,1);
	} elsif ("$arg1" eq "-u") {
		$userid=cleanString(shift,1);
	} elsif ("$arg1" eq "-p") {
		$profile=cleanString(shift,1);
	} else {
		useage;
   		exit 202;
	}
}

if ("$uscope" eq "l" || "$uscope" eq "local") {
	$uscope = "local";
} elsif ("$uscope" eq "g" || "$uscope" eq "global") {
	$uscope = "global";
} else {
	$uscope = "all";
}

if ("$pscope" eq "l" || "$pscope" eq "local") {
	$pscope = "local";
} elsif ("$pscope" eq "g" || "$pscope" eq "global") {
	$pscope = "global";
} else {
	$uscope = "all";
}

# two keys to this: 1=UID, 2=Profile
# uid:username:uscope:profile:pscope:plock:abbrev:acct group

if ("$uscope" ne "global") {
	if ("$pscope" ne "global") {
		readUserProf $userid, "local", $profile, "local";
	}
	if ("$pscope" ne "local") {
		readUserProf $userid, "local", $profile, "global";
	}
}

if ("$uscope" ne "local") {
	if ("$pscope" ne "global") {
		readUserProf $userid, "global", $profile, "local";
	}
	if ("$pscope" ne "local") {
		readUserProf $userid, "global", $profile, "global";
	}
}

	
if ("$flaguser" eq "false") {
	exit 203;
} elsif ("$flaguserprof" eq "false") {
	exit 208;
} elsif ("$flagprofile" eq "false") {
	exit 204;
} else {
	exit 0;
}


# ------------------------------------------------------------------------------

sub readUserProf {
	my ($uid, $uscp, $pro, $pscp)=@_;
	my $su="local";
	my $sp="local";

	my @luid;
	my @guid;
	my @d;
	my @dat;
	my @p;
	my @prof;
	my @u;

	if ("$uscp" eq "local") {$su="l";}
	if ("$uscp" eq "global") {$su="g";}
	if ("$pscp" eq "local") {$sp="l";}
	if ("$pscp" eq "global") {$sp="g";}

	# get the UIDs
	$i=0;
	$coedat=coeFileConv("COE_USER/$uscp/Profiles/.User.dat");
	$COEDAT=readFile($coedat);
	if ("$uid" eq "all") {
		while (<$COEDAT>) { $luid[$i++]=cleanString($_,3); }
		$flaguser="true";
	} else {
		while (<$COEDAT>) { 
			if (/^$uid:/) {
				$luid[$i++]=cleanString($_,3); 
				$flaguser="true";
			}
		}
	}
	close $COEDAT;
	
	# get the profiles assigned to this profile
	foreach (@luid) {
		@u=split(':');
		
		# get the profiles for this uid
		$i=0;
		$#prof=0;
		$coedat=coeFileConv("COE_USER/$pscp/Profiles/.UserProfile.dat");
		$COEDAT=readFile($coedat);
		while (<$COEDAT>) { 
			chomp();
			if ("$pro" eq "all" && /^$u[0]:$uscp:/) { 
				$prof[$i++]=cleanString($_,3); 
				$flaguserprof="true";
			} elsif (/^$u[0]:$uscp:$pro$/) { 
				$prof[$i++]=cleanString($_,3); 
				$flaguserprof="true";
			}
		}
		close $COEDAT;
	
		foreach (@prof) {
			@p=split(':');
	
			if ("$p[2]" ne "" ) {
				# get the data for this profile
				$i=0;
				$#dat=0;
				@dat={"","","","","","","","","","",""};
				$coedat=
			  	coeFileConv
				   ("COE_USER/$pscp/Profiles/.Profile.dat");
				$COEDAT=readFile($coedat);
				while (<$COEDAT>) { 
					if (/^$p[2]:/) { 
						$dat[$i++]=cleanString($_,3); 
						$flagprofile="true";
					}
				}
				close $COEDAT;
		
				foreach (@dat) {
					@d=split(':');
			      		print "$u[0]:$u[1]:$su:$p[2]:$sp:";
					print "$d[2]:$d[3]:$d[1]\n";
				}
			}
		}
	}	

}