#!/bin/sh
#
# limalautomake - generate LiMaL "Makefile.am"
# toplevel directory: from template and ./SUBDIRS
#
# Author: Stefan Hundhammer <sh@suse.de>
#         Stefan Schubert   <schubi@suse.de>
# (c) SuSE GmbH 2001
#
# $Id: limalautomake 1862 2006-08-08 13:01:45Z mc $
#


#
# Append the contents of file $src to file $target.
#
# Parameters:
#	$src	Source file name
#	$target	Target file name
#
function append_lines()
{
    src=$1
    target=$2

    echo "# --- START lines from $src ---"	>>$target
    cat $src					>>$target
    echo "# --- END lines from $src ---"	>>$target
}


#
# Init
#

self=`basename $0`
limaladminpath=/usr/share/limal/devtools/admin

if [ "$1" == "--bootstrap" ]; then
    limaladminpath=$2
fi


#
# Generate toplevel Makefile.am
#

template=$limaladminpath/Makefile.am.toplevel

RPMNAME=`cat ./RPMNAME`

if [ ! -e VERSION ]; then
    echo "${self} ERROR: Start this only in LiMaL toplevel project directories!" >&2
    exit 1
fi

if [ ! -e SUBDIRS ]; then
    subdirs=`ls */Makefile.am | sed -e 's:/Makefile.am::' | sort | grep -v "^$RPMNAME-" | tr '\n' ' '`
    comment="No ./SUBDIRS file found - assuming default: All direct subdirs with Makefile.am"

    if [ -z "$subdirs" ]; then
        echo "${self} ERROR: No ./SUBDIRS file and no subdir with a \"Makefile.am\"" >&2
        exit 2
    fi
else
    subdirs=`cat SUBDIRS | sed -e 's/SUBDIRS *= *//'`
    comment="Contents of ./SUBDIRS"
fi


if [ ! -e $template ]; then
    echo "${self} ERROR: Can't find $template" >&2
    exit 3
fi

echo "${self}: Generating toplevel Makefile.am"
cp -f $template Makefile.am
chmod u+w Makefile.am

grep AM_GNU_GETTEXT\( configure.in >/dev/null && echo "ACLOCAL_AMFLAGS = -I m4" >>Makefile.am

test -z "$comment" || echo "# $comment" >>Makefile.am
echo "SUBDIRS = $subdirs" >>Makefile.am

