#!/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 254 2005-04-20 13:54:42Z mt $
#


#
# 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

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


#
# Generate po/Makefile.am
#

if [ x"limal-trans" = x$(cat RPMNAME | sed 's:\(limal-trans\).*:\1:') -a \
     -d po ]; then

    po_adminpath=$limaladminpath/po

    if [ -e po/Makefile.am.top ]; then
        top_part=po/Makefile.am.top
    else
	top_part=$po_adminpath/Makefile.am.top
    fi

    center_part=$po_adminpath/Makefile.am.center

    if [ -e po/Makefile.am.bottom ]; then
	bottom_part=po/Makefile.am.bottom
    else
	bottom_part=$po_adminpath/Makefile.am.bottom
    fi

    target=po/Makefile.am

    echo "${self}: Generating po/Makefile.am from"
    echo "    $top_part"
    echo "    $center_part"
    echo "    $bottom_part"

    cat >$target <<'EOF'
# Emacs: -*- makefile -*-
#
# Makefile.am for a LiMaL translation (po/) subproject
#
# -- This file is generated by limalautomake - DO NOT EDIT! --
#
EOF

    append_lines $top_part	$target
    append_lines $center_part	$target
    append_lines $bottom_part	$target

fi
