#!/bin/bash
#
# y2automake - generate YaST2 "Makefile.am"
# toplevel directory: from template and ./SUBDIRS
# po directory: from Makefile.am.top, template and Makefile.am.bottom
#
# Author: Stefan Hundhammer <sh@suse.de>
# (c) SuSE GmbH 2001
#
# $Id$
#


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

if [ "$1" == "--bootstrap" ]; then
    y2adminpath=$2
else
    # assuming that pkg-config can find it because presumably we're running
    # with pfx
    devtoolsprefix=`pkg-config --print-errors --variable=prefix yast2-devtools`
    y2adminpath=$devtoolsprefix/share/YaST2/data/devtools/admin
fi


#
# Generate toplevel Makefile.am
#

template=$y2adminpath/Makefile.am.toplevel
common=$y2adminpath/Makefile.am.common

RPMNAME=`cat ./RPMNAME`

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

# synchronize with y2autoconf
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

# if [ -e Makefile.am ]; then
#     mv Makefile.am ${result}.old
# fi

echo "${self}: Copying automakefiles for inclusion"
AMIS="$common `find $y2adminpath/aminclude -name \*.ami`"
for AMI in $AMIS; do
    cp -f $AMI .
    chmod u+w ${AMI##*/}
done

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"yast2-trans" = x`cat RPMNAME | sed 's:\(yast2-trans\).*:\1:'` -a \
     -d po ]; then

    po_adminpath=$y2adminpath/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 YaST2 translation (po/) subproject
#
# -- This file is generated by y2automake - DO NOT EDIT! --
#
EOF

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

fi

# keep the code, just in case ;-)
if false; then
#
# Build hack:
#  while binary packages don't build and run,
#  prevent running testsuites, without resubmitting all packages:
#
echo -n "${self}: Disabling testsuites via SUBDIRS... "
# only ycp testsuites should be deleted, keep others
KEEPLIST="liby2util yast2-packager yast2-packagemanager yast2-core yast2-ncurses yast2-qt yast2-testsuite"
#  only do it in autobuild
#  (and /usr is not the prefix here)
case `pwd` in /usr/src/packages/BUILD/*)
	for KEEPRPM in $KEEPLIST; do
	    if [ "$RPMNAME" = "$KEEPRPM" ]; then
		KEEP=1
		break
	    fi
	done
	if [ "x$KEEP" = x ]; then
	    echo "yes"
	    # remove testsuite from SUBDIRS
	    find . -name Makefile.am | xargs perl -pi.bak -e 'm/^SUBDIRS/ && s/testsuite//;'
	else
	    echo "no ($RPMNAME wants tests)"
	fi
    ;;
    *)
	echo "no (not in %build)"
esac
fi
