die "Error: 'substitute.pl' must be run from the root of a source tree"
}
-sub get_version {
- # Two build cases: from checkout (svn info works), or dist tarball
- # (FULL_VERSION will exist). First try FULL_VERSION, then
- # try svn info, which takes more time and processing. We assume our url
- # is structured something like this:
- # http://<server>/<project>/<trunk|branches|tags>/[<branch>|<tag>]
- # The re is tested on http urls with full DNS names, but ssh+svn:// or
- # file:/// and short DNS names should work too.
-
- my $VERSION;
- my $version_file = "FULL_VERSION";
- my $version_handle;
- my $versioned_tree = 'packaging/deb';
- if (-e $version_file) {
- # Autogen has been run, search FULL_VERSION file.
- if (-e "$version_file") {
- open($version_handle, "<", "$version_file") or
- die "Could not open FULL_VERSION.";
- chomp($VERSION = <$version_handle>);
- close($version_file);
-
- } else {
- die "Could not find FULL_VERSION file.";
- }
- }
- if ( ! $VERSION ) {
- # Autogen has not been run or VERSION macro was found. Try to
- # use svn info.
- my $SVN_URL = "";
- my $SVN_ROOT = "";
- my $SVN_REV = "";
- foreach my $info_line (`svn info $versioned_tree`) {
- $SVN_URL = $1 if $info_line =~ m{^URL: (.*)};
- $SVN_ROOT = $1 if $info_line =~ m{^Repository Root: (.*)};
- $SVN_REV = $1 if $info_line =~ m{^Revision: (.*)};
- }
- my @paths;
- my $BRANCH;
- my $PROJECT;
- my $svn_version;
- # Only newer versions of svn supply Repository Root.
- if ( $SVN_ROOT ) {
- $SVN_URL =~ m/$SVN_ROOT(.*)/;
- my $SVN_PATH = $1;
+sub get_date {
+ my $date;
+ # Set the date using date -r
+ open(my $DATE_PIPE, "-|", "/bin/date '+%a, %d %b %Y %T %z'");
+ [ $? == 0 ] or die "could not read output of date '+%a, %d %b %Y %T %z'";
+ chomp($date = <$DATE_PIPE>);
+ close($DATE_PIPE);
+ return $date;
+};
- @paths = split "/", $SVN_PATH;
- # We get ( empty, project branch, svn_version...)
- $PROJECT = $paths[1];
- $BRANCH = $paths[2];
- $svn_version = $paths[3];
- } else {
- # This may not work with file or ssh+svn urls. In an
- # http: url, we get ( Protocol, empty, server, project,
- # branch, svn_version...)
- @paths = split "/", $SVN_URL;
- $PROJECT = $paths[3];
- $BRANCH = $paths[4];
- $svn_version = $paths[5];
- }
+sub get_arch {
+ my @u = POSIX::uname();
+ return $u[4];
+};
- if ( $BRANCH eq "trunk" | $BRANCH eq "branches" ) {
- # Suffix -svn-rev to branch and trunk builds.
- } else {
- # Fix VERSION by stripping up to the first digit
- $svn_version =~ s/^\D*//;
- my $cruft = qr{[_.]};
- if ( $BRANCH eq "branches" ) {
- # Branch names *should* have only 2 digits.
- $svn_version =~ m{^(\d)$cruft?(\d)$cruft?(\w*)?};
- # We throw away anything other than the first
- # two digits.
- $VERSION = "$1.$2";
- # Make sure that the version indicates this is
- # neither an RC or patch build.
- $VERSION .= "branch";
- } else {
- # We should have a tag, which *should* have 3
- # and maybe an rc## suffix
- $svn_version =~ m{^(\d)$cruft?(\d)$cruft?(\d)$cruft?(\w*)?};
- $VERSION = "$1.$2.$3$4";
- }
- }
+sub read_file {
+ # $1 is the file name and must exist.
+ my $contents;
+ my $file = "$_[0]";
+ my $f_handle;
+ # Autogen has been run, the file will be there.
+ if (-e $file) {
+ open($f_handle, "<", "$file") or
+ die "Could not open $file.";
+ chomp($contents = <$f_handle>);
+ close($f_handle);
+
+ } else {
+ die "Could not find $file file. run config/set_full_version or ./autogen";
}
- return $VERSION;
+ return $contents;
}
+sub fix_pkg_rev {
+ my $pkg_rev = "$_[0]";
+ # $1 should be a package type, and we build the rest of the regex string
+ # here for simplicity
+ my $type_match_str = "$_[1]0?";
+ # strip pkg_type and maybe a zero, else assign pkg_rev = 1
+ $pkg_rev = $pkg_rev =~ s/$type_match_str// || 1;
+ return $pkg_rev;
+ }
+
my $pkg_type;
# Check environment to see if it's something else.
if (defined($ENV{'pkg_type'})) {
elsif ( $ARGV[0] =~ /sun/ ) {
$pkg_type = "sun";
}
+else {
+ die "Could not determine pkg_type either by environment variable, or
+ pathname of files to substitute ($ARGV[0]).";
+}
+# The keys to the hashes used are the "tags" we try to substitute. Each
+# tag should be on a line by itself in the package file, as the whole line is
+# replaced by a set of lines. The line may be commented.
+my %replacement_filenames = (
+ "%%COMMON_FUNCTIONS%%" => "packaging/common/common_functions.sh",
+ "%%PRE_INST_FUNCTIONS%%" => "packaging/common/pre_inst_functions.sh",
+ "%%POST_INST_FUNCTIONS%%" => "packaging/common/post_inst_functions.sh",
+ "%%POST_RM_FUNCTIONS%%" => "packaging/common/post_rm_functions.sh",
+# TODO: PRE_UNINST?
+);
-# The surrounding line is preserved, and only the tag is replaced. This
-# behavior is somewhat arbitrary, but hopefully keeps replacements in comments
-# syntax legal.
+# These are handled slightly differently: The surrounding line is preserved,
+# and only the tag is replaced. This behavior is somewhat arbitrary, but
+# hopefully keeps replacements in comments syntax legal.
my %replacement_strings_common = (
- "%%VERSION%%" => get_version(),
+ "%%VERSION%%" => read_file("FULL_VERSION"),
+ "%%PKG_REV%%" => read_file("PKG_REV"),
"%%AMANDAHOMEDIR%%" => "/var/lib/amanda",
"%%LOGDIR%%" => "/var/log/amanda",
);
);
my %replacement_strings_sun = (
+ "%%ARCH%%" => "",
);
my %replacement_strings;
if ( $pkg_type eq "deb" ) {
%replacement_strings = ( %replacement_strings_deb,
%replacement_strings_common );
+ $replacement_strings{"%%PKG_REV%%"} =
+ fix_pkg_rev($replacement_strings{"%%PKG_REV%%"}, "deb");
# Let's determine the distro:
# Ubuntu has /etc/lsb-release, debian does not
open(my $LSB_RELEASE, "<", "/etc/lsb-release") or
$replacement_strings{"%%DEB_REL%%"} = $line;
close($DEB_RELEASE);
}
- # Set the date using date -r
- open(my $DATE_PIPE, "-|", "/bin/date -R") or die "could not read output of date -r";
- chomp($line = <$DATE_PIPE>);
- $replacement_strings{"%%DATE%%"} = $line;
- close($DATE_PIPE);
+ $replacement_strings{"%%DATE%%"} = get_date();
# 32bit should use bitrock perl, while 64bit should use builtin. we
# live on the edge and assume it's there.
- my @uname=POSIX::uname();
- my $arch = $uname[4];
+ my $arch = get_arch();
if ( $arch eq "x86_64" ) {
$replacement_strings{"%%PERL%%"} = $^X;
}
elsif ( $pkg_type eq "rpm" ){
%replacement_strings = ( %replacement_strings_rpm,
%replacement_strings_common );
+ $replacement_strings{"%%PKG_REV%%"} =
+ fix_pkg_rev($replacement_strings{"%%PKG_REV%%"}, "rpm");
}
else {
%replacement_strings = ( %replacement_strings_sun,
%replacement_strings_common );
+ $replacement_strings{"%%PKG_REV%%"} =
+ fix_pkg_rev($replacement_strings{"%%PKG_REV%%"}, "sun");
+ $replacement_strings{"%%DATE%%"} = get_date();
+ my $arch = get_arch();
+ if ( $arch eq "sun4u" ) {
+ $replacement_strings{"%%ARCH%%"} = "sparc";
+ }
+ elsif ( $arch eq "i86pc" ) {
+ $replacement_strings{"%%ARCH%%"} = "intel";
+ }
+ else {
+ die "Unknown solaris platform!";
+ }
}
+# Make a hash of tags and the contents of replacement files
+my %replacement_data;
+while (my ($tag, $filename) = each %replacement_filenames) {
+ open(my $file, "<", $filename) or die "could not read \"$filename\": $!";
+ $replacement_data{$tag} = join "", <$file>;
+ close($file);
+}
open my $src, "<", $ARGV[0] or die "could not read $ARGV[0]: $!";
open my $dst, ">", $ARGV[1] or die "could not write $ARGV[1]: $!";
select $dst;
chomp;
# check for tags, using non greedy matching
if ( m/(%%.+?%%)/ ) {
+ # Data replaces the line
+ if ( defined($replacement_data{$1})) {
+ print $replacement_data{$1};
+ }
# strings just replace the tag.
- if ( defined($replacement_strings{$1})) {
+ elsif ( defined($replacement_strings{$1})) {
s/(%%.+?%%)/$replacement_strings{$1}/g;
print "$_\n";
}