AC_INIT(thaifonts-scalable.spec.in)
AM_INIT_AUTOMAKE(thaifonts-scalable, 0.4.5)

dnl
dnl Check options
dnl
AC_ARG_ENABLE(ttf,
              [AC_HELP_STRING([--disable-ttf],
                              [disable TTF generation])],
              , enable_ttf="yes")
if test "x$enable_ttf" = "xyes"; then
  AC_ARG_WITH(ttfdir,
    [AC_HELP_STRING([--with-ttfdir=DIR],
                    [font directory in DIR [PREFIX/share/fonts/th/TTF]])],
    [ttfdir="$withval"], [ttfdir=\$\{prefix\}/share/fonts/th/TTF])
  AC_MSG_CHECKING(--with-ttfdir)
  AC_MSG_RESULT("$ttfdir")

  AC_SUBST(ttfdir)
fi

AC_ARG_ENABLE(pfa,
              [AC_HELP_STRING([--enable-pfa],
                              [enable Type1 PFA generation])],
              , enable_pfa="no")
AC_ARG_ENABLE(pfb,
              [AC_HELP_STRING([--enable-pfb],
                              [enable Type1 PFB generation])],
              , enable_pfb="no")
if test "x$enable_pfa" = "xyes" -o "x$enable_pfb" = "xyes"; then
  AC_ARG_WITH(type1dir,
    [AC_HELP_STRING([--with-type1dir=DIR],
                    [font directory in DIR [PREFIX/share/fonts/th/Type1]])],
    [type1dir="$withval"], [type1dir=\$\{prefix\}/share/fonts/th/Type1])
  AC_MSG_CHECKING(--with-type1dir)
  AC_MSG_RESULT("$type1dir")

  AC_SUBST(type1dir)
fi

AC_ARG_ENABLE(otf,
              [AC_HELP_STRING([--enable-otf],
                              [enable OpenType generation])],
              , enable_otf="no")
if test "x$enable_otf" = "xyes"; then
  AC_ARG_WITH(otfdir,
    [AC_HELP_STRING([--with-otfdir=DIR],
                    [font directory in DIR [PREFIX/share/fonts/th/OTF]])],
    [otfdir="$withval"], [otfdir=\$\{prefix\}/share/fonts/th/OTF])
  AC_MSG_CHECKING(--with-otfdir)
  AC_MSG_RESULT("$otfdir")

  AC_SUBST(otfdir)
fi

AC_ARG_ENABLE(testfont,
              [AC_HELP_STRING([--enable-testfont],
                              [enable font generation with prefix {TTF,OTF,PS}])],
              , enable_testfont="no")
if test "x$enable_testfont" = "xyes"; then
  AC_ARG_WITH(testfontdir,
    [AC_HELP_STRING([--with-testfontdir=DIR],
                    [font directory in DIR [$HOME/.fonts/testfont]])],
    [testfontdir="$withval"], [testfontdir=\$\{HOME\}/.fonts/testfont])
  AC_MSG_CHECKING(--with-testfontdir)
  AC_MSG_RESULT("$testfontdir")

  AC_SUBST(testfontdir)
fi


AM_CONDITIONAL(INCLUDE_TTF,test $enable_ttf = "yes")
AM_CONDITIONAL(INCLUDE_PFA,test $enable_pfa = "yes")
AM_CONDITIONAL(INCLUDE_PFB,test $enable_pfb = "yes")
AM_CONDITIONAL(INCLUDE_TYPE1,test $enable_pfa = "yes" -o $enable_pfb = "yes")
AM_CONDITIONAL(INCLUDE_OTF,test $enable_otf = "yes")
AM_CONDITIONAL(INCLUDE_TESTFONT,test $enable_testfont = "yes")

dnl
dnl Check Programs
dnl
AC_PROG_INSTALL
AC_PROG_LN_S
AC_CHECK_PROGS(RMDIR,rmdir,no)

AC_CHECK_PROGS(PFAEDIT,fontforge pfaedit,no)

if test "x$PFAEDIT" = "xno"; then
  AC_MSG_ERROR([You need pfaedit])
fi

# AC_CHECK_PROGS(TTMKFDIR,ttmkfdir,no)
# if test "x$TTMKFDIR" = "xno" ; then
#   AC_MSG_ERROR([You need ttmkfdir (from freetype distribution)])
# fi

dnl prefer mkfontscale over ttmkfdir
AC_CHECK_PROG(MKFONTSCALE,mkfontscale,mkfontscale)

# xorg or xfree86?
if test "x$MKFONTSCALE" = "xmkfontscale"; then
  if $MKFONTSCALE -n >/dev/null 2>&1; then
    # xorg's mkfontscale
    MKFONTSCALE="mkfontscale -a tis620-0 -a tis620-2"
    rm -f fonts.scale encodings.dir
  else
    # xfree86's mkfontscale
    MKFONTSCALE="mkfontscale -e tis620-0 -e tis620-2"
  fi
fi

if test "x$enable_ttf" = "xyes"; then
  if test "x$MKFONTSCALE" = "x"; then
    AC_CHECK_PROG(TTMKFDIR,ttmkfdir,ttmkfdir)
    if test "x$TTMKFDIR" = "x"; then
      AC_MSG_ERROR([You need mkfontscale or ttmkfdir])
    else
      if (ttmkfdir -h 2>&1 | grep -- --encoding >/dev/null); then
        # ttmkfdir 2
        TTMKFDIR="ttmkfdir -e /usr/X11R6/lib/X11/fonts/encodings/encodings.dir -o fonts.scale"
      else
        # ttmkfdir 1
        TTMKFDIR="ttmkfdir > fonts.scale"
      fi
    fi
  else
    TTMKFDIR="$MKFONTSCALE"
  fi
  AC_MSG_RESULT([Will create fonts.scale for TTF using $TTMKFDIR])
fi

if test "x$enable_pfa" = "xyes" -o "x$enable_pfb" = "xyes"; then
  if test "x$MKFONTSCALE" = "x"; then
    AC_CHECK_PROG(TYPE1INST,type1inst,type1inst)
    if test "x$TYPE1INST" = "x"; then
      AC_MSG_ERROR([You need mkfontscale or type1inst])
    fi
  else
    TYPE1INST="$MKFONTSCALE"
  fi
  AC_MSG_RESULT([Will create fonts.scale for Type1 using $TYPE1INST])
fi
AC_CHECK_PROG(FCCACHE,fc-cache,[fc-cache])

AC_OUTPUT(
  Makefile
  nf/Makefile
  nectec/Makefile
  tlwg/Makefile
  scripts/Makefile
  scripts/gen.mk
  tests/Makefile
  thaifonts-scalable.spec
)

