#! /bin/sh
# This script is a hand-made configure script. It contains a lot of
# code stolen from GNU autoconf. I removed all the code that was not
# useful for configuring a LyX installation.




####some configuration variables
lyx_check_config=yes
lyx_keep_temps=no
srcdir=


#### Parse the command line
for ac_option do
  case "$ac_option" in
    -help | --help | -h)
      cat << EOF
Usage: configure [options] 
Options: 
  --help                   show this help lines
  --keep-temps             keep temporary files (for debug. purposes)
  --without-latex-config   do not run LaTeX to determine configuration
EOF
      exit 0;;
    --without-latex-config)
      lyx_check_config=no ;;
    --keep-temps)
      lyx_keep_temps=yes ;;
  esac 
done


#### Checking for some echo oddities
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
  if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    ac_n= ac_c='
' ac_t='	'
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi


#### I do not really know why this is useful, but we might as well keep it.
# NLS nuisances.
# Only set these to C if already set.  These must not be set unconditionally
# because not all systems understand e.g. LANG=C (notably SCO).
# Fixing LC_MESSAGES prevents Solaris sh from translating var values in `set'!
# Non-C LC_CTYPE values break the ctype check.
if test "${LANG+set}"   = set; then LANG=C;   export LANG;   fi
if test "${LC_ALL+set}" = set; then LC_ALL=C; export LC_ALL; fi
if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
if test "${LC_CTYPE+set}"    = set; then LC_CTYPE=C;    export LC_CTYPE;    fi


#### Guess the directory in which configure is located.
ac_prog=$0
srcdir=`echo $ac_prog|sed 's%/[^/][^/]*$%%'` 
srcdir=`echo "${srcdir}" | sed 's%\([^/]\)/*$%\1%'`
test "x$srcdir" = "x$ac_prog" && srcdir=.
if test ! -r ${srcdir}/chkconfig.ltx ; then
  echo "configure: error: cannot find chkconfig.ltx script"
  exit 1
fi


#### Create the build directories if necessary
for dir in bind doc kbd layouts templates ; do
  test ! -d $dir && mkdir $dir
done


#### Searching some useful programs
# Search LaTeX2e
echo $ac_n "checking for a LaTeX2e program""... $ac_c"
echo "$ac_t"""
LATEX=
for ac_prog in latex latex2e
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      LATEX="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$LATEX"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
## Check whether this is really LaTeX2e
rm -f chklatex.ltx
cat >chklatex.ltx <<EOF
\\nonstopmode\\makeatletter
\\ifx\\undefined\\documentclass
  \\message{NotLaTeX2e}
\\fi
\\@@end
EOF
if eval ${LATEX} chklatex.ltx 2>/dev/null | grep 'NotLaTeX2e' >/dev/null; then
  LATEX=
  ac_result="not useable"
fi
rm -f chklatex.ltx chklatex.log
  echo "$ac_t""$ac_result"
  test -n "$LATEX" && break
fi
done

if test -z "$LATEX" ; then
  LATEX=none
    lyx_check_config=no
fi


# Search for a Postscript interpreter
echo $ac_n "checking for a Postscript interpreter""... $ac_c"
echo "$ac_t"""
GS=
for ac_prog in gs
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      GS="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$GS"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
  echo "$ac_t""$ac_result"
  test -n "$GS" && break
fi
done

if test -z "$GS" ; then
  GS=none
fi


# Search something to preview postscript
echo $ac_n "checking for a Postscript previewer""... $ac_c"
echo "$ac_t"""
GHOSTVIEW=
for ac_prog in gv ghostview
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      GHOSTVIEW="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$GHOSTVIEW"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
  echo "$ac_t""$ac_result"
  test -n "$GHOSTVIEW" && break
fi
done

if test -z "$GHOSTVIEW" ; then
  GHOSTVIEW=none
fi


# Search a *roff program (used to translate tables in ASCII export)
echo $ac_n "checking for a *roff formatter""... $ac_c"
echo "$ac_t"""
ROFF=
for ac_prog in groff nroff
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      ROFF="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$ROFF"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
  echo "$ac_t""$ac_result"
  test -n "$ROFF" && break
fi
done

if test -z "$ROFF" ; then
  ROFF=none
fi

ascii_roff_command=$ROFF
test $ROFF = "groff" && ascii_roff_command="groff -t -Tlatin1 \$\$FName"
test $ROFF = "nroff" && ascii_roff_command="tbl \$\$FName | nroff"

# Search the ChkTeX program
echo $ac_n "checking for ChkTeX""... $ac_c"
echo "$ac_t"""
CHKTEX=
for ac_prog in chktex
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      CHKTEX="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$CHKTEX"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
  echo "$ac_t""$ac_result"
  test -n "$CHKTEX" && break
fi
done

if test -z "$CHKTEX" ; then
  CHKTEX=none
fi

chktex_command=$CHKTEX
test $CHKTEX = "chktex" && chktex_command="$CHKTEX -n1 -n3 -n6 -n9 -n22 -n25 -n30 -n38"

# Search a Fax handling program
echo $ac_n "checking for a fax driver""... $ac_c"
echo "$ac_t"""
FAX=
for ac_prog in sendfax faxsend fax
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      FAX="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$FAX"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
  echo "$ac_t""$ac_result"
  test -n "$FAX" && break
