#!/usr/bin/perl
#
# spec_configure - produce husky.spec from the husky.spec.in stub using
#                  the settings from huskymak.cfg and the version
#                  numbers from the sources.

use File::Basename;
use warnings;
use strict;

my $husky_ver_major = 1;
my $husky_ver_minor = 9;
my $husky_ver_patch = 0;

# The file with makefile settings
my $huskymak = "huskymak.cfg";
my $stub     = "husky.spec.in";
my $spec     = "husky.spec";


my (@lines, @macros);
my @settings = qw(PROGRAMS PERL USE_HPTZIP DYNLIBS DEBUG GNMSGID FIDOCONF_UTIL
                  HPT_UTIL HTML TXT DVI PDF PREFIX LIBDIR BINDIR
                  MAN1DIR MAN3DIR MAN5DIR INFODIR DOCDIR CC);
my @progs = qw(hpt htick hptkill hptsqfix hptutil sqpack msged fidoroute util areastat nltools);
my ($hpt, $htick, $hptkill, $hptutil, $hptsqfix, $sqpack, $msged, $fidoroute,
    $util, $areastat, $nltools, $tparser,
    $huskylib, $smapi, $fidoconf, $areafix, $hptzip,
    $PROGRAMS, $PERL, $USE_HPTZIP, $DYNLIBS, $DEBUG, $GNMSGID, $FIDOCONF_UTIL,
    $HPT_UTIL, $HTML, $TXT, $DVI, $PDF, $PREFIX, $LIBDIR, $BINDIR, $PERLDATADIR,
    $MAN1DIR, $MAN3DIR, $MAN5DIR, $INFODIR, $DOCDIR, $CC, $DOC, $texi2dvi,
    $texinfo,
    $huskylib_reldate, $smapi_reldate, $fidoconf_reldate, $areafix_reldate,
    $hptzip_reldate, $hpt_reldate, $htick_reldate, $hptkill_reldate,
    $hptsqfix_reldate, $hptutil_reldate, $sqpack_reldate, $msged_reldate,
    $fidoroute_reldate, $util_reldate, $areastat_reldate, $nltools_reldate,
    $hpt_doc_date, $htick_doc_date, $msged_doc_date, $fidoconf_doc_date,
    $fidoroute_doc_date, $doc_reldate);

# Initialize variables
$huskylib = $smapi = $fidoconf = $areafix = $hptzip = 0;
$PERL = $USE_HPTZIP = $DYNLIBS = $DEBUG = $GNMSGID = $FIDOCONF_UTIL = 0;
$HPT_UTIL = $HTML = $TXT = $DVI = $PDF = 0;
$PROGRAMS = $PREFIX = $PERLDATADIR = $MAN1DIR = $MAN3DIR = "";
$MAN5DIR = $INFODIR = $DOCDIR = $CC = "";
$huskylib_reldate = $smapi_reldate = $fidoconf_reldate = $areafix_reldate = 0;
$hptzip_reldate = $hpt_reldate = $htick_reldate = $hptkill_reldate = 0;
$hptsqfix_reldate = $hptutil_reldate = $sqpack_reldate = $msged_reldate = 0;
$fidoroute_reldate = $util_reldate = $areastat_reldate = $nltools_reldate = 0;
$hpt_doc_date = $htick_doc_date = $msged_doc_date = $fidoconf_doc_date = 0;
$fidoroute_doc_date = $doc_reldate = 0;
@macros = ("# husky.spec", "");

# Read a file to @lines
sub readfile
{
    my ($file) = @_;
    my @lines = ();
    open(FH, "<", $file) or die "Cannot open $file; $!";
    while(<FH>)
    {
        my $line = $_;
        $line =~ s/[\n\r]+//g;
        push(@lines, $line);
    }
    close(FH);
    return @lines;
}

# Fetch settings
sub fetch
{
    my ($file) = @_;

    my @lines = readfile($file);

    for my $line (@lines)
    {
        for my $var (@settings)
        {
            if($line =~ /^$var\s*=\s*(.*)$/)
            {
                # substitute values of the variables into the expression
                my $expr = eval("\"$1\"");
                my $val = '$'."$var=\'$expr\'";
                # assign expression to the variable in $var
                eval($val);
                last;
            }
        }
    }
}

