#! /usr/bin/perl
use warnings;
use strict;
use integer;
use FindBin;
use lib $FindBin::RealBin;
use Def;

# This script adds or updates the Debian package version number at the
# head of debian/changelog.

our $usage = <<END;
usage: $0 [-h] [debian-version]
    -h print this usage message
If no debian-version is given, then the package version number
is removed from the changelog.
END

our $changelog_deb = "${FindBin::RealBin}/../../debian/changelog";

# Read command-line arguments and options.
my @arg;
my %opt;
for ( @ARGV ) {
  if ( my($o) = /^-(\S+)/ ) {
    $opt{$_} = 1 for split //, $o;
  }
  else { push @arg, $_ }
}
if ( $opt{h} || $opt{'?'} ) {
  print $usage;
  exit 0;
}
my( $debver, $vdstr );
if    ( @arg == 0 ) {
  die $usage;
  $debver = '';
  $vdstr  = '';
}
elsif ( @arg == 1 ) {
  $debver = shift @arg;
  $vdstr  = "-$debver";
}
else { die $usage }

# Read the changelog in.
my @cld;
open CL, '<', $changelog_deb;
  @cld = <CL>;
close CL;

# Update the changelog's first line.
$cld[0] =~
  s/^(${Def::out} \(${Def::basever}\.\d+)((?:-\d+)?)(\))/$1$vdstr$3/
  or die "$0: can't parse the first line of debian/changelog\n";

# Write the changelog out.
open CL, '>', $changelog_deb;
  print CL @cld;
close CL;

