#!/usr/bin/perl
#
# $Header: //sapdb/V75/c_00/b_24/sys/src/install/perl/packages/pcr/instpcr#1 $
# $DateTime: 2005/02/18 17:24:39 $
# $Change: 90789 $
#
# Desc:


import SAPDB::Install::StdIO;
import SAPDB::Install::System;
import SAPDB::Install::Registry;
import SAPDB::Install::Tools;
import SAPDB::Install::SetUser;


$path{'name'} = 'precompiler PACKVERS path';
$path{'default'} = getSysProgPath().'/sdb/data';
$path{'mode'}=0775;
$path{'opt'}='pcr_PACKVERS=s';

unless($SAPDB::Install::StartInstall::opt_INSTANCE){ # these option makes no sense while instance upgrade
	$usage= " [-pcr_PACKVERS <precompiler runtime PACKVERS path>]";
	$usage_desc = "\t-pcr_PACKVERS <precompiler runtime PACKVERS path>\tinstallation root for pcr PACKVERS\n";
}

@paths = (\%path);
$main_path=\%path;
@opts=();

my $data;
my $prog;

sub preinstall{
    local @ARGV=@_; 
    ($data,$prog)=readIndepPath();
	$prog !~ /\S/ and $prog = $SAPDB::Install::Values::indep_prog_path;

	$prog !~ /\S/ and print2stderr("independent program path not found\n") and
	diesoft($SAPDB::Install::Values::diemsg);

	$data !~ /\S/ and $data = $SAPDB::Install::Values::indep_data_path;
 	$path{'value'}=$prog;
}
 
sub postinstall{
}

sub registerWin{
	my $path = $prog{'value'};
	$prog=~s/\//\\/g;

	my $callstring =
	(PACKVERS >= 7500) ?
		"\"$prog\\runtime\\PACKVERS\\bin\\irconf\" -g -i -p \"$prog\\runtime\\PACKVERS\"" :
		"\"$prog\\runtime\\PACKVERS\\bin\\irconf\" -i -p \"$prog\\runtime\\PACKVERS\"";

	$callstring=~s/\//\\/g;
	$callstring=~s/%/\\/g;
	callsystem($callstring);
}

sub registerUX{
	my $inifile = $data.'/config/Runtimes.ini';

	if (-f $inifile) {
		chmod (0444, $inifile);
		chown ($packobj->UID,$packobj->GID,$inifile);
	}

	if(SetUser($packobj->UID) == -1){
		print2stderr("cannot set effective uid and gid\n");
		diesoft($SAPDB::Install::Values::diemsg);
    }
    else{
		$SAPDB::Install::Values::log->SetMsg
		("MSG: switch effective uid successfully\n");
    }
	
	callsystem('id',sub{return 1;},1);

	my $callstring =
	(PACKVERS >= 7404) ?
		"$prog/runtime/PACKVERS/bin/irconf -g -i -p $prog/runtime/PACKVERS" :
		"$prog/runtime/PACKVERS/bin/irconf -i -p $prog/runtime/PACKVERS";

	callsystem($callstring);

	if(SetUser() == -1){
		print2stderr("cannot set effective uid and gid back to root\n");
    }
    else{
		$SAPDB::Install::Values::log->SetMsg
		("MSG: switch back effective uid successfully\n");
    }
}

*register = $^O =~ /^MsWin/i ? \&registerWin : \&registerUX;


sub unregisterWin{
	($data,$prog)=readIndepPath();
	$prog=~s/\//\\/g;

	my $callstring =
	(PACKVERS >= 7404) ?
		"\"$prog\\runtime\\PACKVERS\\bin\\irconf\" -g -r -p \"$prog\\runtime\\PACKVERS\"" :
		"\"$prog\\runtime\\PACKVERS\\bin\\irconf\" -r -p \"$prog\\runtime\\PACKVERS\"";

	$callstring=~s/\//\\/g;
	callsystem($callstring,undef,1);

}

sub unregisterUX{
	($data,$prog)=readIndepPath();

	my $callstring =
	(PACKVERS >= 7404) ?
		"$prog/runtime/PACKVERS/bin/irconf -g -r -p $prog/runtime/PACKVERS" :
		"$prog/runtime/PACKVERS/bin/irconf -r -p $prog/runtime/PACKVERS";

	callsystem($callstring,undef,1);

}

*unregister =  $^O =~ /^MsWin/i ? \&unregisterWin : \&unregisterUX;

sub preuninstall{

}

sub postuninstall{

}


sub verify{
	my %runtimes = readRuntimes();
	my ($found,$version_ok) = (0,0);
	foreach my $pcr_path (keys(%runtimes)){
		if(normalizePath($regpackobj->Path.'/runtime/PACKVERS') eq normalizePath($pcr_path)){
			$found = 1;
			if(release2num($regpackobj->Version) == release2num($runtimes{$pcr_path})){
				$version_ok = 1;
			}
			else{
				$SAPDB::Install::Values::log->SetMsg
				("version dont match: ".$regpackobj->Version." != ".$runtimes{$pcr_path}."\n");			
			}
			last;
		}
	}
	$SAPDB::Install::Values::log->SetMsg("registration not found\n") unless $found;
	return ($found && $version_ok);
}


1;
                                                                                                         