sub get_version
{
    my ($file, @regexp) = @_;

    my @lines = readfile($file);
    my $num_versions = 0;
    my @ver = ();
    my $num_regexps = 0;
    for my $regex (@regexp)
    {
        $num_regexps++ if($regex ne "");
    }

    for my $line (@lines)
    {
        for (my $i = 0; $i <= $num_regexps - 1; $i++)
        {
            if($regexp[$i] ne "")
            {
                if($line =~ /$regexp[$i]/)
                {
                    $ver[$i] = $1;
                    $num_versions++;
                    last;
                }
            }
        }
        last if($num_versions == $num_regexps);
    }

    return @ver;
}

sub get_reldate
{
    my ($file, $regexp) = @_;

    my @lines = readfile($file);
    $lines[0] =~ $regexp;
    return "$1$2$3";
}

sub get_commit_date
{
    my ($subproject, $file) = @_;
    my $cd = qx(cd $subproject; git log -1 --date=short --format=format:"%cd%n" $file);
    $cd =~ /^(\d+)-(\d+)-(\d+)$/;
    return "$1$2$3";
}

sub max
{
    my ($first, $second) = @_;
    return $first > $second ? $first : $second;
}

###### Here the program starts ######

fetch($huskymak);

for my $var (@progs)
{
    my $value = ($PROGRAMS =~ /$var\s+/) || ($PROGRAMS =~ /$var$/) ? 1 : 0;
    eval('$'."$var = $value");
    push(@macros, "%global $var $value");
}

if(!(($PROGRAMS =~ /hpt\s+/) || ($PROGRAMS =~ /hpt$/)))
{
    $HPT_UTIL = 0;
}

for my $var (@settings)
{
    next if($var eq "PROGRAMS");
    my $value = eval('$'."$var");
    next if($value eq "");
    if($var eq "PREFIX")
    {
        push(@macros, "%global _prefix $value");
    }
    elsif($var eq "LIBDIR")
    {
        push(@macros, "%global _libdir $value");
    }
    elsif($var eq "BINDIR")
    {
        push(@macros, "%global _bindir $value");
    }
    elsif($var eq "CC")
    {
        push(@macros, "%global compiler $value");
    }
    else
    {
        push(@macros, "%global $var $value");
    }
}

$DOC = ($INFODIR ne "" || $DOCDIR ne "") ? 1 : 0;
push(@macros, "%global DOC $DOC");

$texinfo = $INFODIR ne "" ? 1 :
    $DOCDIR ne "" && ($HTML == 1 || $TXT == 1 || $PDF == 1 || $DVI == 1) ? 1 : 0;
push(@macros, "%global texinfo $texinfo");
$texi2dvi = ($PDF == 1 || $DVI == 1) ? 1 : 0;
push(@macros, "%global texi2dvi $texi2dvi");

my @regexp;

# huskylib
my $verfile  = "huskylib/huskylib/version.h";
if(-e $verfile)
{
    my $datefile = "huskylib/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $huskylib_reldate = get_reldate($datefile, $regex);

    $huskylib = ($PROGRAMS =~ /hpt\s+/) || ($PROGRAMS =~ /hpt$/) ||
                ($PROGRAMS =~ /htick/) || ($PROGRAMS =~ /hptkill/) ||
                ($PROGRAMS =~ /hptsqfix/) || ($PROGRAMS =~ /sqpack/) ||
                ($PROGRAMS =~ /msged/) || ($PROGRAMS =~ /areastat/) ||
                ($PROGRAMS =~ /nltools/) ? 1 : 0;
    if($huskylib == 1 || $GNMSGID == 1)
    {
        push(@macros, "%global huskylib_reldate $huskylib_reldate");
        @regexp = ('^#define\s+huskylib_VER_MAJOR\s+(\d+)',
                   '^#define\s+huskylib_VER_MINOR\s+(\d+)',
                   '^#define\s+huskylib_VER_PATCH\s+(\d+)');

        my ($huskylib_ver_major,
            $huskylib_ver_minor,
            $huskylib_ver_patch) = get_version($verfile, @regexp);

        push(@macros, "%global huskylib_ver_major $huskylib_ver_major");
        push(@macros, "%global huskylib_ver_minor $huskylib_ver_minor");
        push(@macros, "%global huskylib_ver_patch $huskylib_ver_patch");
    }
    $huskylib = ($huskylib == 1 && $DYNLIBS == 1) ? 1 : 0;
}
push(@macros, "%global huskylib $huskylib");


