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

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

sub useage {
 	print "Useage: $PRGM [options] { -a \" 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 "-a") {
		$alabel=cleanString(shift,1);
		$acctgrp="all";
		$ascope=cleanString(shift,1);
	} elsif ("$arg1" eq "-g") {
		$alabel="all";
		$acctgrp=cleanString(shift,1);
		$ascope=cleanString(shift,1);
	} else {
		useage;
		exit 202;
	}
}

# do some simple sanity checks
if ( "$ascope" eq "l" || "$ascope" eq "local" )  {
	$ascope="local";
} elsif ( "$ascope" eq "g" || "$ascope" eq "global" )  {
	$ascope="global";
} else {
	$ascope="all";
}


#- for local or all scopes, delete app data
if ("$ascope" ne "global") {
	$lerr=deleteAppData "local", $alabel, $acctgrp;
}

#- for global or all scopes, delete app data
if ("$ascope" ne "local") {
	$gerr=deleteAppData "global", $alabel, $acctgrp;
}


#- calculate error return based on above subroutine returns
if ($lerr == 0 || $gerr == 0) {
	exit 0;
} elsif ($gerr != 0 && $gerr != 201) {
	exit $gerr;
} elsif ($lerr != 0 && $lerr != 201) {
	exit $lerr;
} else {
	exit 201;
}

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

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
sub deleteAppData {
	my ($scope, $app, $acct) = @_; 
	my $flagapp="false";
	my @readline;
	my $i=0;
	my $APPDAT;
	my $appdat;
	my @a;

	sub deleteProfileAppData;

	# path for the AppData data COEDAT
	$appdat=coeFileConv "COE_USER/$scope/Profiles/.AppData.dat";

	# AppData.dat format:: label:name:acctgrp:exec:args:icon

	# if the application label and acct group are unique, 
	#  append it to the COEDAT 

	$i=0;
	#$APPDAT=readFile($appdat);
	open APPDAT, "$appdat" || 
		die "Cannot open $appdat: $_\n";
	while () { 
		if ("$app" eq "all") {
			if (! /:$acct:/) { 
				$readline[$i++]=cleanString($_,3); 
			} else {
				# - set the app found flag
				$flagapp="true";

				# find this app name 
				# and delete from the profile/app assigments
				@a=split(":");
				&deleteProfileAppData ($scope, "all", $a[0]); 
			}
		} else {
			if (! /^$app:/) { 
				$readline[$i++]=cleanString($_,3); 
			} else {
				# - set the app found flag
				$flagapp="true";

				# find this app name 
				# and delete from the profile/app assigments
				@a=split(":");
				&deleteProfileAppData ($scope, "all", $a[0]); 
			}
		}
	}
	close APPDAT;
	
	#- if file contained app, rewrite without that info
	if ("$flagapp" eq "true" ) {
		open APPDAT, (">$appdat") 
			|| die "Cannot open COEDAT $appdat: $!\n";
		foreach (@readline) {
			print APPDAT "$_\n"; 
		}
		close APPDAT;
	}

	if ("$flagapp" eq "true"){
		return 0;
	} else {
		return 205;
	}
}


# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
sub deleteProfileAppData {
	my ($scope, $prof, $app) = @_; 
	my $flagprof="false";
	my @readline;
	my $i=0;
	my $PROFAPPDAT;
	my $profappdat;

	# Need to add a counter for global files.

	# path for the ProfileApp data COEDAT
	$profappdat=coeFileConv "COE_USER/$scope/Profiles/.ProfileApp.dat";

	# ProfileApp.dat format:: profilename:appname

	$i=0;
	$PROFAPPDAT=readFile($profappdat);
	while (<$PROFAPPDAT>) { 
		chomp();
		if (! /^$prof:$app$/) { 
			$readline[$i++]=cleanString($_,3); 
		} else {
			$flagprof="true";
		}
	}
	close $PROFAPPDAT;
	
	#- if file contained profile/app, rewrite without that info
	if ("$flagprof" eq "true" ) {
		open PROFAPPDAT, (">$profappdat") 
			|| die "Cannot open PROFAPPDAT $profappdat: $!\n";
		foreach (@readline) {
			print PROFAPPDAT "$_\n"; 
		}
		close PROFAPPDAT;
	}

	if ("$flagprof" eq "true"){
		return 0;
	} else {
		return 209;
	}
}