709N/A#!/usr/bin/perl -w
709N/A#
709N/A# uprev-spec - increments by 1 the value of the Release field in spec files
709N/A#
709N/A# Dermot McCluskey 22-July-2003 Initial Version
709N/A
709N/A
709N/Ause strict;
709N/Ause warnings;
709N/Ause Getopt::Long qw(:config gnu_getopt no_auto_abbrev);
709N/A
709N/Amy $exit_val = 0;
709N/Amy $verbose = 1;
709N/Amy $build = "";
709N/Amy @spec_files = ();
709N/A
709N/A
709N/A&main ();
709N/Aexit $exit_val;
709N/A
709N/A
709N/A
709N/Asub print_msg ($)
709N/A{
709N/A ($verbose) && print shift;
709N/A}
709N/A
709N/A
709N/Asub set_quiet
709N/A{
709N/A $verbose = 0;
709N/A}
709N/A
709N/A
709N/Asub process_args ($)
709N/A{
709N/A push @spec_files, shift;
709N/A}
709N/A
709N/A
709N/Asub process_options
709N/A{
709N/A Getopt::Long::Configure ("bundling");
709N/A
709N/A GetOptions ('q|quiet' => \&set_quiet,
709N/A 'h|help' => \&usage,
709N/A 'build=n' => \$build,
709N/A '<>' => \&process_args);
709N/A}
709N/A
709N/A
709N/Asub usage (;$)
709N/A{
709N/A print << "EOF";
709N/A
709N/Auprev-spec - increments by 1 the value of the Release field in spec files
709N/A
709N/AUsage: uprev-spec [options] specs...
709N/A
709N/Aoptions:
709N/A -q|--quiet: Silent operation.
709N/A -h|--help: Print this help message.
709N/A --build=n: If specified, Release will be incremented to at least n
709N/A
709N/Aspecs...
709N/A path(s) to spec file(s)
709N/AEOF
709N/A
709N/A exit 0;
709N/A}
709N/A
709N/A
709N/Asub up_rev ($)
709N/A{
709N/A my $spec_file = shift;
709N/A my $spec_file_base = $spec_file;
709N/A my $temp_file;
709N/A my $found = 0;
709N/A my $out;
709N/A
709N/A # if spec_file contains a leading dir, then split it out
709N/A if ($spec_file =~ /.+\/(.+)/) {
709N/A $spec_file_base = $1;
709N/A }
709N/A
709N/A $temp_file = "/tmp/$spec_file_base.tmp";
709N/A
709N/A &print_msg (sprintf ("%-31s ", $spec_file_base));
709N/A
709N/A if (! -e $spec_file) {
709N/A &print_msg ("ERROR: no such spec file: $spec_file\n");
709N/A return 0;
709N/A }
709N/A
709N/A if (! -w $spec_file) {
709N/A &print_msg ("ERROR: unable to write to spec file: $spec_file\n");
709N/A return 0;
709N/A }
709N/A
709N/A if (-e $temp_file) {
709N/A if (unlink ($temp_file) != 1) {
709N/A &print_msg ("ERROR: Cannot delete old temp file $temp_file: $!\n");
709N/A return 0;
709N/A }
709N/A }
709N/A
709N/A if (! open (SPECFILE, $spec_file)) {
709N/A &print_msg ("ERROR: Cannot read $spec_file: $!\n");
709N/A return 0;
709N/A }
709N/A
709N/A if (! open (TEMPFILE, ">$temp_file")) {
709N/A &print_msg ("ERROR: Cannot create $temp_file: $!\n");
709N/A return 0;
709N/A }
709N/A
709N/A while (<SPECFILE>) {
709N/A /^(release\s*:\s*)(\S*)(\s*)$/i && do {
709N/A my $label = $1;
709N/A my $rel = $2;
709N/A my $tail = $3;
709N/A my $lead = "";
709N/A my $newrel;
709N/A
709N/A $found = 1;
709N/A
709N/A # if Release value does not consist entirely of digits,
709N/A # then check for leading chars ending with digits
709N/A if ($rel !~ /^\d+$/) {
709N/A if ($rel =~ /^(.*\D+)(\d+)$/) {
709N/A $lead = $1;
709N/A $rel = $2;
709N/A } else {
709N/A &print_msg ("ERROR: non-numeric Release: $rel\n");
709N/A close (SPECFILE);
709N/A close (TEMPFILE);
709N/A return 0;
709N/A }
709N/A }
709N/A
709N/A &print_msg (sprintf ("%7s ", "$lead$rel"));
709N/A $newrel = $rel + 1;
709N/A
709N/A if (length ($build) && ($newrel < $build)) {
709N/A $newrel = $build;
709N/A }
709N/A
709N/A &print_msg (sprintf ("%7s ", "$lead$newrel"));
709N/A print TEMPFILE "$label$lead$newrel$tail";
709N/A next;
709N/A };
709N/A
709N/A # print all other lines unmodified
709N/A print TEMPFILE $_;
709N/A }
709N/A
709N/A close (TEMPFILE);
709N/A close (SPECFILE);
709N/A
709N/A if (! $found) {
709N/A &print_msg ("ERROR: no Release value in spec file\n");
709N/A return 0;
709N/A }
709N/A
709N/A $out = `cp $temp_file $spec_file`;
709N/A
709N/A if ($?) {
709N/A &print_msg ("ERROR: \"cp $temp_file $spec_file\": $out");
709N/A return 0;
709N/A }
709N/A
709N/A unlink ($temp_file);
709N/A &print_msg ("OK\n");
709N/A
709N/A return 1;
709N/A}
709N/A
709N/A
709N/Asub main
709N/A{
709N/A &process_options ();
709N/A
709N/A if (! @spec_files) {
709N/A &print_msg ("Nothing to do.\n");
709N/A return;
709N/A }
709N/A
709N/A &print_msg ("Spec file Release New Rel Status\n");
709N/A &print_msg ("=============================== ======= ======= =======\n");
709N/A
709N/A foreach (@spec_files) {
709N/A &up_rev ($_) || $exit_val++;
709N/A }
709N/A}