# smapi
$verfile = "smapi/smapi/version.h";
if(-e $verfile)
{
    my $datefile = "smapi/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $smapi_reldate = get_reldate($datefile, $regex);

    $smapi = ($PROGRAMS =~ /hpt\s+/) || ($PROGRAMS =~ /hpt$/) ||
             ($PROGRAMS =~ /htick/) || ($PROGRAMS =~ /hptkill/) ||
             ($PROGRAMS =~ /hptsqfix/) || ($PROGRAMS =~ /sqpack/) ||
             ($PROGRAMS =~ /msged/) || ($PROGRAMS =~ /areastat/) ||
             ($PROGRAMS =~ /nltools/) ? 1 : 0;
    if($smapi)
    {
        push(@macros, "%global smapi_reldate $smapi_reldate");
        @regexp = ('^#define\s+smapi_VER_MAJOR\s+(\d+)',
                   '^#define\s+smapi_VER_MINOR\s+(\d+)',
                   '^#define\s+smapi_VER_PATCH\s+(\d+)');

        my ($smapi_ver_major,
            $smapi_ver_minor,
            $smapi_ver_patch) = get_version($verfile, @regexp);

        push(@macros, "%global smapi_ver_major $smapi_ver_major");
        push(@macros, "%global smapi_ver_minor $smapi_ver_minor");
        push(@macros, "%global smapi_ver_patch $smapi_ver_patch");
    }
    $smapi = ($smapi == 1 && $DYNLIBS == 1) ? 1 : 0;
}
push(@macros, "%global smapi $smapi");


# fidoconf
$verfile = "fidoconf/fidoconf/version.h";
if(-e $verfile)
{
    my $datefile = "fidoconf/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $fidoconf_reldate = get_reldate($datefile, $regex);

    my $fidoconfig_texi_date = get_commit_date("fidoconf", "doc/fidoconfig.texi");
    my $proposal_texi_date = get_commit_date("fidoconf", "doc/proposal.texi");
    $fidoconf_doc_date = max($fidoconfig_texi_date, $proposal_texi_date);
    my $keywords_bp_date = get_commit_date("fidoconf", "doc/keywords.bp");
    $fidoconf_doc_date = max($fidoconf_doc_date, $keywords_bp_date);
    my $keywords_hpt_date = get_commit_date("fidoconf", "doc/keywords.hpt");
    $fidoconf_doc_date = max($fidoconf_doc_date, $keywords_hpt_date);
    my $keywords_htick_date = get_commit_date("fidoconf", "doc/keywords.htick");
    $fidoconf_doc_date = max($fidoconf_doc_date, $keywords_htick_date);

    $tparser = ($PROGRAMS =~ /hpt\s+/) || ($PROGRAMS =~ /hpt$/) ||
               ($PROGRAMS =~ /htick/) || ($PROGRAMS =~ /hptkill/) ||
               ($PROGRAMS =~ /sqpack/) || ($PROGRAMS =~ /msged/) ||
               ($PROGRAMS =~ /nltools/) ? 1 : 0;
    push(@macros, "%global tparser $tparser");

    $fidoconf = ($tparser == 1 && $DYNLIBS == 1) ? 1 : 0;
    if($tparser)
    {
        push(@macros, "%global fidoconf_reldate $fidoconf_reldate");

        @regexp = ('^#define\s+fidoconf_VER_MAJOR\s+(\d+)',
                   '^#define\s+fidoconf_VER_MINOR\s+(\d+)',
                   '^#define\s+fidoconf_VER_PATCH\s+(\d+)');

        my ($fidoconf_ver_major,
            $fidoconf_ver_minor,
            $fidoconf_ver_patch) = get_version($verfile, @regexp);

        push(@macros, "%global fidoconf_ver_major $fidoconf_ver_major");
        push(@macros, "%global fidoconf_ver_minor $fidoconf_ver_minor");
        push(@macros, "%global fidoconf_ver_patch $fidoconf_ver_patch");
    }
}
push(@macros, "%global fidoconf $fidoconf");


