5 # Used by make to replace tags delimited by pairs of '%%'. Each tag should be
6 # listed below. Remember, the script reads from the environment for $pkg_type
8 # ARGV[0] = Source file (ends in .src)
9 # ARGV[1] = Destination file (usually ARGV[0] - ".src"
12 # We must run from the root of a source tree, but we can only check that the
13 # common files are in the right places
14 if ( not -e "packaging/common/substitute.pl" ) {
15 die "Error: 'substitute.pl' must be run from the root of a source tree"
19 # Two build cases: from checkout (svn info works), or dist tarball
20 # (FULL_VERSION will exist). First try FULL_VERSION, then
21 # try svn info, which takes more time and processing. We assume our url
22 # is structured something like this:
23 # http://<server>/<project>/<trunk|branches|tags>/[<branch>|<tag>]
24 # The re is tested on http urls with full DNS names, but ssh+svn:// or
25 # file:/// and short DNS names should work too.
28 my $version_file = "FULL_VERSION";
30 my $versioned_tree = 'packaging/deb';
31 if (-e $version_file) {
32 # Autogen has been run, search FULL_VERSION file.
33 if (-e "$version_file") {
34 open($version_handle, "<", "$version_file") or
35 die "Could not open FULL_VERSION.";
36 chomp($VERSION = <$version_handle>);
40 die "Could not find FULL_VERSION file.";
44 # Autogen has not been run or VERSION macro was found. Try to
49 foreach my $info_line (`svn info $versioned_tree`) {
50 $SVN_URL = $1 if $info_line =~ m{^URL: (.*)};
51 $SVN_ROOT = $1 if $info_line =~ m{^Repository Root: (.*)};
52 $SVN_REV = $1 if $info_line =~ m{^Revision: (.*)};
58 # Only newer versions of svn supply Repository Root.
60 $SVN_URL =~ m/$SVN_ROOT(.*)/;
63 @paths = split "/", $SVN_PATH;
64 # We get ( empty, project branch, svn_version...)
67 $svn_version = $paths[3];
69 # This may not work with file or ssh+svn urls. In an
70 # http: url, we get ( Protocol, empty, server, project,
71 # branch, svn_version...)
72 @paths = split "/", $SVN_URL;
75 $svn_version = $paths[5];
78 if ( $BRANCH eq "trunk" | $BRANCH eq "branches" ) {
79 # Suffix -svn-rev to branch and trunk builds.
81 # Fix VERSION by stripping up to the first digit
82 $svn_version =~ s/^\D*//;
84 if ( $BRANCH eq "branches" ) {
85 # Branch names *should* have only 2 digits.
86 $svn_version =~ m{^(\d)$cruft?(\d)$cruft?(\w*)?};
87 # We throw away anything other than the first
90 # Make sure that the version indicates this is
91 # neither an RC or patch build.
94 # We should have a tag, which *should* have 3
95 # and maybe an rc## suffix
96 $svn_version =~ m{^(\d)$cruft?(\d)$cruft?(\d)$cruft?(\w*)?};
97 $VERSION = "$1.$2.$3$4";
105 # Check environment to see if it's something else.
106 if (defined($ENV{'pkg_type'})) {
107 $pkg_type = $ENV{"pkg_type"};
109 # Check the file name for a clue
110 elsif ( $ARGV[0] =~ /deb/ ) {
113 elsif ( $ARGV[0] =~ /rpm/ ) {
116 elsif ( $ARGV[0] =~ /sun/ ) {
121 # The surrounding line is preserved, and only the tag is replaced. This
122 # behavior is somewhat arbitrary, but hopefully keeps replacements in comments
124 my %replacement_strings_common = (
125 "%%VERSION%%" => get_version(),
126 "%%AMANDAHOMEDIR%%" => "/var/lib/amanda",
127 "%%LOGDIR%%" => "/var/log/amanda",
130 my %replacement_strings_deb = (
131 # Used in debian changelog
140 my %replacement_strings_rpm = (
143 my %replacement_strings_sun = (
146 my %replacement_strings;
147 if ( $pkg_type eq "deb" ) {
148 %replacement_strings = ( %replacement_strings_deb,
149 %replacement_strings_common );
150 # Let's determine the distro:
151 # Ubuntu has /etc/lsb-release, debian does not
152 open(my $LSB_RELEASE, "<", "/etc/lsb-release") or
153 $replacement_strings{"%%DISTRO%%"} = "Debian";
155 if ( $replacement_strings{"%%DISTRO%%"} ne "Debian" ) {
156 $replacement_strings{"%%DISTRO%%"} = "Ubuntu";
157 # We want the 2nd line
159 my @line = split /=/, <$LSB_RELEASE>;
162 $replacement_strings{"%%DEB_REL%%"} = $line[1];
165 open(my $DEB_RELEASE, "<", "/etc/debian_version") or die "could not read \"/etc/debian_version\": $!";
166 chomp($line = <$DEB_RELEASE>);
167 # Releases can have 3 fields. we want the first 2.
168 $line =~ s/(\d+)\.(\d+).*/$1$2/;
169 $replacement_strings{"%%DEB_REL%%"} = $line;
172 # Set the date using date -r
173 open(my $DATE_PIPE, "-|", "/bin/date -R") or die "could not read output of date -r";
174 chomp($line = <$DATE_PIPE>);
175 $replacement_strings{"%%DATE%%"} = $line;
177 # 32bit should use bitrock perl, while 64bit should use builtin. we
178 # live on the edge and assume it's there.
179 my @uname=POSIX::uname();
180 my $arch = $uname[4];
181 if ( $arch eq "x86_64" ) {
182 $replacement_strings{"%%PERL%%"} = $^X;
185 $replacement_strings{"%%PERL%%"} = "/opt/zmanda/amanda/perl/bin/perl";
188 elsif ( $pkg_type eq "rpm" ){
189 %replacement_strings = ( %replacement_strings_rpm,
190 %replacement_strings_common );
193 %replacement_strings = ( %replacement_strings_sun,
194 %replacement_strings_common );
197 open my $src, "<", $ARGV[0] or die "could not read $ARGV[0]: $!";
198 open my $dst, ">", $ARGV[1] or die "could not write $ARGV[1]: $!";
202 # check for tags, using non greedy matching
203 if ( m/(%%.+?%%)/ ) {
204 # strings just replace the tag.
205 if ( defined($replacement_strings{$1})) {
206 s/(%%.+?%%)/$replacement_strings{$1}/g;
211 # If we got here, print the line unmolested