dnl -*-shell-script-*-
dnl  Process this file with autoconf to produce a configure script. 

dnl should cache result.
dnl should  look in $prefix first.


AC_DEFUN(AC_TEX_PREFIX, [
    

    AC_MSG_CHECKING(TeX/MF root dir directory)    

    find_root_prefix="$prefix"
    

    test "x$find_root_prefix" = xNONE && find_root_prefix="$ac_default_prefix"
    find_texpostfix="";
    for postfix in "/lib/tex/" "/lib/texmf" "/lib" "/tex" "/texmf"; do
	find_texprefix="$find_root_prefix$postfix"
	if test -d $find_texprefix; then
	    find_texpostfix=$postfix
	    break;
	fi
    done
    
    if test "x$find_texpostfix" = x; then
	find_texpostfix='/lib/texmf/tex'
	AC_MSG_WARN(Cannot determine the TeX-directory. Please use --enable-tex-prefix)
    fi

    find_texprefix="$find_root_prefix/$find_texpostfix"

    # only assign if variablename not empty
    if test x != "x$1"; then
    	$1='${prefix}'/"$find_texpostfix"
    fi
    AC_MSG_RESULT($find_texprefix)

])
 

# find a directory inside a prefix, 
# $1 the prefix (expanded version)
# $2 variable to assign
# $3 the directory name 
# $4 description
AC_DEFUN(AC_FIND_DIR_IN_PREFIX, [
    
    AC_MSG_CHECKING($4 directory)    
    find_dirdir=`(cd $1; 
      $FIND ./ -type d -a -name $3 -print |sort|head -1|sed 's#^\./##')`
    

    if test "x$find_dirdir" = x; then
       find_dirdir="/$3";
       AC_MSG_WARN(Cannot determine $4 subdirectory. Please set from command-line)
	true
    fi
    $2=$find_dirdir
    AC_MSG_RESULT($1/$find_dirdir)
])

AC_DEFUN(AC_TEX_SUBDIR, [
dnl    AC_REQUIRE([AC_TEX_PREFIX])
    AC_FIND_DIR_IN_PREFIX($find_texprefix, $1, tex,TeX input)
    $1="$TEXPREFIX/$$1"
])

AC_DEFUN(AC_MF_SUBDIR, [
dnl     AC_REQUIRE([AC_TEX_PREFIX])
    AC_FIND_DIR_IN_PREFIX($find_texprefix, $1, source, MF input)
    $1="$TEXPREFIX/$$1"
])
   
AC_INIT(flower/choleski.cc)

# if given here, these vars are initted at the checking point.
printing_b=no
checking_b=yes
debug_b=yes
optimise_b=no
profile_b=no
    
AC_LANG_CPLUSPLUS

AC_ARG_ENABLE(printing,
    [  enable-printing        turn on debug printing],
    [printing_b=$enableval])

	
AC_ARG_ENABLE(checking,
    [  disable-checking       set runtime checks (assert calls)],
    [checking_b=$enableval] )

AC_ARG_ENABLE(debugging,
    [  disable-debugging         set debug info ],
    [debug_b=$enableval])

AC_ARG_ENABLE(optimise,
    [  enable-optimise       use maximal speed optimisations],
    [optimise_b=$enableval])
    
   
AC_ARG_ENABLE(profiling, 
    [  enable-profiling      compile with gprof support],
    [profile_b=$enableval])
    
AC_ARG_ENABLE(tex-prefix,
    [  tex-prefix=DIR  set the tex-directory to find TeX subdirectories. (default: PREFIX)],
    [TEXPREFIX=$enableval],
    [TEXPREFIX=auto] )
    
AC_ARG_ENABLE(tex-dir,
    [  tex-dir=DIR  set the directory to put LilyPond  TeX files in. ],
    [TEXDIR=$enableval],
    [TEXDIR=auto] )
AC_ARG_ENABLE(mf-dir,
    [  mf-dir=DIR  set the directory to put LilyPond MetaFont files in.],
    [MFDIR=$enableval],
    [MFDIR=auto] )

if test $profile_b = yes; then
    EXTRA_LIBES="-pg"
    DEFINES="$DEFINES -pg"
fi
    
if test $printing_b = no; then
    DEFINES="$DEFINES -DNPRINT=1"
fi
    
if test $debug_b = yes; then	
    DEFINES="$DEFINES -g"