# areafix
$verfile = "areafix/areafix/version.h";
if(-e $verfile)
{
    my $datefile = "areafix/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $areafix_reldate = get_reldate($datefile, $regex);

    $areafix = (($PROGRAMS =~ /hpt\s+/) || ($PROGRAMS =~ /hpt$/) ||
                ($PROGRAMS =~ /htick/)) ? 1 : 0;
    if($areafix)
    {
        push(@macros, "%global areafix_reldate $areafix_reldate");
        @regexp = ('^#define\s+areafix_VER_MAJOR\s+(\d+)',
                   '^#define\s+areafix_VER_MINOR\s+(\d+)',
                   '^#define\s+areafix_VER_PATCH\s+(\d+)');

        my ($areafix_ver_major,
            $areafix_ver_minor,
            $areafix_ver_patch) = get_version($verfile, @regexp);

        push(@macros, "%global areafix_ver_major $areafix_ver_major");
        push(@macros, "%global areafix_ver_minor $areafix_ver_minor");
        push(@macros, "%global areafix_ver_patch $areafix_ver_patch");
    }
    $areafix = ($areafix == 1 && $DYNLIBS == 1) ? 1 : 0;
}
push(@macros, "%global areafix $areafix");


# hptzip
$verfile = "hptzip/hptzip/version.h";
if(-e $verfile)
{
    my $datefile = "hptzip/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $hptzip_reldate = get_reldate($datefile, $regex);

    $hptzip = ((($PROGRAMS =~ /hpt\s+/) || ($PROGRAMS =~ /hpt$/) ||
                ($PROGRAMS =~ /htick/) || ($PROGRAMS =~ /nltools/)) &&
               $USE_HPTZIP == 1) ? 1 : 0;
    if($hptzip)
    {
        push(@macros, "%global hptzip_reldate $hptzip_reldate");
        @regexp = ('^#define\s+hptzip_VER_MAJOR\s+(\d+)',
                   '^#define\s+hptzip_VER_MINOR\s+(\d+)',
                   '^#define\s+hptzip_VER_PATCH\s+(\d+)');

        my ($hptzip_ver_major,
            $hptzip_ver_minor,
            $hptzip_ver_patch) = get_version($verfile, @regexp);

        push(@macros, "%global hptzip_ver_major $hptzip_ver_major");
        push(@macros, "%global hptzip_ver_minor $hptzip_ver_minor");
        push(@macros, "%global hptzip_ver_patch $hptzip_ver_patch");
    }
    $hptzip = ($hptzip == 1 && $DYNLIBS == 1) ? 1 : 0;
}
push(@macros, "%global hptzip $hptzip");


# hpt
$verfile = "hpt/h/version.h";
if(-e $verfile && $hpt == 1)
{
    @regexp = ('^#define\s+hpt_VER_MAJOR\s+(\d+)',
               '^#define\s+hpt_VER_MINOR\s+(\d+)',
               '^#define\s+hpt_VER_PATCH\s+(\d+)');

    my ($hpt_ver_major,
        $hpt_ver_minor,
        $hpt_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global hpt_ver_major $hpt_ver_major");
    push(@macros, "%global hpt_ver_minor $hpt_ver_minor");
    push(@macros, "%global hpt_ver_patch $hpt_ver_patch");

    my $datefile = "hpt/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $hpt_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global hpt_reldate $hpt_reldate");

    if($DOCDIR ne "")
    {
        my $hpt_texi_date = get_commit_date("hpt", "doc/hpt.texi");
        my $texinfo_tex_date = get_commit_date("hpt", "doc/texinfo.tex");
        $hpt_doc_date = max($hpt_texi_date, $texinfo_tex_date);
    }
}


# htick
$verfile = "htick/h/version.h";
if(-e $verfile && $htick == 1)
{
    @regexp = ('^#define\s+htick_VER_MAJOR\s+(\d+)',
               '^#define\s+htick_VER_MINOR\s+(\d+)',
               '^#define\s+htick_VER_PATCH\s+(\d+)');

    my ($htick_ver_major,
        $htick_ver_minor,
        $htick_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global htick_ver_major $htick_ver_major");
    push(@macros, "%global htick_ver_minor $htick_ver_minor");
    push(@macros, "%global htick_ver_patch $htick_ver_patch");

    my $datefile = "htick/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $htick_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global htick_reldate $htick_reldate");

    if($DOCDIR ne "")
    {
        $htick_doc_date = get_commit_date("htick", "doc/htick.texi");
    }
}


