#!/bin/sh
#$Log: autostuff,v $
#Revision 1.15  2012/04/17 08:04:27  alliance
#Removed generation of the old alliance.spec (now uses alliance-fedora.spec).
#
#Revision 1.14  2011/09/13 11:39:15  alliance
#Remove also automake script ylwrap.
#
#Revision 1.13  2011/02/09 22:48:53  jpc
#Replace AC_PROG_RANLIB by AC_PROG_LIBTOOL.
#
#Revision 1.12  2005/04/15 08:38:29  alliance
#
#* alliance/src/ : porting under Darwin/MacOS X
#  - Bug : the Darwin link editor seems to be more strict, or having a
#     different policy about implicit static/extern variables. We have
#     to do a lot of cleanup to avoid symbols redefinition at link time.
#     We do hope it may solve the earlier bison problem with asimut...
#
#Revision 1.11  2004/07/26 17:51:23  ludo
#- operator '==' doesn't exists/not compatible with old /bin/sh version
#  (such as the one installed under Solaris or Cygwin ...)
#  Replaced by '=' and here in the context it doesn't matter
#  (no need of a pattern string matching/comparison ...)
#
#Revision 1.10  2004/07/26 12:34:39  fred
#Added a test on Makefile.am to avoid errors when making 'clean'.
#Sources without Makefile.am in their top directory are now totally
#ignored.
#
###################################################
## Possible arguments: 
##  - clean : to sweap all built files
##  - dirs  : to build only in dirs
###################################################

set -e

libtoolize="libtoolize"

if [ "`uname`" = "Darwin" ]; then
  libtoolize="g${libtoolize}"
fi

## Is it a clean
if  test "$1" = "clean" ; then
   find . -name Makefile.in -exec rm {} \; -print
   find . -name aclocal.m4 -exec rm {} \; -print
   find . -name configure -exec rm {} \; -print
   find . -name autom4te.cache -exec rm -r {} \; -print
   rm -f configure.ac
   rm -f mkinstalldirs
   rm -f missing
   rm -f install-sh
   rm -f depcomp
   rm -f ylwrap
   rm -f distrib/alliance.spec
   exit
fi 


