#!/usr/bin/perl

# usage:  MakeRPM [release]

# if release is not given, defaults to "1"
# automatically default to suffixing with "linux distribution" if set
# in the rpmbuild defaults

my $release = shift;
$release = 1 unless defined $release;


# first make sure that we have the current version number

use YAML::XS 'LoadFile';

system("perl Build.PL >/dev/null 2>/dev/null") if ! -e "Build";
$msg = `./Build distmeta 2>/dev/null`;
if ($msg =~ /has been altered/i) {
    system("perl Build.PL >/dev/null 2>/dev/null");
    system('./Build distmeta >/dev/null 2>/dev/null');
}

my $yaml = LoadFile('META.yml');
my $version = $yaml->{provides}{'Lab::Measurement'}{version};


# put the current version in the .spec file

my $specfile = 'perl-Lab-Measurement.spec';
rename($specfile,"temp.spec");
open(IN,"<temp.spec") || die 'unable to open temp.spec';
open(OUT,">$specfile") || die "unable to open $specfile";

my $gotv = 0;
my $gotr = 0;
while (<IN>) {
    if (!$gotv && /^version:\s/i) {
	$gotv = 1;
	print OUT "Version:\t$version\n";
    } elsif (!$gotr && /^release:\s/i) {
	$gotr = 1;
	print OUT "Release:\t$release".'%{?dist}'."\n";
    } else {
	print OUT $_;
    }
}
close(IN);
close(OUT);
unlink("temp.spec");

# create the distribution tar file

system("./Build manifest");
system("./Build dist");

# create the rpms (source and installation)

system("rpmbuild -ta Lab-Measurement-${version}.tar.gz");