# hptkill
$verfile = "hptkill/h/version.h";
if(-e $verfile && $hptkill == 1)
{
    @regexp = ('^#define\s+hptkill_VER_MAJOR\s+(\d+)',
               '^#define\s+hptkill_VER_MINOR\s+(\d+)',
               '^#define\s+hptkill_VER_PATCH\s+(\d+)');

    my ($hptkill_ver_major,
        $hptkill_ver_minor,
        $hptkill_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global hptkill_ver_major $hptkill_ver_major");
    push(@macros, "%global hptkill_ver_minor $hptkill_ver_minor");
    push(@macros, "%global hptkill_ver_patch $hptkill_ver_patch");

    my $datefile = "hptkill/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $hptkill_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global hptkill_reldate $hptkill_reldate");
}


# hptsqfix
$verfile = "hptsqfix/h/version.h";
if(-e $verfile && $hptsqfix == 1)
{
    @regexp = ('^#define\s+hptsqfix_VER_MAJOR\s+(\d+)',
               '^#define\s+hptsqfix_VER_MINOR\s+(\d+)',
               '^#define\s+hptsqfix_VER_PATCH\s+(\d+)');

    my ($hptsqfix_ver_major,
        $hptsqfix_ver_minor,
        $hptsqfix_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global hptsqfix_ver_major $hptsqfix_ver_major");
    push(@macros, "%global hptsqfix_ver_minor $hptsqfix_ver_minor");
    push(@macros, "%global hptsqfix_ver_patch $hptsqfix_ver_patch");

    my $datefile = "hptsqfix/h/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $hptsqfix_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global hptsqfix_reldate $hptsqfix_reldate");
}


# hptutil
$verfile = "hptutil/h/version.h";
if(-e $verfile && $hptutil == 1)
{
    @regexp = ('^#define\s+hptutil_VER_MAJOR\s+(\d+)',
               '^#define\s+hptutil_VER_MINOR\s+(\d+)',
               '^#define\s+hptutil_VER_PATCH\s+(\d+)');

    my ($hptutil_ver_major,
        $hptutil_ver_minor,
        $hptutil_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global hptutil_ver_major $hptutil_ver_major");
    push(@macros, "%global hptutil_ver_minor $hptutil_ver_minor");
    push(@macros, "%global hptutil_ver_patch $hptutil_ver_patch");

    my $datefile = "hptutil/h/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $hptutil_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global hptutil_reldate $hptutil_reldate");
}


# sqpack
$verfile = "sqpack/version.h";
if(-e $verfile && $sqpack)
{
    @regexp = ('^#define\s+sqpack_VER_MAJOR\s+(\d+)',
               '^#define\s+sqpack_VER_MINOR\s+(\d+)',
               '^#define\s+sqpack_VER_PATCH\s+(\d+)');

    my ($sqpack_ver_major,
        $sqpack_ver_minor,
        $sqpack_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global sqpack_ver_major $sqpack_ver_major");
    push(@macros, "%global sqpack_ver_minor $sqpack_ver_minor");
    push(@macros, "%global sqpack_ver_patch $sqpack_ver_patch");

    my $datefile = "sqpack/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $sqpack_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global sqpack_reldate $sqpack_reldate");
}


# msged
$verfile = "msged/version.h";
if(-e $verfile && $msged == 1)
{
    @regexp = ('^#define\s+msged_VER_MAJOR\s+(\d+)',
               '^#define\s+msged_VER_MINOR\s+(\d+)',
               '^#define\s+msged_VER_PATCH\s+(\d+)');

    my ($msged_ver_major,
        $msged_ver_minor,
        $msged_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global msged_ver_major $msged_ver_major");
    push(@macros, "%global msged_ver_minor $msged_ver_minor");
    push(@macros, "%global msged_ver_patch $msged_ver_patch");

    my $datefile = "msged/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $msged_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global msged_reldate $msged_reldate");

    if($DOCDIR ne "")
    {
        my $msged_texi_date = get_commit_date("msged", "doc/manual/msged.texi");
        my $texinfo_tex_date = get_commit_date("msged", "doc/manual/texinfo.tex");
        $msged_doc_date = max($msged_texi_date, $texinfo_tex_date);
    }
}


