#!/usr/bin/perl

use Symbol;

my @files_ind = qw (
	bin/dbmcli-HelpInst
	bin/niserver
	bin/x_maketi
	bin/x_server
	bin/xvttest
	pgm/maketi
	pgm/niserver
	pgm/vserver
	pgm/vttest
	terminfo/
);

my @files_pre = qw (
	bin/cpc
	bin/cpclnk
	bin/sqllist
	env/de/PRECOMM.de
	env/de/SQLM.de
	env/de/SQLMESS.de
	env/en/PRECOMM.en
	env/en/SQLM.en
	env/en/SQLMESS.en
	env/precopt.hlf
	etc/VERSIONS
	incl/SQLCA.H
	incl/SQLDA.H
	incl/abaphndl.h
	incl/cpc.h
	incl/lcstream.h
	incl/livecachetypes.h
	incl/sqlca.h
	incl/sqlda.h
	incl/sqlinfo.h
	incl/xuserapi.h
	lib/libpcr
	lib/lib64/libpcr
	lib/libpcrl
	lib/lib64/libpcrl
	lib/libpcrs
	lib/lib64/libpcrs
	lib/libsqlca
	lib/lib64/libsqlca
	lib/libsqlrte
	lib/lib64/libsqlrte
	pgm/pc3
	pgm/sqlwhat
);

main (@ARGV);

sub main {
	my @list_complete = make_list_complete ();
	write_file ("CAR.LISTE.CPL", @list_complete);
	
	my @list_srv = compl (\@list_complete, \@files_ind);
	write_file ("CAR.LISTE.SRV", @list_srv);
	
	my @list_ind = avr (\@list_complete, \@files_ind);
	write_file ("CAR.LISTE.IND", @list_ind);

	my @list_pre = avr (\@list_complete, \@files_pre);
	write_file ("CAR.LISTE.PRE", @list_pre);

	my @list_mng = avr (\@list_complete, \@files_mng);
	write_file ("CAR.LISTE.MNG", @list_mng);

	return 0;
	
}

sub compl {
	my $ref1 = shift;
	my $ref2 = shift;
	my @list0 = ();
	my @list1 = @$ref1;
	my @list2 = @$ref2;

	my $outer;
	my $inner;

OUTER:	foreach  $outer (@list1) {
		foreach $inner (@list2) {
			next OUTER if ($outer =~ /^$inner/);		
		}
		push @list0, $outer;
	};

	return (sort (@list0));	
}

sub avr {
	my $ref1 = shift;
	my $ref2 = shift;
	my @list0 = ();
	my @list1 = @$ref1;
	my @list2 = @$ref2;

	my $outer;
	my $inner;

	foreach  $outer (@list1) {
		foreach $inner (@list2) {
			if ($inner =~ /\/$/) {
				push @list0, $outer
					if ($outer =~ /^$inner/);
			} else {
				push @list0, $outer
					if ($outer =~ /^$inner(\..*)?$/);		
			}
		}
	};

	return (sort (@list0));	
}

sub make_list_complete {
	my $fh = gensym ();
	my @list = ();
	
	my $cmd = "find . -type f -print";

	chdir $ENV{"DBROOT"};
	open ($fh, $cmd." |");
	while (<$fh>) {
		chomp $_;
		push @list, $_;
	}
	close ($fh);
	
	@list = remove_dots (@list);
	@list = remove_files (@list);

	return (sort (@list));
}

sub write_file {
	my $filename = $ENV{"DBROOT"}."/etc/".shift;
	my $fh = gensym ();

	die "cannot open ".$filename."\n"
		unless (open ($fh, "> ".$filename));

	foreach $_ (@_) {
		print $fh $_."\n";
	}
	close ($fh);

	return;
}

sub remove_dots {
	my @list = ();
	foreach $_ (@_) {
		if ($_ =~ /^\.\/(.+)$/g) {
			push @list, $1;
		} else {
			push @list, $_;
		}
	}
	return @list;
}

sub remove_files {
	my @list = ();
	foreach $_ (@_) {
		next unless (
			($_ =~ /^etc\//) ||
			($_ =~ /^bin\//) ||
			($_ =~ /^demo\//) || 
			($_ =~ /^env\//) || 
			($_ =~ /^etc\//) || 
			($_ =~ /^incl\//) || 
			($_ =~ /^lib\//) || 
			($_ =~ /^misc\//) || 
			($_ =~ /^pgm\//) || 
			($_ =~ /^sap\//) || 
			($_ =~ /^terminfo\//) ||
			($_ =~ /^INSTALL/) ||
			! ($_ =~ /\//)
		);

		next if ($_ =~ /\.s$/);
		next if ($_ =~ /\.f$/);
		next if ($_ =~ /\.q$/);
		next if ($_ =~ /\.SAR$/);
		next if ($_ =~ /\.SAR\.\d\d$/);
		next if ($_ =~ /\.CAR$/);
		next if ($_ =~ /\.CAR\.\d\d$/);
		next if ($_ =~ /^pgm\/slowci/);
		next if ($_ =~ /^pgm\/db:/);
		next if ($_ =~ /core$/);
		next if ($_ =~ /\.prot$/);
		next if ($_ =~ /\.prt$/);
		next if ($_ =~ /\.pyc$/);
		next if ($_ =~ /^env\/_/);
		next if ($_ eq "bin/sqllist.rte");
		next if ($_ eq "pgm/g_fl_lpno");
		next if ($_ =~ /\.log$/);
	
		if ($_ =~ /^etc\/.+/) {
			next unless (
				($_ eq "etc/VERSIONS") ||
				($_ eq "etc/distlist")
			);
		}
	
		if ($_ =~ /^terminfo\/.+/) {
			next unless (
				($_ =~ /\.ti$/) ||
				($_ =~ /\.hif$/) ||
				($_ =~ /^terminfo\/term\//) ||
				($_ =~ /^terminfo\/chrclass\//)
			);
		}
	
		push @list, $_;
	}
	return @list;
}
