#!/usr/bin/perl
#
# $Header: //sapdb/V75/c_00/develop/sys/src/install/perl/packages/xie/instxie#2 $
# $DateTime: 2003/12/01 15:16:04 $
# $Change: 57901 $
#
# Desc:


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

$path{'name'} = 'XML Indexing Engine path';

# $path{'mode'}=0775;  => use default now (0555)
$path{'opt'}='xiepath=s';
$path{'list'} = 'xie.lst';
$path{'update'}=0;


$xie_ini = '/usr/spool/sql/ini/sapdbxie.ini';


$usage= " [-xiepath <instpath>]";
$usage_desc = "\t-xiepath <instpath>\t XML indexing engine installation directory\n";



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

local ($data,$prog);

sub preinstall{
	($data,$prog)=readIndepPath();
	$prog =~ /\S/ or $prog = $SAPDB::Install::Values::indep_prog_path;
	$data =~ /\S/ or $data = $SAPDB::Install::Values::indep_data_path;
	$prog =~ /\S/ or print2stderr("independent program path not found\n") and diesoft($SAPDB::Install::Values::diemsg);
	$path{'default'} = $prog.'/web';
	$path{'value'}=$prog.'/web' unless ($SAPDB::Install::StartInstall::opt_relocate);
	return 1;
}


sub postprepare{
	return 1;
}


sub postinstall{
	return 1;
}



sub registerWin{
	my $node = hostname();
	my $path=$path{'value'};
	my $regpath=$path;
	$regpath=~s/\/$//;

	my @tmp;
	my $log_path=getAppDataPath().'/sapdb/wa';
	$log_path =~ s/\\/\//g;

	unless(-d $log_path){
		makedir($log_path,0775) or print2sterr("cannot create log directory $log_path\n");
	}

	$log_path=~s/\//\\\\/g;
	$regpath=~s/\//\\\\/g;
	my @statbuf = stat($path.'/config/sapdbxie.reg');
	open(REG,$path.'/config/sapdbxie.reg') or print2stderr("cannot open \"$path/config/sapdbxie.reg\"\n") and diesoft($diemsg);
	my $version = $packobj->Version;
	while(chomp($_=<REG>)){
		s/\$VERSION\$*/$version/g;
		s/\$INSTROOT\$*/$regpath/g;
		s/\$LOG\$*/$log_path/g;
		s/\$NODE\$*/$node/g;
		push(@tmp,$_);
	}
	close(REG);
	open(REG,'>'.$path.'/config/sapdbxie.reg') or print2stderr("cannot open \"$path/config/sapdbxie.reg\" to write\n") and diesoft($SAPDB::Install::diemsg);
	foreach my $line (@tmp){
		print REG $line."\n";
	}
	close(REG);
	utime($statbuf[9],$statbuf[9],$path.'/config/sapdbxie.reg');
	$packobj->RegData->updateMd5Sum('config/sapdbxie.reg');

	setPathInEnv("$path/pgm");

	$path =~ s/\//\\/g;
	$prog =~ s/\//\\/g;


	if($packobj->Update){
		callsystem("\"$prog\\pgm\\regupd\" \"".$path."\\config\\sapdbxie.reg\" \"SOFTWARE\\SAP\\SAP DBTech\\XIE\"");
	}
	else{
		callsystem("regedit -s \"".$path."\\config\\sapdbxie.reg\"");
	}

	my $service_test = sub {
								my ($text)=@_;
								$text =~ /installed\s+successfully/ and return 1;
								if($text =~ /service\s+already\s+exists/m){
									callsystem ('sapdbxie -u',sub {return 1;},1);
									callsystem ('sapdbxie -i',sub {my ($text)=@_;$text =~ /installed\s+successfully/m and return 1; return 0;});
									return 1;
								}
								return 0;
						};
	my $cwd = getcwd();
	chdir($path."\\pgm");
	callsystem ('sapdbxie -i',$service_test);
	chdir($cwd);
	return 1;
}