# fidoroute
$verfile = "fidoroute/version.h";
if(-e $verfile && $fidoroute)
{
    @regexp = ('^#define\s+fidoroute_VER_MAJOR\s+(\d+)',
               '^#define\s+fidoroute_VER_MINOR\s+(\d+)',
               '^#define\s+fidoroute_VER_PATCH\s+(\d+)');

    my ($fidoroute_ver_major,
        $fidoroute_ver_minor,
        $fidoroute_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global fidoroute_ver_major $fidoroute_ver_major");
    push(@macros, "%global fidoroute_ver_minor $fidoroute_ver_minor");
    push(@macros, "%global fidoroute_ver_patch $fidoroute_ver_patch");

    my $datefile = "fidoroute/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $fidoroute_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global fidoroute_reldate $fidoroute_reldate");

    my $fidoroute_en_date = get_commit_date("fidoroute", "doc/Readme.en");
    my $fidoroute_ru_date = get_commit_date("fidoroute", "doc/Readme.ru");
    $fidoroute_doc_date = max($fidoroute_en_date, $fidoroute_ru_date);
}


# util
$verfile = "util/Fidoconfig-Token/lib/Fidoconfig/Token.pm";
if(-e $verfile && $util == 1)
{
    @regexp = ('^\$VERSION\s+=\s+\"(\d+\.\d+)\";$');

    my ($token_ver) = get_version($verfile, @regexp);
    my $token_reldate = get_commit_date(dirname($verfile), basename($verfile));
    push(@macros, "%global token_ver $token_ver");
    push(@macros, "%global token_reldate $token_reldate");

    $verfile = "util/Husky-Rmfiles/lib/Husky/Rmfiles.pm";
    @regexp = ('^\$VERSION\s+=\s+\"(\d+\.\d+)\";$');

    my ($rmfiles_ver) = get_version($verfile, @regexp);
    my $rmfiles_reldate = get_commit_date(dirname($verfile), basename($verfile));
    push(@macros, "%global rmfiles_ver $rmfiles_ver");
    push(@macros, "%global rmfiles_reldate $rmfiles_reldate");

    $verfile = "util/bin/fixOutbound.pl";
    @regexp = ('^our\s+\$VERSION\s+=\s+\"(\d+\.\d+)\";$');

    my ($fixOutbound_ver) = get_version($verfile, @regexp);
    my $fixOutbound_reldate = get_commit_date(dirname($verfile), basename($verfile));

    $verfile = "util/bin/rmLink.pl";
    @regexp = ('^our\s+\$VERSION\s+=\s+\"(\d+\.\d+)\";$');

    my ($rmLink_ver) = get_version($verfile, @regexp);
    my $util_ver = max($fixOutbound_ver, $rmLink_ver);
    my $rmLink_reldate = get_commit_date(dirname($verfile), basename($verfile));
    $util_reldate = max($fixOutbound_reldate, $rmLink_reldate);

    $verfile = "util/bin/rmLinkMail.pl";
    @regexp = ('^our\s+\$VERSION\s+=\s+\"(\d+\.\d+)\";$');

    my ($rmLinkMail_ver) = get_version($verfile, @regexp);
    $util_ver = max($util_ver, $rmLinkMail_ver);
    my $rmLinkMail_reldate = get_commit_date(dirname($verfile), basename($verfile));
    $util_reldate = max($util_reldate, $rmLinkMail_reldate);

    $verfile = "util/bin/showold.pl";
    @regexp = ('^our\s+\$VERSION\s+=\s+\"(\d+\.\d+)\";$');

    my ($showold_ver) = get_version($verfile, @regexp);
    $util_ver = max($util_ver, $showold_ver);
    my $showold_reldate = get_commit_date(dirname($verfile), basename($verfile));
    $util_reldate = max($util_reldate, $showold_reldate);
    push(@macros, "%global util_ver $util_ver");
    push(@macros, "%global util_reldate $util_reldate");

    $PERLDATADIR = "$PREFIX/share/perl5";
    push(@macros, "%global PERLDATADIR $PERLDATADIR");
}


