#!/usr/bin/perl
###############################################################################
#                             Hwclock Configure
###############################################################################
#  This program creates config.mk, which tells the Hwclock make file about
#  the build environment and the user's build choices
#
#  See README for how to work around this program if it gives you any trouble.
###############################################################################




use strict;
use warnings;
use English;

my $TRUE=1; my $FALSE = 0;

sub findIoH($$) {
    my ($haveIoHR, $ioHNamespaceR) = @_;
#-----------------------------------------------------------------------------
#  Determine in what namespace the include file io.h resides.  On older
#  systems, one finds it in 'asm', while in newer systems it is in 'sys'.
#  on some, it may be in both.
#
#  We do a cheap test, just looking at typical places the files might exist.
#  A better test would be a test compile, but that is difficult and there
#  probably isn't much call to compile on the older systems anyway, so we just
#  bias our decision toward the newer scheme.
#-----------------------------------------------------------------------------
    # Note: we saw a system in March 2013 that actually had <asm/io.h> in
    # its include path, but not from /usr/include.  It was in with the GCC
    # header files, but was in fact the kernel's traditional io.h.  It did
    # not work (compile) in user space, though.

    if (-f '/usr/include/asm/io.h') {
        $$haveIoHR = '1';
        $$ioHNamespaceR = 'asm';
    } elsif (-f '/usr/include/sys/io.h') {
        $$haveIoHR = '1';
        $$ioHNamespaceR = 'sys';
    } else {
        $$haveIoHR = '0';
        $$ioHNamespaceR = '';
    }
}



sub writeConfigMk($$) {
    my ($haveIoH, $ioHNamespace) = @_;

    open(CONFIG_MK, '>', 'config.mk')
        or die("Cannot open 'config.mk' for writing");

    print CONFIG_MK ("# This was created by 'configure'.\n");
    print CONFIG_MK ("# Its purpose is to be included by Makefile \n");
    print CONFIG_MK ("\n");

    print CONFIG_MK ("HAVE_IO_H = $haveIoH\n");
    print CONFIG_MK ("IO_H_NAMESPACE = $ioHNamespace\n");

    close(CONFIG_MK);
}



###############################################################################
#                             MAINLINE
###############################################################################


findIoH(\my $haveIoH, \my $ioHNamespace);

writeConfigMk($haveIoH, $ioHNamespace);
