#!/devtool/local/bin/perl
#
# $Header: //sapdb/TOOLSRC/develop/sys/src/tt/BuildPackageNew.pm.tt#3 $
# $DateTime: 2001/08/10 13:44:46 $
# $Change: 7377 $
#
# Desc: tool to build several sap db packages

package BuildPackageNew;

use Getopt::Long;

sub start{
	local @ARGV=@_;

	my %opt_ctrl=(
			'h' => \$opt_help,
			'help' => \$opt_help,
			'profile=s' => \$opt_profile,
			'package=s' => \$opt_package,
			'format=s' => \$opt_format,
			'ignore_errors' => \$opt_ignore_errors
	);

	if(! GetOptions(%opt_ctrl) or $opt_help){
		print "Usage: buildPackage [-h] [-profile <profile>] [-package <package list>] [-format tgz|sar]\n\n";
		exit 0;
	}

	# tiny helper functions

	sub err{
		my ($text)=@_;
		if($opt_ignore_errors){
			print STDERR "ERR: $text";
		}
		else{
			die("ERR: $text");
		}
	}

	sub existInList{
		my ($packname,@packages)=@_;
		foreach my $hrPack (@packages){
			$packname eq ${%$hrPack}{'name'} and return 1;
		}
		return 0;
	}


	my $wrk=$ENV{'WRK'};
	$wrk=~s/\\/\//g;
	my $own=$ENV{'OWN'};
	$own=~s/\\/\//g;
	my $relver=$ENV{'RELVER'};
	my $rel = $relver;
	$rel=~s/R//;
	my $bit = '32';
	$bit = '64' if $ENV{'BIT64'};
	my $correction_level=$ENV{'CORRECTION_LEVEL'};
	my $cor = $correction_level;
	$cor = "0$correction_level" if length($correction_level) == 1;
	my $buildpraefix=$ENV{'BUILDPRAEFIX'};
	my $release = $relver;
	$release =~ s/R(\d)(\d)/$1\.$2/;
	$release="$release.$correction_level.$buildpraefix";
	my $buildroot="$wrk/fast/install/buildroot";
	my $install = "$wrk/fast/install";
	my $packpath="$install/packages";
	my $temp= "$install/tmp";
	my $usr="$own/usr";


	my @packages;

	#define replacements -> for replacing strings in signature file and package script

	my %replacements = (
		'§RELEASE' => $release,
		'§PACKVERS' => "$rel$cor", # used for names/pathes of PCR and SDK packages
		'§BIT' => $bit
	);


	#define profiles -> contain package names
	my %profiles=(
			'Server' => ['Base','Server Utilities','Database Kernel'],
			'Client' => ['Base',"PCR $rel$cor"],
			'Web' => ['Base','Web'],
			'DB Analizer' => ['Base','DB Analizer'],
			'Develop' => ['Base',"PCR $rel$cor","SDK $rel$cor",'ODBC','JDBC','Perl','Python']
	);



	#define packages
	my @allpackages=(
		{
			'name' => 'Base',
			'subdir' => 'base',
			'script' => 'instbase',
			'archive' => 'SAPDBBAS.TGZ',
			'filelist' => 'base.lst'
		},
		{
			'name' => 'Server Utilities',
			'subdir' => 'utils',
			'script' => 'instutils',
			'archive' => 'SAPDBUTL.TGZ',
			'filelist' => 'utils.lst'
		},
		{
			'name' => 'Database Kernel',
			'subdir' => 'kernel',
			'script' => 'instsrv',
			'archive' => 'SAPDBKRN.TGZ',
			'filelist' => 'CAR.LISTE.SRV'
		},
		{
			'name' => "PCR $rel$cor",
			'subdir' => 'pcr',
			'script' => 'instpcr',
			'archive' => "PCR$rel$cor.TGZ",
			'filelist' => 'pcr.lst'
		},
		{
			'name' => 'DB Analizer',
			'subdir' => 'dba',
			'script' => 'instdba',
			'archive' => 'SAPDBA.TGZ',
			'filelist' => 'dba.lst'
		},
		{
			'name' => "SDK $rel$cor",
			'subdir' => 'sdk',
			'script' => 'instsdk',
			'archive' => "SDK$rel$cor.TGZ",
			'filelist' => 'sdk.lst'
		},
		{
			'name' => 'Web',
			'subdir' => 'web',
			'script' => 'instweb',
			'archive' => 'SAPDBWEB.TGZ',
			'filelist' => 'webagent.lst'
		},
		{
			'name' => 'ODBC',
			'subdir' => 'odbc',
			'script' => 'instodbc',
			'archive' => 'ODBC.TGZ',
			'filelist' => 'odbc.lst'
		},
		{
			'name' => 'JDBC',
			'subdir' => 'jdbc',
			'script' => 'instjdbc',
			'archive' => 'JDBC.TGZ',
			'filelist' => 'jdbc.lst'
		},
		{
			'name' => 'Perl',
			'subdir' => 'perl',
			'script' => 'instperl',
			'archive' => 'PERLIF.TGZ',
			'filelist' => 'perl.lst'
		},
		{
			'name' => 'Python',
			'subdir' => 'python',
			'script' => 'instpy',
			'archive' => 'PYTHONIF.TGZ',
			'filelist' => 'python.lst'
		}

	);


	# select packages to build
	# with profile option

	if($opt_profile){
		my @profiles = split(',',$opt_profile);
		foreach my $profile (@profiles){
			exists $profiles{$profile} or err ("no such profile: \"".$profile."\"\n");
			my @prof_pack;
			foreach my $pack (@{$profiles{$profile}}){
				my $found = 0;
				foreach my $packref (@allpackages){
					if($pack eq ${%$packref}{'name'}){
						$found=1;
						existInList($pack,@packages) and last;
						push @packages,$packref;
						last;
					}
				}
				if($found){
					push @prof_pack,$pack;
				}
				else{
					err("no such package: \"$pack\"\n");
				}
			}
			print "found for \"$profile\":\n";
			foreach my $packnam (@prof_pack){
				print "\t$packnam\n";
			}
			print "\n";
		}



	}

	# select packages to build
	# with package option

	if($opt_package){
		my @packs = split(',',$opt_package);
		foreach my $pack (@packs){
			my $found = 0;
			foreach my $packref (@allpackages){
				if($pack eq ${%$packref}{'name'}){
					push @packages,$packref;
						$found=1;
					last;
				}
			}
			$found || err("no such package: \"$pack\"\n");
		}
	}

	# select all packages to build if no option set
	if($#packages == -1){
		@packages=@allpackages;
	}


	# print out packages
	print "try to build following packages: \n";
	foreach $hrPack (@packages){
		print "\t".${%$hrPack}{'name'}."\n";
	}
	print "\n";



	# test needed files:
	foreach my $package (@packages){
		foreach my $file (${%$package}{'filelist'},${%$package}{'script'},'PACKAGEDATA'){
			-f $packpath.'/'.${%$package}{'subdir'}.'/'.$file || err($packpath.'/'.${%$package}{'subdir'}.'/'.$file.' [package "'.${%$package}{'name'}."\"] not found\n");
		}
	}


	# build packages
	foreach my $package (@packages){

		# make sure that there is no old stuff
		`rm -rf $buildroot`;
		-d $buildroot and err("cannot remove buildroot\n");
		`mkdir -p $buildroot`;
		-d $buildroot or err("cannot create buildroot\n");

		# copy and change script and signature
		foreach my $file (${%$package}{'script'},'PACKAGEDATA'){
			open(IN,$packpath.'/'.${%$package}{'subdir'}.'/'.$file) or err('cannot open '.$packpath.'/'.${%$package}{'subdir'}."/$file\n");
			open (OUT,">$buildroot/$file") or err("cannot create $buildroot/$file\n");
			while(<IN>){
				chomp;
				foreach my $string (keys(%replacements)){
					s/$string/$replacements{$string}/g;
				}
				print OUT "$_\n" or err("cannot write into $buildroot/$file\n");
			}
			close(IN);
			close(OUT);
		}

		# copy files, generate md5sums and change filelist (attach checksums)
		open(IN,$packpath.'/'.${%$package}{'subdir'}.'/'.${%$package}{'filelist'}) or err('cannot open '.$packpath.'/'.${%$package}{'subdir'}.'/'.${%$package}{'filelist'}."\n");
		open(OUT,">$buildroot/".${%$package}{'filelist'}) or err("cannot create $buildroot/".${%$package}{'filelist'}."\n");
		while(<IN>){
			chomp;
			/^#/ && next;
			my $src="$usr/$_";
			my $dst="$buildroot/$_";
			my $dir = $dst;
			$dir=~s/\/[^\/]*$//;
			-d $dir or `mkdir -p $dir`;
			-d $dir or err("cannot create directory $dir\n");
			if($^O=~/mswin/i){
				`cp $src $dst`;
			}
			else{
				`cp -p $src $dst`;
			}
			-f $dst or err("cannot copy $src\n");
			my $md5sum=`md5sum $dst`;
			$md5sum=~s/\s.*$//;
			$md5sum=~/^[a-f0-9]{32}$/ or err("cannot get md5sum of $dst\n");
			print OUT "\"$_\" $md5sum\n" or err("cannot write into $buildroot/".${%$package}{'filelist'}."\n");
		}
		close(IN);
		close(OUT);
		chdir("$buildroot") or err("cannot change into directory $buildroot\n");
		my $tarname="$install/".${%$package}{'archive'};


		# generate list of files and folders to pack -> order is important for performance of installer
		my $packlist = 'PACKAGEDATA '.${%$package}{'script'}.' ' .${%$package}{'filelist'};
		opendir(DH,'.') || err("cannot open directory: $!\n");
		my @content = readdir(DH);
		close(DH);
		foreach (@content){
			(/^PACKAGEDATA$/ or /^${%$package}{'script'}$/ or /^${%$package}{'filelist'}$/ or /^\.{1,2}$/) and next;
			$packlist="$packlist $_";
		}

		# pack archive
		print "pack archive $tarname\n";
		#print "COMMAND: tar -cvpzf $tarname $packlist\n";
		`tar -cvpzf $tarname $packlist`;
		$? == 0 or err("tar error while packing \"".${%$package}{'name'}."\"\n");
		-f $tarname or err("no archive \"$tarname\" produced\n");
	}


	# pack main package
	# $packagename="sapdb";

	#my $ext='tgz';

	#if($opt_format eq 'sar'){
	#	$ext='sar';
	#	chdir($install) or err("cannot change into dir $install\n");
	#	$packagename="$install/$packagename.$ext";
	#	`SAPCAR -cvf $packagename *`;
	#}
	#else{
	#	$packagename="$install/$packagename.$ext";
	#		`tar -cvpzf	$packagename *`;
	#}
	#if(-f $packagename){
	#	print "packed $packagename successfully\n";
	#}
	#else{
	#	err("cannot pack mainpackage\n");
	#}
}

1;