fi
done

if test -z "$FAX" ; then
  FAX=none
fi

if test $FAX = sendfax ; then
  fax_command="sendfax -n -h '\$\$Host' -c '\$\$Comment' -x '\$\$Enterprise' -d '\$\$Name'@'\$\$Phone' '\$\$FName'"
elif test $FAX = faxsend ; then
  fax_command="faxsend '\$\$Phone' '\$\$FName'"
elif test $FAX = fax ; then
  fax_command="fax send '\$\$Phone' '\$\$FName'"
else
  fax_command="none"
fi

# Search for LinuxDoc/SGML-tools
echo $ac_n "checking for SGML-tools (aka LinuxDoc-SGML)""... $ac_c"
echo "$ac_t"""
SGMLTOOLS=
for ac_prog in sgml2lyx
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog ; ac_word=$2
if test -n "$ac_word"; then
  echo $ac_n "+checking for \"$ac_word\"""... $ac_c"
  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS="${IFS}:"
  for ac_dir in $PATH; do
    test -z "$ac_dir" && ac_dir=.
    if test -x $ac_dir/$ac_word; then
      SGMLTOOLS="$ac_prog"
      break
    fi
  done
  IFS="$ac_save_ifs"

  if test -n "$SGMLTOOLS"; then
    ac_result=yes
  else
    ac_result=no
  fi
  
  echo "$ac_t""$ac_result"
  test -n "$SGMLTOOLS" && break
fi
done

if test -z "$SGMLTOOLS" ; then
  SGMLTOOLS=none
fi

chk_sgmltools=no
if test $SGMLTOOLS != none; then
  chk_sgmltools=yes
  sgmltools_cmd="\\def\\hassgmltools{}"
fi


#### Explore the LaTeX configuration
echo $ac_n "checking LaTeX configuration""... $ac_c"
if test ${lyx_check_config} = no ; then
  echo "$ac_t""default"
  cat >textclass.lst <<EOF
# This file declares layouts and their associated definition files
# (include dir. relative to the place where this file is).
# It contains only default values, since chkconfig.ltx could not be run 
# for some reason. Run ./configure if you need to update it after a
# configuration change.  
article	article	article
report	report	report
book	book	book
linuxdoc	linuxdoc	linuxdoc
letter	letter	letter
EOF
  cat >chkconfig.sed <<EOF
s/@.*@/???/g
EOF
else
  echo "$ac_t""auto"
  rm -f wrap_chkconfig.ltx chkconfig.vars chkconfig.classes chklayouts.tex
  cat >wrap_chkconfig.ltx <<EOF
\\newcommand\\srcdir{${srcdir}}
${sgmltools_cmd}
\\input{${srcdir}/chkconfig.ltx}
EOF
  ## Construct the list of classes to test for.
  # build the list of available layout files and convert it to commands 
  # for chkconfig.ltx 
  for file in ./layouts/*.layout ${srcdir}/layouts/*.layout ; do 
    case $file in
      */\*.layout) ;;
      *) echo $file ;;
    esac
  done | sed -e 's%^.*layouts/\(.*\)\.layout$%\\TestDocClass{\1}%'\
             > chklayouts.tex
  eval ${LATEX} wrap_chkconfig.ltx 2>/dev/null | grep '^\+'
  eval `cat chkconfig.vars | sed 's/-/_/g'`
fi

# This is a bit of cheating since chkconfig.ltx did it for us, but it
# is better to have some output 
echo creating textclass.lst 
echo creating packages.lst

echo creating doc/LaTeXConfig.lyx
echo "s/@chk_sgmltools@/$chk_sgmltools/g" >> chkconfig.sed
sed -f chkconfig.sed ${srcdir}/doc/LaTeXConfig.lyx.in >doc/LaTeXConfig.lyx

echo creating lyxrc.defaults
rm -f lyxrc.defaults
cat >lyxrc.defaults <<EOF
# This file has been automatically generated by LyX' lib/configure
# script. It contains default settings that have been determined by
# examining your system. PLEASE DO NOT MODIFY ANYTHING HERE! If you
# want to customize LyX, make a copy of the file LYXDIR/lyxrc as
# ~/.lyx/lyxrc and edit this file instead. Any setting in lyxrc will
# override the values given here.
\\latex_command "$LATEX"
\\ps_command "$GS"
\\view_ps_command "$GHOSTVIEW -swap"
\\view_pspic_command "$GHOSTVIEW"
\\ascii_roff_command "$ascii_roff_command"
\\chktex_command "$chktex_command"
\\fax_command "$fax_command"
\\font_encoding "$chk_fontenc"
EOF

if test $srcdir != . ; then
  for file in lyxrc.defaults textclass.lst packages.lst \
              doc/LaTeXConfig.lyx ; do
    if test -r $srcdir/$file && diff $file $srcdir/$file 1>&2 2>/dev/null ; 
    then 
      echo "removing $file, which is identical to the system global version"
      rm -f $file
    fi
  done
fi

# Final clean-up
if test $lyx_keep_temps = no ; then
rm -f chkconfig.sed chkconfig.vars wrap_chkconfig.* chklayouts.tex \
      missfont.log
fi
