#! /usr/bin/perl

# This script repackages an ImageMagick RPM reconfigured to use lcms

# Copyright  2005 Brendt Wohlberg <software@wohlberg.net>
# Please see the ImageInfo distribution README file for license information

# Most recent modification: 29 March 2005

use strict;
use File::Path;
use File::Copy;
use Getopt::Std;

my $usagetext = <<EOF;
usage: magickrpmwlcms [-r release-postfix] [-b] [-o output-dir] rpm-src-file
       -r Specify a new RPM release label
       -b Build binary RPMs after reconstructing the source RPM
       -o Write reconstructed RPM(s) into specified directory
EOF

my $opts = {};
getopts('hr:bo:', $opts);

if ($opts->{'h'}) {print $usagetext; exit(0)};
if (@ARGV == 0) {print $usagetext; exit(1)};

my $rpmfile = $ARGV[0];

my $tmpdir = '/tmp/magickrpmwlcms';
mkpath($tmpdir);
my $subdirs = ['src', 'tmp', 'build', 'install', 'rpm', 'srpm'];
my $tmpsubdirs;
@$tmpsubdirs = map { "$tmpdir/$_" } @$subdirs;
mkpath($tmpsubdirs);

# Note: RPM macro configuration details were obtained from the example file
#       made available by Mike A. Harris <mharris@redhat.com>
my $rpmrc = <<EOF;
include:    /usr/lib/rpm/rpmrc
macrofiles: /usr/lib/rpm/macros:/usr/lib/rpm/%{_target}/macros:/etc/rpm/macros.specspo:/etc/rpm/macros:/etc/rpm/%{_target}/macros:$tmpdir/rpmmacros
EOF
my $rpmmacros = <<EOF;
%_topdir        $tmpdir
%_sourcedir     %{_topdir}/src
%_specdir       %{_sourcedir}
%_tmppath	%{_topdir}/tmp
%_builddir	%{_topdir}/build
%_buildroot	%{_topdir}/install
%_rpmdir	%{_topdir}/rpm
%_srcrpmdir	%{_topdir}/srpm
%_rpmfilename	%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
EOF
open(OF, "> $tmpdir/rpmrc") or die "File open failed\n";
print OF $rpmrc;
close(OF);
open(OF, "> $tmpdir/rpmmacros") or die "File open failed\n";
print OF $rpmmacros;
close(OF);

my $cmd = "rpm --rcfile $tmpdir/rpmrc -U $rpmfile";
system($cmd);

my $spec = glob("$tmpdir/src/*.spec");
my $tmpspec = "${spec}.tmp";
my $line;
open(IF, "< $spec") or die "File open failed\n";
open(OF, "> $tmpspec") or die "File open failed\n";
while ($line = <IF>) {
  if ($line =~ /^%configure/) {
    print OF $line;
    while ($line = <IF> and $line =~ /^\s+--.*\s+\\\s*$/) {
      print OF $line;
    }
    if ($line =~ /^(\s+)--/) {
      my $ws = $1;
      chomp($line);
      print OF "$line \\\n";
      print OF "$ws--with-lcms\n";
    }
  } elsif ($line =~ /^%package\s+devel/) {
    print OF $line;
    while ($line = <IF> and $line !~ /^Requires:/) {
      print OF $line;
    }
    print OF $line;
    while ($line = <IF> and $line =~ /^Requires:/) {
      print OF $line;
    }
    print OF "Requires: lcms\n$line";
  } elsif (defined($opts->{'r'}) and $line =~ /^Release:/) {
    $line =~ s/(Release:\s+)[^\s]+(\s*)/$1$opts->{'r'}$2/;
    print OF $line;
  } else {
    print OF $line;
  }
}
close(OF);
close(IF);
rename $tmpspec, $spec;

$cmd = "rpmbuild --rcfile $tmpdir/rpmrc -bs $spec >/dev/null";
system($cmd);

my $outdir = (defined $opts->{'o'})?"$opts->{'o'}":"ImageMagickRPM";
mkpath($outdir);
my $srpm = glob("$tmpdir/srpm/*.src.rpm");
move($srpm, $outdir);

if ($opts->{'b'}) {
  $cmd = "rpmbuild --rcfile $tmpdir/rpmrc -bb $spec >/dev/null";
  system($cmd);
  system("mv $tmpdir/rpm/*.rpm $outdir");
}

rmtree($tmpdir);

exit(0);
