#! /usr/bin/perl -w
my $message = << 'END';
#y2autoconf [-h|--help]
#
#Create YaST2 configure.in from configure.in.in
#
#Available macros in the configure.in.in:
#  @RPMNAME@                - reads it from file RPMNAME
#  @VERSION@                - reads it from file VERSION
#  @MAINTAINER@             - reads it from file MAINTAINER
#
#  @YAST2-INIT-COMMON@      - Initialization. The generated configure.in
#                              will not work without it (or its equivalent)
#  @YAST2-INIT-PROGRAM@     - Initilazation necessary for packages with programs
#  @YAST2-INIT-PO@          -                        with translations
#  @YAST2-INIT-YCP@         -                        with YCP
#
#  @YAST2-CHECKS-COMMON@    - Checks for tools needed by all YaST2 code
#  @YAST2-CHECKS-PROGRAM@   - Additional checks for packages with programs
#  @YAST2-CHECKS-PO@        -                        with translations
#  @YAST2-CHECKS-YCP@       -                        with YCP
#  @YAST2-CHECKS-TESTSUITE@ - Checks for expect and dejagnu
#
#  @YAST2-OUTPUT@           - Creates list of Makefiles to be generated
END
#Authors:
#  Jan Holesovsky <kendy@suse.cz>, 2001
#  Michal Svec <msvec@suse.cz>
#
# $Id: y2autoconf 20732 2005-01-12 15:51:22Z mvidner $

