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

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

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

my $a;
my $arg1;
my $coedat;
my $COEDAT;
my $COEERR;
my $i;
my $profile;
my $scope;

my @a;
my @app;
my @profapp;

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 {
		$profile=$arg1;
	 	$scope=cleanString(shift,1);
        }
}

# Get the appropriate ProfileApp.dat COEDAT for this scope
if ("$scope" eq "l" || "$scope" eq "local") {
	$scope="local";
} elsif ("$scope" eq "g" || "$scope" eq "global") {
	$scope="global";
} else {
	useage;
	exit 202;
}


# Grep for the Profile in the ProfileApp.dat COEDAT
# And then strip the profile name leaving just the name of the app

$i=0;
$coedat=coeFileConv "COE_USER/$scope/Profiles/.ProfileApp.dat";
$COEDAT=readFile($coedat);
while (<$COEDAT>) { chomp(); if (/^$profile:/) { s/^.*://; $app[$i++]=$_; } }
close $COEDAT;

#foreach (@app) { print "$_";}

# Get details for the Applications assigned to this profile
$i=0;
$coedat=coeFileConv "COE_USER/$scope/Profiles/.AppData.dat";

foreach (@app) { 
	$a=cleanString($_,3);
	$COEDAT=readFile($coedat);
	while (<$COEDAT>) {  if (/^$a:/) { $profapp[$i++]=$_; } }
	close $COEDAT;
}

#- set COEERR to no app/profiles found
$COEERR=209;

foreach (@profapp) { 
	$_=cleanString($_,3);
	@a=split(':'); 
	print "$a[0]:$a[1]:$a[2]:$a[5]:$a[3]:$a[4]\n";
	#- reset COEERR to success
	$COEERR=0;
}

exit $COEERR;