sub registerUX{
	my $node = hostname();
	my $path=$path{'value'};
	my $regpath=$path;
	$regpath=~s/\/$//;
	my $version = $packobj->Version;
	my $lib = $regpath.'/lib';
	$lib .= '/lib64' if ($packobj->Mode eq '64' and $^O !~ /linux|osf/i);
	my $log_path = '/var/log/sapdb/wa';
	if(-d $log_path){
		chmod(0775,$log_path);
		chown($packobj->UID,$packobj->GID,$log_path);
	}
	else{
		makedir($log_path,0775,$packobj->UID,$packobj->GID) or print2stderr("cannot create log directory \"$log_path\"\n");
	}
	my @statbuf = stat($path.'/config/sapdbxie.ini');
	open(REG,$path.'/config/sapdbxie.ini') or print2stderr("cannot open \"$path/config/sapdbxie.ini\": $!\n") and diesoft($SAPDB::Install::diemsg);
	open(REGDEST,'>'.$path.'/config/sapdbxie.ini.tmp')  or print2stderr("cannot create file \"$path/config/sapdbxie.ini.tmp\": $!\n") and diesoft($SAPDB::Install::diemsg);
	while(<REG>){
		chomp;
		s/\$VERSION\$/$version/;
		s/\$LIB\$/$lib/g;
		s/\$LOG\$/$log_path/;
		s/\$INSTROOT\$/$regpath/;
		s/\$NODE\$/$node/g;
		print REGDEST $_."\n";
	}
	close(REG);
	close(REGDEST);
	unlink($path.'/config/sapdbxie.ini.tmp');
	rename($path.'/config/sapdbxie.ini.tmp',$path.'/config/sapdbxie.ini');
	utime ($statbuf[8],$statbuf[9],$path.'/config/sapdbxie.ini');
	chmod($statbuf[2],$path.'/config/sapdbxie.ini');
	chown($statbuf[4],$statbuf[5],$path.'/config/sapdbxie.ini');
	$packobj->RegData->updateMd5Sum('config/sapdbxie.ini');
	if($packobj->Update){
		callsystem("$prog/pgm/regupd $path/config/sapdbxie.ini $xie_ini");
	}
	else{
		copy($path.'/config/sapdbxie.ini',$xie_ini);
	}
	return 1;
}

sub register{

	#
	#	load Hostname module
	#

	my ($repo) = @SAPDB::Install::Repository::AllRepo;

	my @neededPackages = ('Hostname');

	foreach my $package (@neededPackages){
	  	unless (defined $repo->Eval ("SAPDB::Install::$package", 1.01)) {
                print join ("\n", $repo->GetErr)."\n";
                die ("error loading:  SAPDB::Install::$package\n");
        	}
		SAPDB::Install::Exporter::import ("SAPDB::Install::$package");
	}



	if($^O =~ /mswin/i){
		registerWin();
	}
	else{
		registerUX();
	}
}



sub unregisterWin{
	my $path = $regpackobj->Path;
	setPathInEnv("$path/pgm");
	$path =~ s/\//\\/g;

	my $service_test = sub {
								my ($text)=@_;
								$text =~ /removed/m and return 1;
								return 0;
						};
	callsystem ('sapdbxie -u',$service_test,1);
	return 1;
}

sub unregisterUX{
	rename($xie_ini,$xie_ini.'.last');
	return 1;
}


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


sub preuninstall{
	my $path = $regpackobj->Path;
	my $pid_file = $path.'/pgm/wahttp.pid';
	if(-f $pid_file){
			unlink($pid_file) or print2stderr("cannot remove file \"$pid_file\": $!\n");
	}
	unless($^O =~ /mswin/i){
		$pid_file = '/usr/spool/sql/pid/wahttp.pid';
		if(-f $pid_file){
			unlink($pid_file) or print2stderr("cannot remove file \"$pid_file\": $!\n");
		}
	}
	return 1;
}

sub postuninstall{
	if($^O !~ /mswin/i){
		foreach my $file ($xie_ini,$xie_ini.'.last'){
			-e $file or next;
			unlink($file) or print2stderr("cannot remove $file: $!\n");
		}
		my $logpath = ($^O =~ /mswin/i) ? getAppDataPath().'/sapdb/wa' : '/var/log/sapdb/wa';
		if (-d $logpath){
			foreach (grep {/\.log$/} find($logpath,'f')){
				unlink || print2stderr("cannot remove file \"$_\"\n");
			}
			my ($rc,$deleted,$undeleted) = removeEmptyDirs($logpath);
			if(defined $undeleted and ref($undeleted) eq 'ARRAY'){
				foreach (@$undeleted){
					print2stderr("cannot remove directory $_\n");
				}
			}
		}
	}
	else{
		removeKey({'name' => 'HKEY_LOCAL_MACHINE'},'Software\SAP\SAP DBTech\XIE',2)
			or print2stderr("cannot remove registry key \"HKEY_LOCAL_MACHINE\\Software\\SAP\\SAP DBTech\\XIE\"\n");
	}
	return 1;
}

1;

