#!/h/COE/Comp/PERL5/bin/perl -T -I/h/COE/lib  -U
#-
 # Copyright (C) 2002, Federal Linux Systems
 # License: GNU Lesser General Public License, v2.1
#- 

#- use -U for writing to linked directories
#- drop -w for warning of insecure open of LastProfiles

use strict;
use Coe;
use Sys::Hostname;

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

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

sub useage {
 	print "Useage: COE_delete_current_profile \n";
}


my $profile="";
my $pscope="";
my $arg1;
my $flagprof="false";
my $lastprofiles;
my @profdat;
my $home;
my $hostname;
my $display;
my $i;
my @tmp;

while (@ARGV > 0 ){
	$arg1=cleanString(shift,3);
	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 {
		$_=$arg1;
		@tmp=split(":");
		$profile=$tmp[0];
		$pscope=$tmp[1];
	}
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# do simple sanity checks
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# is this a legal scope ?
if ( "$pscope" eq "local" || "$pscope" eq "l" ) {
	$pscope="l";
} elsif ("$pscope" eq "global" || "$pscope" eq "g" )  {
	$pscope="g";
} else {
	&useage;
	exit 202;
}

# does this acct group exist?

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

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# (1) LastProfiles
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

# define path/file info
$home=$ENV{'HOME'};

$hostname=hostname();

$_=$ENV{'DISPLAY'};
s/^.*://;
s/\..*$//;
$display=$_;
if ("$display" eq "" ) {
        $display="0";
}

#- Place the LastProfiles file into the users home directory, unless DII COE
$lastprofiles = coeFileConv("$home/LastProfiles_".$hostname."_".$display, 3);

#- In a strict DII COE environment, the following LastProfiles should be used
#$lastprofiles =coeFileConv("$home/../LastProfiles_".$hostname."_".$display, 3);

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# if the user id is found, remove it from the file
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

$i=0;
if (-e $lastprofiles) {
	open LASTPROF, "$lastprofiles" 
		|| die "Cannot open file $lastprofiles: $!\n";
	while () { 
		if (! /^$profile:$pscope/) { 
			$profdat[$i++]=cleanString($_,3); 
		}
	}
	close LASTPROF;
}

open LASTPROF, ">$lastprofiles" 
	|| die "Cannot open file $lastprofiles: $!\n";
foreach (@profdat){
	print LASTPROF "$_\n"; 
}
close LASTPROF;
exit 0;