summaryrefslogtreecommitdiff
path: root/source4/script/installheader.pl
blob: e0597ab3812cb3f4ad1d05c4cdd3fe6f463f575c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl
# Copyright (C) 2006 Jelmer Vernooij
use strict;
use File::Basename;
use Cwd 'abs_path';

my $includedir = shift;
my $builddir = abs_path($ENV{samba_builddir});
my $srcdir = abs_path($ENV{samba_srcdir});

sub read_headermap($)
{
	my ($fn) = @_;
	my %map = ();
	my $ln = 0;
	open(MAP, "<$fn");
	while(<MAP>) {
		$ln++;
		s/#.*$//g;
		next if (/^\s*$/);
		if (! /^(.*): (.*)$/) {
			print STDERR "headermap.txt:$ln: Malformed line\n";
			next;
		}
		$map{$1} = $2;
	}

	close(MAP);

	return %map;
}

my %map = read_headermap("$srcdir/headermap.txt");

sub findmap($)
{
	$_ = shift;
	s/^\.\///g;
	s/$builddir\///g;
	s/$srcdir\///g;

	if (! -f $_ && -f "lib/$_") { $_ = "lib/$_"; }
	if ($srcdir !~ $builddir) {
	 if (! -f "$srcdir/$_" && -f "$srcdir/lib/$_") { $_ = "lib/$_"; }
	}
	
	return $map{$_};
}

sub rewrite_include($$)
{
	my ($pos,$d) = @_;

	my $n = findmap($d);
	return $n if $n; 
	return $d;
}

sub install_header($$)
{
	my ($src,$dst) = @_;

	my $lineno = 0;

	open(IN, "<$src") || open(IN, "<$srcdir/$src");
	open(OUT, ">$dst");

	while (<IN>) {
		$lineno++;
		if (/^#include \"(.*)\"/) {
			print OUT "#include <" . rewrite_include("$src:$lineno", $1) . ">\n";
		} else {
			print OUT $_;
		}
	}

	close(OUT);
	close(IN);
}

foreach my $p (@ARGV)
{
	my $p2 = findmap($p);
	unless ($p2) {
	    warn("Unable to map $p");
	    next;
	}
 	print "Installing $p as $includedir/$p2\n";

	my $dirname = dirname($p2);

	if (! -d "$includedir/$dirname") {
		mkdir("$includedir/$dirname");
	}

	if ( -f "$includedir/$p2" ) {
		unlink("$includedir/$p2.old");
		rename("$includedir/$p2", "$includedir/$p2.old");
	}

	install_header($p,"$includedir/$p2");
}

print <<EOF;
======================================================================
The headers are installed. You may restore the old headers (if there
were any) using the command "make revert". You may uninstall the headers
using the command "make uninstallheader" or "make uninstall" to uninstall
binaries, man pages and shell scripts.
======================================================================
EOF

exit 0;