if ($#ARGV >= 0 && ($ARGV[0] eq "-h" || $ARGV[0] eq "--help"))
{
    $message =~ s/^#//gm;	# there used to be sed here
    die $message;
}

$RPMNAME = `cat RPMNAME`;
$VERSION = `cat VERSION`;
$MAINTAINER = `cat MAINTAINER`;
chomp $RPMNAME;
chomp $VERSION;
chomp $MAINTAINER;

$OUTPUT = qx(SUBDIRS=`cat SUBDIRS 2> /dev/null` ;
	     [ -z "\$SUBDIRS" ] && SUBDIRS="./" ;
	     for DIR in \$SUBDIRS ;
	       do find \$DIR -name 'Makefile.am' | grep -v "^./$RPMNAME-" | grep -v '^\\./Makefile.am\$' | grep -v '^po/' | sed 's/.am\$//' ;
	     done;
	     [ -d "po" ] && echo "po/Makefile");

%macros =
(

    # common variables
    '@RPMNAME@' => $RPMNAME,
    '@VERSION@' => $VERSION,
    '@MAINTAINER@' => $MAINTAINER,

    # init: common stuff
    '@YAST2-INIT-COMMON@' =>
"AC_INIT($RPMNAME, $VERSION, http://www.suse.de/feedback, $RPMNAME)
dnl Check for presence of file 'RPMNAME'
AC_CONFIG_SRCDIR([RPMNAME])

dnl Checking host/target/build systems, for make, install etc.
AC_CANONICAL_TARGET
dnl Perform program name transformation
AC_ARG_PROGRAM

AC_PREFIX_DEFAULT(/usr)

AM_INIT_AUTOMAKE(tar-ustar) dnl searches for some needed programs

dnl Important YaST2 variables
VERSION=\"$VERSION\"
RPMNAME=\"$RPMNAME\"
MAINTAINER=\"$MAINTAINER\"
". '
dnl pkgconfig honors lib64
pkgconfigdir=\${libdir}/pkgconfig
yast2dir=\${prefix}/share/YaST2

ybindir=\${prefix}/lib/YaST2/bin
# FIXME duplicates execcompdir
ystartupdir=\${prefix}/lib/YaST2
plugindir=\${libdir}/YaST2/plugin
includedir=\${prefix}/include/YaST2
potdir=\${docdir}/pot

docdir=\${prefix}/share/doc/packages/$RPMNAME
mandir=\${prefix}/share/man

execcompdir=\${prefix}/lib/YaST2
agentdir=${execcompdir}/servers_non_y2

ydatadir=${yast2dir}/data
imagedir=${yast2dir}/images
themedir=${yast2dir}/theme
localedir=${yast2dir}/locale
clientdir=${yast2dir}/clients
moduledir=${yast2dir}/modules
yncludedir=${yast2dir}/include
schemadir=${yast2dir}/schema
scrconfdir=${yast2dir}/scrconf
desktopdir=\${prefix}/share/applications/YaST2

AC_SUBST(VERSION)
AC_SUBST(RPMNAME)
AC_SUBST(MAINTAINER)

AC_SUBST(pkgconfigdir)

AC_SUBST(yast2dir)

AC_SUBST(ybindir)
AC_SUBST(ystartupdir)
AC_SUBST(plugindir)
AC_SUBST(includedir)
AC_SUBST(potdir)
AC_SUBST(execcompdir)

AC_SUBST(docdir)
AC_SUBST(mandir)

AC_SUBST(ydatadir)
AC_SUBST(imagedir)
AC_SUBST(themedir)
AC_SUBST(localedir)
AC_SUBST(clientdir)
AC_SUBST(moduledir)
AC_SUBST(yncludedir)
AC_SUBST(schemadir)
AC_SUBST(scrconfdir)
AC_SUBST(agentdir)
AC_SUBST(desktopdir)
',

    # init: prog
    '@YAST2-INIT-PROGRAM@' =>
'',

    # init: translation
    '@YAST2-INIT-PO@' =>
'LL=${RPMNAME##*-}
AC_SUBST(LL)

AC_ARG_WITH(own_compendium,
	    AS_HELP_STRING([--with-own_compendium=FILE],
			   [Location of compendium file]),
	    y2compendium=yes, y2compendium=no)
if test "x$with_own_compendium" = "x" ; then
  COMPENDIUM=\'../../memory/yast2.$(LL).po\'
else
  COMPENDIUM=${with_own_compendium}
fi
AC_SUBST(COMPENDIUM)
',

    # init: YCP sources
    '@YAST2-INIT-YCP@' =>
'',

    # check: common stuff
    '@YAST2-CHECKS-COMMON@' =>
'dnl Checks for programs.
AC_PROG_INSTALL
INSTALL="${INSTALL} -p"
AC_PROG_LN_S
AC_PROG_MAKE_SET

dnl pkgconfig
AC_ARG_VAR([PKG_CONFIG_PATH], [where to search for pkg-config files])
dnl devtools
dnl ...

dnl no need for AC_ARG_VAR
AC_PATH_PROG(XGETTEXT, xgettext)
if test -z "$XGETTEXT" ; then
    AC_MSG_ERROR(xgettext is missing; please install gettext.)
fi

devtools_ybindir=`pkg-config --variable=ybindir yast2-devtools`
devtools_yast2dir=`pkg-config --variable=yast2dir yast2-devtools`

dnl producing pkg-config for others?
AM_CONDITIONAL(CREATE_PKGCONFIG, test x${CREATE_PKGCONFIG} = xtrue)

dbdir=${devtools_yast2dir}/data/docbook
STYLESHEET_HTML=${dbdir}/stylesheets/customize-html.xsl
STYLESHEET_PDF=${dbdir}/stylesheets/customize-fo.xsl
STYLESHEET_CSS=${dbdir}/css/yast2docs.css
STYLESHEET_YDOC=${dbdir}/stylesheets/ydoc.xsl
STYLESHEET_YCPDOC=${dbdir}/stylesheets/ycpdoc.xsl
AC_SUBST(STYLESHEET_HTML)
AC_SUBST(STYLESHEET_PDF)
AC_SUBST(STYLESHEET_CSS)
AC_SUBST(STYLESHEET_YDOC)
AC_SUBST(STYLESHEET_YCPDOC)

',

    # check: program
    '@YAST2-CHECKS-PROGRAM@' =>
'
AC_PATH_PROG(YDOXYGEN, ydoxygen, false, $PATH:$devtools_ybindir)
if test "$YDOXYGEN" = "false"; then
    AC_MSG_ERROR([ydoxygen is not installed])
fi

AC_PROG_CXX
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP

CFLAGS="${CFLAGS} -Wall -Wformat -Wmissing-prototypes"
CXXFLAGS="${CXXFLAGS} -Wall -Wformat"

: ${AGENT_LIBADD:=\'-L$(libdir) -lscr\'}
AC_SUBST(AGENT_LIBADD)

AC_DISABLE_STATIC
AC_PROG_LIBTOOL
AM_PROG_LIBTOOL dnl for libraries

dnl generate the config header
AC_CONFIG_HEADERS([config.h]) dnl at the distribution this done

AC_FUNC_ALLOCA
AC_HEADER_STDC
',

    # check: qt
    '@YAST2-CHECKS-QT@' =>
'dnl Check qt paths
QTDIR="/usr/lib/qt3"
QTLIBDIR="/usr/lib/qt3/lib"
AC_ARG_WITH(qt-dir,
    AS_HELP_STRING([--with-qt-dir=DIR],
		   [where the root of Qt is installed.]),
    [ QTDIR="$withval" ])
AC_ARG_WITH(qt-libdir,
    AS_HELP_STRING([--with-qt-libdir=DIR],
		   [where the Qt library is installed.]),
    [ QTLIBDIR="$withval" ])
AC_SUBST(QTDIR)
AC_SUBST(QTLIBDIR)

dnl Try to find the X includes and libraries
AC_PATH_X
if test -n "$x_includes"; then
	CPPFLAGS="$CPPFLAGS -I$x_includes"
fi
if test -n "$x_libraries"; then
	LDFLAGS="$LDFLAGS -L$x_libraries"
fi
',

    # check: qt
    '@YAST2-CHECKS-TESTSUITE@' =>
'dnl Check packages expect and dejagnu
AC_PATH_PROG(MYEXPECT, expect, false)
if test "$MYEXPECT" = "false"; then
	AC_MSG_ERROR([expect not installed])
fi
AC_PATH_PROG(MYRUNTEST, runtest, false)
if test "$MYRUNTEST" = "false"; then
	AC_MSG_ERROR([dejagnu not installed])
fi
',

    # check: translations
    '@YAST2-CHECKS-PO@' =>
'dnl searches for some needed programs
AC_PATH_PROG(MSGFMT, msgfmt, no, /usr/bin)
if test x$MSGFMT != x"/usr/bin/msgfmt"; then
  AC_MSG_ERROR(msgfmt is missing; please install gettext.)
fi
AC_PATH_PROGS(MSGCAT, msgcat pmsgcat, no)
if test x$MSGCAT = xno; then
  AC_MSG_ERROR(msgcat is missing; please install gettext-0.11pre or better.)
fi

AC_PATH_PROG(MSGMERGE, msgmerge, no, /usr/bin)
if test x$MSGMERGE != x"/usr/bin/msgmerge"; then
  AC_MSG_ERROR(msgmerge is missing; please install gettext.)
fi

AC_PATH_PROG(MSGCONV, msgconv, no, /usr/bin)
if test x$MSGCONV != x"/usr/bin/msgconv"; then
  AC_MSG_ERROR(msgconv is missing; please install gettext.)
fi
',

    # check: YCP sources
    '@YAST2-CHECKS-YCP@' =>
'AC_PATH_PROG(YCPDOC, ycpdoc, false, $PATH:$devtools_ybindir)
if test "$YCPDOC" = "false"; then
    AC_MSG_ERROR([ycpdoc is not installed])
fi
AC_PATH_PROG(YCPMAKEDEP, ycpmakedep, false, $PATH:$devtools_ybindir)
if test "$YCPMAKEDEP" = "false"; then
    AC_MSG_ERROR([ycpmakedep is not installed])
fi
',

    # generate output
    '@YAST2-OUTPUT@' =>
"AC_CONFIG_FILES(Makefile $OUTPUT)
AC_OUTPUT
"
);

# main program
print STDERR "y2autoconf: Generating toplevel configure.in\n";

open(INPUT, '<', "configure.in.in") or die "y2autoconf: Cannot open configure.in.in!\n";
open(OUTPUT, '>', "configure.in") or die "y2autoconf: Cannot open configure.in!\n";

print OUTPUT
    "dnl configure.in for $RPMNAME\n" .
    "dnl\n" .
    "dnl -- This file is generated by y2autoconf - DO NOT EDIT! --\n" .
    "dnl    (edit configure.in.in instead)\n";

# FIXME: when all packages use create-spec and everything works fine,
# replace these macros in all sources and remove them here.
%obsolete_macros =
(
    '@YAST2-INIT-AGENT@' => '@YAST2-INIT-PROGRAM@',
    '@YAST2-CHECKS-AGENT@' => '@YAST2-CHECKS-PROGRAM@',
    '@YAST2-INIT-LIB@' => '@YAST2-INIT-PROGRAM@',
    '@YAST2-CHECKS-LIB@' => '@YAST2-CHECKS-PROGRAM@',
    '@YAST2-INIT-PROG@' => '@YAST2-INIT-PROGRAM@',
    '@YAST2-CHECKS-PROG@' => '@YAST2-CHECKS-PROGRAM@',
);

$unknown_macro = 0;

while (<INPUT>)
{
    if (! /^##.*/)
    {
	foreach $macro_name ( keys %obsolete_macros )
	{
	    s/$macro_name/$obsolete_macros{$macro_name}/g;
	}

	foreach $macro_name ( keys %macros )
	{
	    s/$macro_name/$macros{$macro_name}/g;
	}

	if (/^\@.+\@/)
	{
	    chomp;
	    print STDERR "y2autoconf: ERROR: unknown macro $_\n";
	    $unknown_macro = 1;
	}

	print OUTPUT;
    }
}
close (INPUT);
close (OUTPUT);

if ($unknown_macro)
{
    print STDERR "y2autoconf: ERROR: unknown macro - update your yast2-devtools\n";
    exit 1;
}