# areastat
$verfile = "areastat/h/version.h";
if(-e $verfile && $areastat == 1)
{
    @regexp = ('^#define\s+areastat_VER_MAJOR\s+(\d+)',
               '^#define\s+areastat_VER_MINOR\s+(\d+)',
               '^#define\s+areastat_VER_PATCH\s+(\d+)');

    my ($areastat_ver_major,
        $areastat_ver_minor,
        $areastat_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global areastat_ver_major $areastat_ver_major");
    push(@macros, "%global areastat_ver_minor $areastat_ver_minor");
    push(@macros, "%global areastat_ver_patch $areastat_ver_patch");

    my $datefile = "areastat/h/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $areastat_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global areastat_reldate $areastat_reldate");
}


# nltools
$verfile = "nltools/h/version.h";
if(-e $verfile && $nltools == 1)
{
    @regexp = ('^#define\s+nltools_VER_MAJOR\s+(\d+)',
               '^#define\s+nltools_VER_MINOR\s+(\d+)',
               '^#define\s+nltools_VER_PATCH\s+(\d+)');

    my ($nltools_ver_major,
        $nltools_ver_minor,
        $nltools_ver_patch) = get_version($verfile, @regexp);

    push(@macros, "%global nltools_ver_major $nltools_ver_major");
    push(@macros, "%global nltools_ver_minor $nltools_ver_minor");
    push(@macros, "%global nltools_ver_patch $nltools_ver_patch");

    my $datefile = "nltools/h/cvsdate.h";
    my $regex = '^char\s+cvs_date\[\]\s*=\s*"(\d+)-(\d+)-(\d+)';
    $nltools_reldate = get_reldate($datefile, $regex);
    push(@macros, "%global nltools_reldate $nltools_reldate");
}


# Compute $husky_reldate as maximum reldate of its components
my @reldates;
if($smapi_reldate)
{
    push(@reldates, $smapi_reldate);
}
if($fidoconf_reldate)
{
    push(@reldates, $fidoconf_reldate);
}
if($areafix_reldate)
{
    push(@reldates, $areafix_reldate);
}
if($hptzip_reldate)
{
    push(@reldates, $hptzip_reldate);
}
if($hpt_reldate)
{
    push(@reldates, $hpt_reldate);
}
if($htick_reldate)
{
    push(@reldates, $htick_reldate);
}
if($hptkill_reldate)
{
    push(@reldates, $hptkill_reldate);
}
if($hptsqfix_reldate)
{
    push(@reldates, $hptsqfix_reldate);
}
if($hptutil_reldate)
{
    push(@reldates, $hptutil_reldate);
}
if($sqpack_reldate)
{
    push(@reldates, $sqpack_reldate);
}
if($msged_reldate)
{
    push(@reldates, $msged_reldate);
}
if($fidoroute_reldate)
{
    push(@reldates, $fidoroute_reldate);
}
if($util_reldate)
{
    push(@reldates, $util_reldate);
}
if($areastat_reldate)
{
    push(@reldates, $areastat_reldate);
}
if($nltools_reldate)
{
    push(@reldates, $nltools_reldate);
}

my $husky_reldate = max($huskylib_reldate, 0);
for my $reldate (@reldates)
{
    $husky_reldate = max($reldate, $husky_reldate);
}

push(@macros, "%global husky_ver_major $husky_ver_major");
push(@macros, "%global husky_ver_minor $husky_ver_minor");
push(@macros, "%global husky_ver_patch $husky_ver_patch");
push(@macros, "%global husky_reldate $husky_reldate");

# Compute $doc_reldate as maximum *_doc_date
for my $reldate ($fidoconf_doc_date, $hpt_doc_date, $htick_doc_date, $msged_doc_date, $fidoroute_doc_date)
{
    $doc_reldate = max($doc_reldate, $reldate);
}
push(@macros, "%global doc_reldate $doc_reldate");



#
# Write to the spec file
#
@lines = readfile($stub);
open(FH, ">", $spec) or die "Cannot open husky.spec for writing; $!";
map {print FH "$_\n"} @macros;
map {print FH "$_\n"} @lines;
