#! /usr/bin/env bash

set -x
typeset -r prog=$(basename "$0" .sh)
typeset -r program=$(basename "$0")
typeset -r progdir=$(\cd $(dirname "$0") && pwd -P)
typeset -r progpth=${progdir}/${program}
typeset -r progpid=$$
PS4='>mlp> '

die() { 
    echo "${prog} error:  $*" >&2
    kill -TERM ${progpid}
    exit 1
}

init() {
    local PS4=">${FUNCNAME}> "

    PROJECT=gnulib
    EADDR=bug-gnulib@gnu.org
    TARNAME=libposix
    PROJURI=http://www.gnu.org/software/gnulib
    export PROJECT EADDR TARNAME PROJURI

    \cd ${progdir}
    PATH=$(\cd .. ; pwd -P):${PATH}
    ddir=${PWD}/${TARNAME}
}

run_gnulib_tool() {
    local PS4=">${FUNCNAME}> "
    test -d "$ddir" && rm -rf $ddir
    if test -f "$ddir.tar.gz" -a "$ddir.tar.gz" -nt ../ChangeLog
    then tar -xzf "$ddir.tar.gz"
    else
        local opts="--create-testdir --source-base=lib --dir=${ddir}"

        gnulib-tool ${opts} $(posix-modules)
        tar czf "$ddir.tar.gz" $(basename $ddir)
    fi
}

# Set the time stamp
get_time_stamp() {
    local PS4=">${FUNCNAME}> "
    time_stamp=$(
        egrep '^2[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]  ' ../ChangeLog | \
            sed 's/-//g;s/ .*//;1q')
}

mk_version() {
    set -e
    local PS4=">${FUNCNAME}> "
    local date_range=${time_stamp%????}
    test "X${date_range}" = X2010 || date_range=2010-${date_range}

    cd ${TARNAME} || die failed $(history | tail -2 | head -1)
    mv gllib ${TARNAME}
    sed -i "/SUBDIRS/s/gllib/${TARNAME}/" Makefile.am

    cd ${TARNAME}
    local headerlist=$' \\\n'$(
        ls -1 *.h | sed 's/\.in\.h$/.h/' | columns -I4 --spread=1 --line=' \\\')
    sed -i -e "/^EXTRA_DIST *= *$/s/$/ version.[ch]/" \
        -e "/^noinst_LTLIB/d" \
        -e "/^noinst_LIBRARIES/s/noinst_L/lib_LTL/g" \
        -e "/noinst_HEADERS/s/noinst_H.*/nobase_include_HEADERS = \\\\/" \
        -e "/^nobase_include_HEADERS =/a${headerlist}" \
        -e "s/^libgnu_a_SOURCES *=.*/${TARNAME}_la_SOURCES = version.c/g" \
        -e "s/libgnu_a_/${TARNAME}_la_/g" \
        -e "/^lib_LTLIBRARIES *=/s/=.*/= ${TARNAME}.la/" \
        -e "/^lib_LTLIBRARIES *+/d" \
        -e "s/libgnu\\.a/libposix.la/" \
        Makefile.am

    sed -e "1,/^## *LIBPOSIX_VERSION/d" \
        -e "s/@YEAR@/${date_range}/" \
        -e "s/@DECVERSION@/${time_stamp}/" \
        -e "/^#endif.*LIBPOSIX_VERSION/q" ${progpth} \
        > version.h
    (   sed '/^#ifndef/,$d' version.h
        echo "int const _libposix_version = ${time_stamp};"
    ) > version.c

    cd ..
}

mk_config() {
    local PS4=">${FUNCNAME}> "
    mv configure.ac configure.ac-saved
    exec 3> configure.ac

    VERSION=$(echo "${time_stamp}" | \
        sed 's/\(....\)\(..\)/\1.\2./')
    export VERSION
    printf 'AC_INIT([%s],[%s],[%s],[%s],[%s])\n' \
        ${PROJECT} ${VERSION} ${EADDR} ${TARNAME} ${PROJURI} >&3
    echo "AC_CONFIG_SRCDIR([${TARNAME}/version.c])" >&3

    local sedcmd=$(
        cat <<- _EOF_
	1,/^AC_INIT(/d
	/^\$/d
	s/gllib/${TARNAME}/g
	s/LIBGNU_L/LIBPOSIX_L/g
	s/^AM_CONDITIONAL.*LIBTOOL.*/AC_PROG_LIBTOOL/
	/^gl_cond_libtool/d
	_EOF_
    )

    sed -e "${sedcmd}" configure.ac-saved >&3

    exec 3>&-
    rm -f configure.ac-saved
}

build_libposix() {
    local PS4=">${FUNCNAME}> "
    autoreconf && configure && make && make dist
}

init
run_gnulib_tool
get_time_stamp
mk_version
mk_config
build_libposix

exit $?

## LIBPOSIX_VERSION
/* Meta information about GNU libposix.
  Copyright (C) @YEAR@ Free Software Foundation, Inc.

  This program is free software: you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef _LIBPOSIX_VERSION
/* Version number: (year * 10000) + (month * 100) + day */
#define _LIBPOSIX_VERSION @DECVERSION@

#ifdef __cplusplus
extern "C" {
#endif

extern int const _libposix_version;

#ifdef __cplusplus
}
#endif
#endif /* _LIBPOSIX_VERSION */