## take parameters for directories or take all dirs by default
if [ $# -eq 0 ] ; then
dirs=`\ls -l $srcdir | grep '^d' | awk '{print $NF}'`
else
dirs=$*
fi
newdirs=""
for dir in $dirs; do
  if [ "$dir" = "CVS"            ]; then continue; fi
  if [ "$dir" = "autom4te.cache" ]; then continue; fi
  if [ "$dir" = "Linux"          ]; then continue; fi
  if [ "$dir" = "Solaris"        ]; then continue; fi
  if [ "$dir" = "documentation"  ]; then continue; fi
  if [ ! -f "$dir/Makefile.am"    ]; then continue; fi
  newdirs="$newdirs $dir"
done
dirs="$newdirs documentation"


## Order directories.
order="mbk \
       aut rds elp abl bdd log btr vex ctl ctp abe abt \
       abv fsm fks fvh ftl rtn rtd scl vpn vbh pat gcp \
       druc beh bhl bvl gscr"
ordered_dirs=""
for lib in $order; do
   if echo "$dirs" | grep $lib > /dev/null ; then
     dirs=`echo "$dirs" | sed "s, $lib , ,"`
     if [ -z "$ordered_dirs" ]; then
       ordered_dirs="$lib"
     else
       ordered_dirs="$ordered_dirs $lib"
     fi
   fi
done
ordered_dirs="$ordered_dirs $dirs"

AC_OUTPUT=`find $ordered_dirs -name Makefile.am | sed "s,\.am,,"`

rm -f configure.ac
echo ""                                      >> configure.ac
echo "AC_INIT(./autostuff)"                  >> configure.ac
echo "AM_INIT_AUTOMAKE(alliance, 5.0)"       >> configure.ac
echo ""                                      >> configure.ac
echo "AM_PROG_LEX"                           >> configure.ac
echo "AM_PROG_LIBTOOL"                       >> configure.ac
echo "AC_CHECK_HEADERS(fcntl.h malloc.h strings.h sys/time.h unistd.h)" >>  configure.ac
echo "AC_CHECK_HEADERS(strings.h unistd.h)"  >> configure.ac
echo "AC_CHECK_LIB(gen, basename)"           >> configure.ac
echo "AC_CHECK_LIB(iberty, basename)"        >> configure.ac
echo "AC_CHECK_LIB(m, exp)"                  >> configure.ac
echo "AC_CHECK_LIB(m, floor)"                >> configure.ac
echo "AC_CHECK_LIB(m, pow)"                  >> configure.ac
echo "AC_CHECK_LIB(m, sqrt)"                 >> configure.ac
echo "AC_CHECK_PROG(SED, sed, sed)"          >> configure.ac
echo "AC_CHECK_PROGS(SED, gsed sed)"         >> configure.ac
echo "AC_C_CONST"                            >> configure.ac
echo "AC_FUNC_VFORK"                         >> configure.ac
echo "AC_HEADER_STDC"                        >> configure.ac
echo "AC_HEADER_SYS_WAIT"                    >> configure.ac
echo "AC_PATH_XTRA"                          >> configure.ac
echo "AC_PROG_CC"                            >> configure.ac
echo "AC_PROG_CPP"                           >> configure.ac
echo "AC_PROG_CXX"                           >> configure.ac
echo "AC_PROG_INSTALL"                       >> configure.ac
echo "AC_PROG_MAKE_SET"                      >> configure.ac
echo "AC_PROG_LIBTOOL"                       >> configure.ac
echo "AC_PROG_YACC"                          >> configure.ac
echo "AC_TYPE_SIGNAL"                        >> configure.ac

cat >> configure.ac <<"EOF"
dnl
dnl Check for X stuff
dnl
dnl
dnl Check X options
dnl
if test "$with_x" = no; then
AC_MSG_ERROR([requires the X window system to compile and run.
                  Please do not use the configure option '--without-x'.])
fi
if test "$with_motif" = no; then
AC_MSG_ERROR([requires Motif to compile and run.
                  Please do not use the configure option '--without-motif'.])
fi
dnl
dnl
dnl Check for X libraries
dnl
if test "$with_x" != no; then
ice_save_LIBS="$LIBS"
ice_save_CFLAGS="$CFLAGS"
ice_save_CXXFLAGS="$CXXFLAGS"
ice_save_CPPFLAGS="$CPPFLAGS"
ice_save_LDFLAGS="$LDFLAGS"

dnl
dnl

AC_FIND_MOTIF
AC_FIND_XPM

dnl
dnl
dnl
LIBS="$LIBS $X_EXTRA_LIBS"
CFLAGS="$CFLAGS $X_CFLAGS"
CPPFLAGS="$CPPFLAGS $X_CFLAGS"
LDFLAGS="$LDFLAGS $X_LIBS"
dnl
dnl Check for X library
dnl
X11_LIBS=""
AC_CHECK_LIB(X11, XOpenDisplay, X11_LIBS="-lX11",,${X_PRE_LIBS} ${X_EXTRA_LIBS})
if test "$X11_LIBS" = ""; then
dnl Not having X is fatal.  Let the user fix this.
AC_MSG_ERROR([The X11 library '-lX11' could not be found.
                  Please use the configure options '--x-includes=DIR'
                  and '--x-libraries=DIR' to specify the X location.])
fi
AC_SUBST(X_LIBS)
AC_SUBST(X11_LIBS)
AC_SUBST(X_PRE_LIBS)
dnl
dnl
dnl Check for -lXintl library.  Lutz Kittler <kittler@sse-erfurt.de> says
dnl that DLD-Linux with CDE wants `-lXintl' to refine `Xlcmbcurmax'.
dnl Guenther Roehrich <guenther@studbox.uni-stuttgart.de> says that
dnl we must check for `_Xlcmbcurmax' instead of `Xlcmbcurmax'.
dnl
AC_CHECK_LIB(Xintl, _Xlcmbcurmax, X11_LIBS="${X11_LIBS} -lXintl",,
	${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
dnl
dnl
dnl Check for -lipc library.  SCO unix is said to want that.
dnl
AC_CHECK_LIB(ipc, shmap, X11_LIBS="${X11_LIBS} -lipc",,
	${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
dnl
dnl
dnl Check for X toolkit libraries
dnl
XT_LIBS=""
AC_CHECK_LIB(Xt, XtToolkitInitialize, XT_LIBS="-lXt",,
	${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
if test "$XT_LIBS" = ""; then
dnl Not having Xt is fatal.  Let the user fix this.
AC_MSG_ERROR([The X toolkit library '-lXt' could not be found.
                  Please use the configure options '--x-includes=DIR'
                  and '--x-libraries=DIR' to specify the X location.
                  See the files 'config.log' and 'ddd/config.log'
                  for further diagnostics.])
fi
AC_SUBST(XT_LIBS)
dnl
dnl
dnl Check for X extension libraries
dnl
dnl
XEXT_LIBS=""
AC_CHECK_LIB(Xext, XShapeQueryVersion, XEXT_LIBS="-lXext",,
	${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
AC_SUBST(XEXT_LIBS)
dnl
dnl
dnl
dnl Check for Xpm library and headers
dnl
XPM_LIBS=""
if test "$xpm_includes" != "no" && test "$xpm_libraries" != "no"
then
AC_CHECK_LIB(Xpm, XpmCreatePixmapFromXpmImage,
	XPM_LIBS="-lXpm"
	AC_DEFINE(HAVE_XPM),,${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
if test "$XPM_LIBS" != ""; then
AC_CHECK_HEADERS(X11/xpm.h xpm.h)
fi
fi
AC_SUBST(XPM_LIBS)

dnl Check for Motif widget libraries
dnl

XM_LIBS=""
XP_LIBS=""
if test "$motif_includes" != "no" && test "$motif_libraries" != "no"
then
dnl Motif 2.1 wants `-lXp' (X11R6.3 print server)
AC_CHECK_LIB(Xp, XpSelectInput,
	XP_LIBS="-lXp"
	AC_DEFINE(HAVE_XP),,
                   ${XPM_LIBS} ${XEXT_LIBS} ${XT_LIBS} ${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
dnl
AC_CHECK_LIB(Xm, XmCreateOptionMenu, 
	XM_LIBS="-lXm"
	AC_DEFINE(HAVE_MOTIF),,
	${XP_LIBS} ${XPM_LIBS} ${XEXT_LIBS} ${XT_LIBS} ${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
dnl
AC_CHECK_LIB(Xm, xmUseVersion, 
	AC_DEFINE(HAVE_XMUSEVERSION),,
	${XP_LIBS} ${XPM_LIBS} ${XEXT_LIBS} ${XT_LIBS} ${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
dnl
AC_CHECK_LIB(Xm, XmInstallImage,
	AC_DEFINE(HAVE_XMINSTALLIMAGE),,
	${XP_LIBS} ${XPM_LIBS} ${XEXT_LIBS} ${XT_LIBS} ${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
dnl
AC_CHECK_LIB(Xm, Xm21InstallImage,
	AC_DEFINE(HAVE_XM21INSTALLIMAGE),,
	${XP_LIBS} ${XPM_LIBS} ${XEXT_LIBS} ${XT_LIBS} ${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS})
fi
if test "$XM_LIBS" = ""; then
dnl Without Motif, we won't get far.  Let the user fix this.
AC_MSG_ERROR([The Motif library '-lXm' could not be found.
                  Please use the configure options '--with-motif-includes=DIR'
                  and '--with-motif-libraries=DIR' to specify the Xm location.
                  See the files 'config.log'
                  for further diagnostics.])
fi

AC_SUBST(XP_LIBS)
AC_SUBST(XM_LIBS)

LIBS="$LIBS ${XM_LIBS} ${XP_LIBS} ${XPM_LIBS} ${XEXT_LIBS} ${XT_LIBS} ${X_PRE_LIBS} ${X11_LIBS} ${X_EXTRA_LIBS}"
    
    
LIBS="$ice_save_LIBS"
CFLAGS="$ice_save_CFLAGS"
CXXFLAGS="$ice_save_CXXFLAGS"
CPPFLAGS="$ice_save_CPPFLAGS"
LDFLAGS="$ice_save_LDFLAGS"
fi
EOF

echo "AM_ALLIANCE"                                 >> configure.ac
echo "AM_CONDITIONAL([ALLIANCE_BUILD],[(exit 0)])" >> configure.ac

find $ordered_dirs -name configure.in | while read config; do
  echo "Scanning $config"
  echo "" >> configure.ac
  echo "dnl Infos extracted from $config"  >> configure.ac

  for version_line in  `grep -ah _CUR= $config`; do
    echo "$version_line"  >> configure.ac
    version_name=`echo $version_line | sed 's,=.*,,'`
    echo "AC_SUBST($version_name)" >> configure.ac
  done
  for version_line in  `grep -ah _REV= $config`; do
    echo "$version_line"  >> configure.ac
    version_name=`echo $version_line | sed 's,=.*,,'`
    echo "AC_SUBST($version_name)" >> configure.ac
  done
  for version_line in  `grep -ah _REL= $config`; do
    echo "$version_line"  >> configure.ac
    version_name=`echo $version_line | sed 's,=.*,,'`
    echo "AC_SUBST($version_name)" >> configure.ac
  done

  for dll_line in `grep -ah _DLL_VERSION= $config`; do
    echo "$dll_line"  >> configure.ac
    dll_name=`echo $dll_line | sed 's,=.*,,'`
    echo "AC_SUBST($dll_name)" >> configure.ac
  done
  for version_line in  `grep -ah _VERSION= $config | grep -v DLL`; do
    echo "$version_line"  >> configure.ac
    version_name=`echo $version_line | sed 's,=.*,,'`
    echo "AC_SUBST($version_name)" >> configure.ac
  done
done

echo ""                                      >> configure.ac
echo "TOOLSDIRS=\"$ordered_dirs\""           >> configure.ac
echo "AC_SUBST(TOOLSDIRS)"                   >> configure.ac

echo ""                                      >> configure.ac
echo "AC_OUTPUT(["                           >> configure.ac
echo "Makefile"                              >> configure.ac
echo "distrib/etc/alc_env.sh"                >> configure.ac
echo "distrib/etc/alc_env.csh"               >> configure.ac
for template in $AC_OUTPUT; do
  echo "$template"                           >> configure.ac
done
echo "])"                                    >> configure.ac


aclocal -I .
${libtoolize} --force --copy --automake
automake --foreign --add-missing --copy
autoconf

exit 0
