1#!/usr/bin/perl
2
3use strict;
4use warnings;
5no warnings 'uninitialized';
6
7die "You must be root to build the RPM\n" if ($>);
8
9my $specfile   = 'Shell-EnvImporter.spec';
10my $sourcefile = &get_sourcefile_name($specfile);
11
12print "SOURCEFILE: $sourcefile\n";
13
14&build_rpm($sourcefile, $specfile);
15
16system("rpmbuild -ba $specfile");
17
18unlink($sourcefile);
19
20
21##############################################################################
22###############################  Subroutines  ################################
23##############################################################################
24
25###############
26sub build_rpm {
27###############
28  my $sourcefile  = shift;
29  my $specfile    = shift;
30  my $excludefile = "/var/tmp/build_rpm_exclude.$$";
31
32  # Build a source tarball, excluding CVS directories
33  system("find . -name '.svn' > $excludefile");
34  open(EX, ">>$excludefile");
35  print EX "$specfile\n";
36  print EX "build_rpm\n";
37  close(EX);
38
39  my $rv     = system("tar -czf $sourcefile --exclude-from $excludefile .");
40  my $errmsg = $!;
41
42  unlink($excludefile);
43
44  die "Couldn't create $sourcefile: $errmsg" if ($rv);
45
46}
47
48
49
50#########################
51sub get_sourcefile_name {
52#########################
53  my $specfile = shift;
54
55  my $tmpdir   = $ENV{'TMP'} || '/var/tmp';
56  my $tmpfile  = "$tmpdir/build_rpm.$$";
57  my $tag      = 'SOURCEFILE';
58  my $sourcefile;
59
60  open(SPEC, $specfile) or die "Couldn't open $specfile: $!";
61  open(TMP, ">$tmpfile") or die "Couldn't create $tmpfile: $!";
62  while (<SPEC>) {
63    last if (/^%prep/);
64    print TMP $_;
65  }
66  close(SPEC);
67
68  print TMP "%build\n";
69  print TMP "echo ", join(":", $tag, '%{S:0}'), "\n";
70  print TMP "exit -1\n";
71  close(TMP);
72
73  open(RPMBUILD, "rpmbuild -bc $tmpfile --short-circuit 2>&1 |")
74    or die "Couldn't spawn rpmbuild: $!";
75
76  while (<RPMBUILD>) {
77    next if (/^Executing/);
78    next if (/^\+/);
79    if (/^$tag/) {
80      chomp;
81      $sourcefile = (split(/:/, $_, 2))[1];
82      last;
83    } else {
84      warn("Unexpected output from rpmbuild: $_");
85    }
86  }
87  close(RPMBUILD);
88
89  unlink($tmpfile);
90
91  return $sourcefile;
92}
93