fi
if test $checking_b = no; then
    DEFINES="$DEFINES -DNDEBUG=1"
fi

if test $optimise_b = yes; then
    DEFINES="$DEFINES -O2 -DSTRING_UTILS_INLINED"
fi

dnl COMPILEINFO="$HOST $host $TARGET $target"
AUTOHEADER="This file was automatically generated by configure"
CXXFLAGS=${CXXFLAGS:-""}	# we don't want -g -O junk
AC_PROG_CXX
AC_PROG_RANLIB
AC_PROG_INSTALL
AC_CHECK_PROGS(FIND, find, error)

dnl should check out -print
if test FIND = error; then
   AC_MSG_WARN(Couldn't find \`find'.  Please use --enable-tex-dir)
fi
    
AC_SUBST(CXX)
AC_SUBST(DEFINES)
AC_SUBST(COMPILEINFO)
AC_SUBST(AUTOHEADER)
AC_SUBST(BISON)
AC_SUBST(FLEX)
AC_SUBST(TEXPREFIX)
AC_SUBST(TEXDIR)
AC_SUBST(MFDIR)
AC_SUBST(EXTRA_LIBES)

AC_CHECK_PROGS(BISON, bison, error)
AC_CHECK_PROGS(FLEX, flex, error)
AC_CHECK_PROGS(MAKE, make, error)
AC_CHECK_PROGS(PODMAN, pod2man, error)

    
if test "x$TEXPREFIX" = xauto ; then
    AC_TEX_PREFIX(TEXPREFIX)
else
 find_texprefix=$TEXPREFIX
fi
    
if test "x$TEXDIR" = xauto ; then
    AC_TEX_SUBDIR(TEXDIR)
fi

if test "x$MFDIR" = xauto; then
    AC_MF_SUBDIR(MFDIR)
fi
    
if test $MAKE = "error" 
then
	AC_MSG_ERROR(Please install GNU make)
else
	$MAKE -v| grep GNU > /dev/null
	if test "$?" = 1
	then
	    AC_MSG_WARN(Please install *GNU* make) 
    fi 
fi 
    
if test $BISON = "error" 
then
	AC_MSG_WARN(can't find bison. Please install Bison (1.25 or better))
else
    bison_version=`$BISON --version| sed 's/^.*version 1.//g' `
    if test $bison_version -lt 25; then
	AC_MSG_WARN(Your bison is too old (1.$bison_version). Please install 1.25)
    fi	
fi

if test $PODMAN = "error" 
then
	AC_MSG_WARN(can't find pod. You should install Perl (version 5 or better))
fi

if test $FLEX = "error" 
then
	AC_MSG_WARN(can't find flex. Please install Flex (2.5 or better))
fi

if $CXX --version | grep '2\.7' > /dev/null
then
	true
else
	AC_MSG_WARN(can't find g++ 2.7)
fi

AC_CHECK_HEADER(FlexLexer.h, true,
	AC_MSG_WARN(can't find flex header. Please install Flex headers correctly))
AC_CONFIG_SUBDIRS(flower)
AC_OUTPUT(make/out/Configure_variables.make:make/Configure_variables.make.in)



dnl URG!!!!!!
eval "DIR_DATADIR=$datadir"
DIR_DATADIR="$DIR_DATADIR/lilypond"
AC_MSG_CHECKING

cat << EOF > lib/out/config.hh

/* automatically generated by configure */
/* include this file only once! */

#define  DIR_DATADIR "$DIR_DATADIR"

EOF

CXX="$ac_cv_prog_CXX" bin/make_version >> lib/out/config.hh

touch make/out/Site.make

# ugr
(cd mi2mu; CXX="$ac_cv_prog_CXX" ../bin/make_version > out/version.hh
dnl    echo 0 > out/.build
)
# rgu
sed 's/TOPLEVEL_//g' <  .version >  lily/.version
(cd lily; CXX="$ac_cv_prog_CXX" ../bin/make_version > out/version.hh
)

$MAKE -f make/Toplevel.make.in Makefile

cat << END
Finished configuring. For making everything, do:

	make all

If you only want help on the make targets, do a

	make help

assuming that \`make' is GNU make, of course.

If you want to make site-wide extensions to the makefiles, please use

    make/out/Site.make

If you can't find a toplevel makefile, please execute

    make -f make/Toplevel.make.in Makefile              # GNU make, of course
    
END

