#!/bin/sh
#
#	Configuration.
#

#
if ./which uname > /dev/null ; then
  sysname=`uname -s`
  sysversion=`uname -r`
  platform=`uname -m`
elif [ -f /usr/sony/bin/machine -a -f /etc/osversion ]; then
  sysname='NEWS-OS'
  sysversion=`sed 's/[^ ]* [^ ]* //' /etc/osversion`
  platform=`/usr/sony/bin/machine`
fi

host=`hostname`
locale_dir='(NONE)'

sysversion1=`echo $sysversion | awk -F. '{print $1}'`
sysversion2=`echo $sysversion | awk -F. '{print $2}'`
sysversion3=`echo $sysversion | awk -F. '{print $3}'`

echo $sysname $sysversion1 $sysversion2 $sysversion3 /$platform at $host

if [ -f config.param ] ; then
  . ./config.param
fi

# parameters:

prefix=/usr/local
ssl_includedir=
ssl_libdir=
gc_includedir=
gc_libdir=
mimetypes=

all_yes=0
while [ $# -gt 0 ]; do
  case "$1" in #(

   -yes|--yes|-nonstop|--nonstop) #(
   all_yes=1
   echo "Setting all parameters to the default..."
   ;;
   
   -prefix|--prefix) #(
   prefix=$2
   shift
   ;;

   -prefix=*|--prefix=*) #(
   prefix=`expr "$1" : "-*prefix=\(.*\)"`
   ;;

   -suffix|--suffix) #(
   suffix=$2
   shift
   ;;

   -suffix=*|--suffix=*) #(
   suffix=`expr "$1" : "-*suffix=\(.*\)"`
   ;;

   -bindir=*|--bindir=*)
   bindir=`expr "$1" : "-*bindir=\(.*\)"`
   ;;

   -auxbindir=*|--auxbindir=*)
   auxbindir=`expr "$1" : "-*auxbindir=\(.*\)"`
   ;;

   -libdir=*|--libdir=*)
   libdir=`expr "$1" : "-*libdir=\(.*\)"`
   ;;

   -helpdir=*|--helpdir=*)
   helpdir=`expr "$1" : "-*helpdir=\(.*\)"`
   ;;

   -mandir=*|--mandir=*)
   mandir=`expr "$1" : "-*mandir=\(.*\)"`
   ;;

   -sysconfdir=*|--sysconfdir=*)
   sysconfdir=`expr "$1" : "-*sysconfdir=\(.*\)"`
   ;;

   -lang=) #(
   lang=`expr "$1" : "-*lang=\(.*\)"`

   case "$lang" in #(
    [Mm][Aa]*) lang=MANY;; #(
    [Jj][Aa]*) lang=JA;; #(
    [Ee][Nn]*) lang=EN;; #(
    *|.*) lang=;;
   esac

   ;;

   -libmoe=*) #(
   libmoedir=`expr "$1" : "-*libmoe=\(.*\)"`
   ;;

   -mb_h=*) #(
   mb_h_dir=`expr "$1" : "-*mb_h=\(.*\)"`
   ;;

   -mk_btri=*) #(
   mk_btri_dir=`expr "$1" : "-*mk_btri=\(.*\)"`
   ;;

   -cpp=*) #(
   dcpp=`expr "$1" : "-*cpp=\(.*\)"`
   ;;

   -accept_lang=*) #(
   accept_lang=`expr "$1" : "-*accept_lang=\(.*\)"`
   ;;

   -locale_dir=*) #(
   locale_dir=`expr "$1" : "-*locale_dir=\(.*\)"`
   ;;

   -model=*|--model=*) #(
   dmodel_name=`expr "$1" : "-*model=\(.*\)"`

   case "$dmodel_name" in #(
    baby) dmodel=1;; #(
    little) dmodel=2;; #(
    mouse) dmodel=3;; #(
    cookie) dmodel=4;; #(
    monster) dmodel=5;; #(
    *|.*) dmodel=6;;
   esac

   ;;

   -code=*|--code=*)
   display_code=`expr "$1" : "-*code=\(.*\)"`
   ;;

   -cflags=*|--cflags=*)
   dcflags=`echo $1 | sed -e 's/-*cflags=//'`
   ;;

  -ldflags=*|--ldflags=*)
   dldflags=`echo $1 | sed -e 's/-*ldflags=//'`
   ;;

   --ssl-includedir=*)
   ssl_includedir=`expr "$1" : "--ssl-includedir=\(.*\)"`
   ;;

   --ssl-libdir=*)
   ssl_libdir=`expr "$1" : "--ssl-libdir=\(.*\)"`
   ;;

   --gc-includedir=*)
   gc_includedir=`expr "$1" : "--gc-includedir=\(.*\)"`
   ;;

   --gc-libdir=*)
   gc_libdir=`expr "$1" : "--gc-libdir=\(.*\)"`
   ;;

   --mimetypes=*)
   mimetypes=`expr "$1" : "--mimetypes=\(.*\)"`
   ;;

   -help|--help)
   echo "-yes, -nonstop  Set all parameters to the default"
   echo "-prefix=DIR     Specify prefix (default: /usr/local)"
   echo "-suffix=DIR     Specify suffix (default: <empty string>)"
   echo "-bindir=DIR     user executable in DIR (default: PREFIX/bin)"
   echo "-auxbindir=DIR  support programs in DIR (default: PREFIX/lib/w3m)"
   echo "-libdir=DIR     local cgi in DIR (default: PREFIX/lib/w3m/cgi-bin)"
   echo "-helpdir=DIR    help file in DIR (default: PREFIX/share/w3m)"
   echo "-mandir=DIR     manual in DIR (default: PREFIX/man)"
   echo "-sysconfdir=DIR w3mconfig in DIR [PREFIX/etc/w3m]"
   echo "-lang=(en|ja|many)"
   echo "                Specify default language"
   echo "-libmoe=<directory>"
   echo "                Specify directory in which multi"
   echo "                character encoding support library resides"
   echo "-mb_h=<directory>"
   echo "                Specify directory in which header file of multi"
   echo "                character encoding support library resides"
   echo "-mk_btri=<directory>"
   echo "                Specify directory in which command to create"
   echo "                initial data of binary tri."
   echo "-accept_lang=<accept lang>"
   echo "                Specify the value for accept-language header"
   echo "-locale_dir=<locale directory>"
   echo "                Specify the directory under which locale data reside."
   echo "                If you don't want gettextization"
   echo "                please specify the string '(NONE)'"
   echo "-model=(baby|little|mouse|cookie|monster|custom)"
   echo "                Specify default model"                     
   echo "-code=(S|E|j|N|n|m)"
   echo "                Specify derault kanji code"
   echo "-cflags=FLAGS   Specify C flags"
   echo "-ldflags=FLAGS  Specify LD flags"
   echo "--ssl-includedir=DIR	specify OpenSSL include dir"
   echo "--ssl-libdir=DIR	specify OpenSSL lib dir"
   echo "--gc-includedir=DIR	specify Boehm-GC include dir"
   echo "--gc-libdir=DIR	specify Boehm-GC lib dir"
   echo "--mimetypes=PATH	specify mime.types path"
   echo "-help           Display help"
   exit 0
   ;;
 esac
 shift
done

echo "# Configuration at $host" > config.param

if [ "x$IMLIB_CONFIG" = x ]; then
 IMLIB_CONFIG=imlib-config
fi

if [ "x$IMLIB2_CONFIG" = x ]; then
 IMLIB2_CONFIG=imlib2-config
fi

if [ "x$GDKPIXBUF_CONFIG" = x ]; then
 GDKPIXBUF_CONFIG=gdk-pixbuf-config
fi


if [ -z "`echo -n aho | grep n`" ] ; then
Echo() {
 echo -n "$*"
}
else
Echo() {
 echo "$*\c"
}
fi


do_sigtest() {
 echo "#include <signal.h>" > _zmachdep.c

 if [ "$2" = void ]; then
  echo "$1 _handler($2) {}" >> _zmachdep.c
 else
  echo "$1 _handler($2 x) {}" >> _zmachdep.c
 fi

 echo "int main(void) { $1 (*hdl)($2); hdl = signal(SIGINT,_handler); return 0; }" >> _zmachdep.c
 $cc $cflags -o _zmachdep _zmachdep.c > _zwarning 2>&1 
 stat=$?
 warning=`cat _zwarning`
 rm -f _zwarning
}

do_localetest() {
 cat > _zmachdep.c <<EOF
#include <locale.h>
#include <libintl.h>

$use_locale_dir

int main() {
setlocale(LC_MESSAGES, "");
textdomain("w3m");
bindtextdomain("w3m", LOCALE_DIR);
return 0;
}
EOF

 if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
  echo "You don't need \"-lintl\""
 else
  echo "You need \"-lintl\""
  intllib=-lintl
 fi
}

readdir() { 
 if [ "$all_yes" = 0 ]; then
  read __dir
 else
  __dir=$1
  echo "$1"
 fi

 if [ -z "$__dir" ]; then
  _dir=$1
 else
  _dir=`echo "$__dir"|sed -e "s;^~;$HOME;"`
 fi
}

readanswer() {
 var=$1
 dflt=$2
 ok=$3

 if [ "$all_yes" = 0 -o -z "$dflt$ok" ]; then
  read ans

  if [ -z "$ans" ]; then
   ans=$dflt
  fi
 else
  ans=$dflt
  echo "$ans"
 fi

 eval $var='$ans'
}

yesno() {
 var=$1
 dflt=$2
 ddflt=$3

 if [ -z "$dflt" ]; then
  dflt=$ddflt
 fi

 if [ "$dflt" = y ]; then
  ndflt=n
 else
  ndflt=y
 fi

 Echo "[$dflt]? "

 if [ "$all_yes" = 0 ]; then
  read ks_ans
 else
  ks_ans=$dflt
  echo "$dflt"
 fi

 if [ "$ks_ans" = "$ndflt" ]; then
  eval $var='$ndflt'
 else
  eval $var='$dflt'
 fi
}

#
# def_param "name" "default_value"
# =>
#  $name=${name:-$default_value}
#  def_$name='#define $NAME' (if $name=y)
#  def_$name='#undef $NAME'  (otherwise)
#
def_param() {
 flag=$1
 eval "fvar=\$$flag"
 dflt=$2

 if [ -z "$fvar" ]; then
  eval "fvar=$dflt"
 fi

 def=`echo $flag | tr '[a-z]' '[A-Z]'`

 if [ "$fvar" = "y" ]; then
  eval "def_$flag='#define $def'"
 else
  eval "def_$flag='#undef $def'"
 fi

 eval "echo $flag=$fvar >> config.param"
}

#
# ask_choice "prompt" "var"
#   "choice-1" "value-1" "description-1" .. .
#   "choice-2" "value-2" "description-2" .. .
# =>
#  $var=${value-<n>}
#  def_$var='#define $VAR ${value-<n>}'
# 
ask_choice() {
 prompt=$1
 var=$2
 def=`echo $var | tr '[a-z]' '[A-Z]'`
 shift; shift
 list="$@"

 echo "$prompt"
 dc=
 dvar=
 varvar=
 # check old value
 eval "old_dvar=\$$var"
 set -- $list

 while [ -n "$1" -a -n "$2" ]; do
  c=$1; v=$2; shift; shift;

  case $v in #(
   $old_dvar) dc=$c; dvar=$old_dvar;; #(
   *) ;;
  esac

  while [ "$1" != "." ]; do
   shift;
  done

  shift
 done

 if [ "$all_yes" = 1 ]; then
  eval "varvar=$dc"
 fi

 while [ -z "$varvar" ]; do
  set -- $list
  prompt="["

  while [ -n "$1" -a -n "$2" ]; do
   c=$1; v=$2;
   shift; shift;
   Echo "$c .."

   while [ "$1" != "." ]; do
    Echo " $1"; shift
   done

   echo " ($def=$v)"
   shift
   prompt="$prompt$c"
  done

  prompt="$prompt]? "
  Echo "$prompt"

  if [ -n "$dvar" ]; then
   Echo "(default: $dc - $dvar)"
  fi

  read REPLY

  if [ -z "$REPLY" ]; then
   REPLY=$dc
  fi

  set -- $list

  while [ -n "$1" -a -n "$2" ]; do
   case $REPLY in #(
    $1) eval "varvar=$1"
    break
    ;;
   esac

   shift; shift

   while [ "$1" != "." ]; do
    shift;
   done

   shift
  done
 done

 Echo "You select"
 set -- $list

 while [ -n "$1" -a -n "$2" ]; do
  c=$1; v=$2; shift; shift
  case $varvar in #(

   $c) #(

   while [ "$1" != "." ]; do
    Echo " $1"; shift
   done

   shift
   eval "def_$var='#define $def $v'"
   eval "$var=$v"
   eval "echo $var=$v >> config.param";
   echo " $def=$v"
   break
   ;;

   *)

   while [ "$1" != "." ]; do
    shift;
   done

   shift
   ;;
  esac
 done
}

#
# ask_param "prompt" "name" "default_value"
# =>
#  ask user
#  def_param "name" "default_value"
#
ask_param() {
 prompt=$1
 flag=$2
 eval "fvar=\$$flag"
 dflt=$3
 Echo "$prompt "

 if [ "$all_yes" = 0 ]; then
  yesno $flag $fvar $dflt
 fi

 def_param $flag $dflt

 if [ "$all_yes" = 1 ]; then
  eval "fvar=\$$flag"

  if [ "$fvar" = y ]; then
   echo ".... yes"
  else
   echo ".... no"
  fi
 fi
}

find_ssl() {
 sslinclude=""
 for i1 in $ssl_includedir /usr /usr/local $prefix; do
  for i2 in /openssl /ssl /; do
   if [ "$i2" = "/" ]; then
    i2='';
   fi

   dirname=${i1}${i2}

   if [ -f $dirname/include/ssl.h ]; then
    sslinclude="-I${dirname}/include"
   elif [ -f $dirname/include/openssl/ssl.h ]; then
    sslinclude="-I${dirname}/include/openssl -I${dirname}/include"
   fi

   for i3 in lib/openssl lib; do
    dirname=${i1}${i2}/${i3}

    for ext in a $so_ext; do
     if [ -f $dirname/libssl.$ext -o -f $dirname/libcrypto.$ext ]; then
      if [ "$ssllib" = -L${dirname} ]; then
       ssllib="-L${dirname}"
      else
       ssllib="$ssllib -L${dirname}"
      fi
     fi
    done
   done
  done
 done

 if [ -f "$ssl_libdir/libssl.$ext" -o -f "$ssl_libdir/libcrypto.$ext" ]; then
  ssllib="-L$ssl_libdir $ssllib"
 fi

 ssllib="$ssllib -lssl -lcrypto"

 if [ "$sslinclude" = "" ]; then
  echo "Where is ssl.h? (for example, /usr/crypto/include)"
  Echo ":"
  read ks_ans
  sslinclude="-I${ks_ans}"

  if [ -d $ks_ans/openssl ]; then
   sslinclude="${sslinclude} -I${ks_ans}/openssl"
  fi

  echo "Where is libssl.a? (for example, /usr/crypto/lib)"
  Echo ":"
  read ks_ans
  ssllib="-L${ks_ans} -lssl -lcrypto"
 fi
}

cleanup_lang() {
 expr "$1" : '\([^;, 	][^;, 	]*\).*'
}

find_imlib() {
  imlib_major=1
  imlib_minor=9
  imlib_micro=8
  imlib_version=$imlib_major.$imlib_minor.$imlib_micro
  echo "Checking Imlib."
  version=`$IMLIB_CONFIG --version`
  if [ "x$version" = x ]; then
    echo "You don't have Imlib. Install Imlib (version >= $imlib_version)."
    return 1
  fi
  major=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1/'`
  minor=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\2/'`
  micro=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\3/'`
  echo "The version of Imlib is $version."
  if [ "$major" -ne $imlib_major -o "$minor" -ne $imlib_minor -o "$micro" -lt $imlib_micro ]; then
    echo "Imlib is too old. Install Imlib (version >= $imlib_version)."
    return 1
  fi
  return 0
}

find_imlib2() {
  imlib_major=1
  imlib_minor=0
  imlib_micro=5
  imlib_version=$imlib_major.$imlib_minor.$imlib_micro
  echo "Checking Imlib 2."
  version=`$IMLIB2_CONFIG --version`
  if [ "x$version" = x ]; then
    echo "You don't have Imlib 2. Install Imlib 2 (version >= $imlib_version)."
    return 1
  fi
  major=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1/'`
  minor=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\2/'`
  micro=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\3/'`
  echo "The version of Imlib 2 is $version."
  if [ "$major" -ne $imlib_major -o "$minor" -lt $imlib_minor -o "$micro" -lt $imlib_micro ]; then
    echo "Imlib 2 is too old. Install Imlib 2 (version >= $imlib_version)."
    return 1
  fi
  return 0
}

find_gdkpixbuf() {
  gdkpixbuf_major=0
  gdkpixbuf_minor=16
  gdkpixbuf_micro=0
  gdkpixbuf_version=$gdkpixbuf_major.$gdkpixbuf_minor.$gdkpixbuf_micro
  echo "Checking GdkPixbuf ."
  if ./which $GDKPIXBUF_CONFIG > /dev/null
  then
    version=`$GDKPIXBUF_CONFIG --version`
  else
    version=""
  fi
  if [ "x$version" = x ]; then
    echo "You don't have GdkPixbuf. Install GdkPixbuf (version >= $gdkpixbuf_version)."
    return 1
  fi
  major=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1/'`
  minor=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\2/'`
  micro=`echo "$version" | sed 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\3/'`
  echo "The version of GdkPixbuf is $version."
  if [ "$major" -ne $gdkpixbuf_major -o "$minor" -lt $gdkpixbuf_minor -o "$micro" -lt $gdkpixbuf_micro ]; then
    echo "GdkPixbuf is too old. Install GdkPixbuf (version >= $gdkpixbuf_version)."
    return 1
  fi
  return 0
}

#--------------------------------------------------------------
if [ -n "$USER" ]; then
 user=$USER
elif [ -n "$LOGNAME" ]; then
 user=$LOGNAME
elif [ -n "`whoami`" ]; then
 user=`whoami`
else
# Echo "Who are you? "
# read user
 user=nobody
fi

echo "%"
echo "% Hello $user. Let's start configuration process for w3m."
echo "% Please answer some questions."
echo "%"

extension=
def_use_binstream='#undef USE_BINMODE_STREAM'

topdir=$prefix
special_sys=''

case "$sysname" in #(
 aix | AIX ) #(
 special_sys="#define AIX"
 ;;

 CYGWIN* ) #(
 sysname='CYGWIN'
 special_sys="#define CYGWIN $sysversion1"

 if [ $sysversion1 -eq 0 ]; then
  topdir=/cygnus/cygwin-b20/H-i586-cygwin32
 fi

 extension='.exe'
 def_use_binstream='#define USE_BINMODE_STREAM'
 ;;

 OS/2 ) #(
 extension='.exe'
 def_use_binstream='#define USE_BINMODE_STREAM'
 ;;

 NetBSD )
# Newer NetBSD system does not define 'unix' symbol anymore, but GC library
# requires it.
 special_sys="#define unix"
 ;;
esac

Echo "Do you need extra suffix to 'w3m' [$suffix]? "
if [ -z "$suffix" ]; then
 readanswer suffix '(NONE)'
else
 readanswer suffix "$suffix"
fi

if [ "X$suffix" = 'X(NONE)' ]; then
 suffix=
fi

echo "suffix='$suffix'" >> config.param

# determine shared object extention
so_ext=so

case "$sysname" in #(
 HP-UX )
 so_ext=sl
 ;;

 Darwin )
 so_ext=dylib
 ;;
esac

d_bindir=$bindir

if [ -z "$d_bindir" ]; then
 d_bindir="$topdir/bin"
fi

echo "Which directory do you want to put the binary?"
Echo "(default: $d_bindir) "
readdir "$d_bindir"
bindir=$_dir
echo "bindir='$bindir'" >> config.param

d_auxbindir=$auxbindir

if [ -z "$d_auxbindir" ]; then
 case "$sysname" in
  *BSD)
  d_auxbindir="$topdir/libexec/w3m$suffix"
  ;;

  *)
  d_auxbindir="$topdir/lib/w3m$suffix"
  ;;
 esac
fi

echo "Which directory do you want to put the support binary files?"
Echo "(default: $d_auxbindir) "
readdir "$d_auxbindir"
auxbindir=$_dir
echo "auxbindir='$auxbindir'" >> config.param

d_libdir=$libdir

if [ -z "$d_libdir" ]; then
 case "$sysname" in #(
  *BSD) #(
#  d_libdir="$topdir/libexec/w3m$suffix/cgi-bin"
  d_libdir="$auxbindir"
  ;;

  *)
#  d_libdir="$topdir/lib/w3m$suffix/cgi-bin"
  d_libdir="$auxbindir"
  ;;
 esac
fi

echo "Which directory do you want to use local cgi?"
Echo "(default: $d_libdir) "
readdir "$d_libdir"
libdir=$_dir
echo "libdir='$libdir'" >> config.param

d_helpdir=$helpdir

if [ -z "$d_helpdir" ]; then
 d_helpdir="$topdir/share/w3m$suffix"
fi

echo "Which directory do you want to put the helpfile?"
Echo "(default: $d_helpdir) "
readdir "$d_helpdir"
helpdir=$_dir
echo "helpdir='$helpdir'" >> config.param

d_mandir=$mandir

if [ -z "$d_mandir" ]; then
 d_mandir="$topdir/man"
fi

echo "Which directory do you want to put the manfile?"
Echo "(default: $d_mandir) "
readdir "$d_mandir"
mandir=$_dir
echo "mandir='$mandir'" >> config.param

d_sysconfdir=$sysconfdir

if [ -z "$d_sysconfdir" ]; then
 d_sysconfdir="$topdir/etc/w3m$suffix"
fi

echo "Which directory do you want to put the system wide w3m configuration file?"
Echo "(default: $d_sysconfdir) "
readdir "$d_sysconfdir"
sysconfdir=$_dir
echo "sysconfdir='$sysconfdir'" >> config.param

if [ -z "$mk_btri_dir" -o \! -x "$mk_btri_dir/mk_btri" ]; then
 mk_btri_dir=
 mk_btri_dirs='/usr/local/libexec/moe /usr/libexec/moe /usr/share/moe'

 if [ "X$libmoedir" \!= X ]; then
  mk_btri_dirs="$mk_btri_dirs $libmoedir"
 fi

 for dir in $mk_btri_dirs; do
  if [ -x $dir/mk_btri ]; then
   mk_btri_dir=$dir
   break;
  fi
 done
fi

echo mk_btri_dir=$mk_btri_dir >> config.param

many_lang_choice=

if [ -n "$mk_btri_dir" -a -x $mk_btri_dir/mk_btri ]; then
 if [ -z "$libmoedir" ]; then
  libmoedirs='/usr/local/lib /usr/lib'

  for dir in $libmoedirs; do
   if [ -r $dir/libmoe.so -o -r $dir/libmoe.a ]; then
    libmoedir=$dir
    break
   fi
  done
 fi

 if [ -z "$libmoedir" ]; then
  echo "I looked for libmoe.{so,a} in the directories \"$libmoedirs\","
  echo "but couldn't find either."
  mb_h_dir=
 elif [ -r "$libmoedir/libmoe.so" -o -r "$libmoedir/libmoe.a" ]; then
  if [ -z "$mb_h_dir" ]; then
   mb_h_dirs='/usr/local/include/moe /usr/local/include /usr/include'
   for dir in $mb_h_dirs; do
    if [ -r $dir/mb.h ]; then
     mb_h_dir=$dir
     break
    fi
   done
  fi

  if [ -n "$mb_h_dir" -a -r $mb_h_dir/mb.h ]; then
   many_lang_choice="3 MANY Many (ISO 2022, Shift_JIS, Big Five, UTF-8, and so on) ."
  else
   echo "I looked for mb.h in the directories \"$mb_h_dirs\","
   echo "but couldn't find it."
   libmoedir=
   mb_h_dir=
  fi
 else
  echo "I looked for libmoe.{so,a} in the directory \"$libmoedir\","
  echo "but couldn't find either."
  libmoedir=
  mb_h_dir=
 fi
fi

ask_choice "Which language do you prefer?" lang \
1 JA "Japanese (charset ISO-2022-JP, EUC-JP, Shift_JIS)" . \
2 EN "English (charset US_ASCII, ISO-8859-1, etc)" . \
$many_lang_choice

case "$lang" in #(
 JA) #(

 ask_choice "What is your display kanji code?" display_code \
 S S "Shift JIS" . \
 E E "EUC-JP" . \
 j j 'JIS: ESC $@ - ESC (J' . \
 N N 'JIS: ESC $B - ESC (J' . \
 n n 'JIS: ESC $B - ESC (B' . \
 m m 'JIS: ESC $@ - ESC (B' .

 system_code=

 if [ "$display_code" = "S" ]; then
  system_code=S
  def_param system_code S
 else
  system_code=E
  def_param system_code E
 fi

 ;;

 *)
 display_code=x
 system_code=x
 def_param display_code x
 def_param system_code x
 ;;
esac

alien_objs="altmalloc.o uirx.o btri.o wcrx.o"
mb_ldflag=
mb_cflag=

if [ "X$lang" = XMANY ]; then
 echo libmoedir=$libmoedir >> config.param
 echo mb_h_dir=$mb_h_dir >> config.param
 mb_ldflag="$mb_ldflag -L$libmoedir -lmoe"
 mb_cflag="$mb_cflag -DHAVE_MOE -I$mb_h_dir"
 alien_objs=
fi

Echo "Which language do you accept [$accept_lang]? "
readanswer alg_ans "$accept_lang"

if [ -n "$alg_ans" ]; then
 use_alg="#define ACCEPT_LANG \"$alg_ans\""
else
 use_alg="#undef ACCEPT_LANG"
fi

echo "accept_lang=\"$alg_ans\"" >> config.param
alg_ans=`cleanup_lang "$alg_ans"`

use_locale_dir="#undef LOCALE_DIR"
intllib=
def_locale_dir=
def_textdomain=

if [ "$lang" \!= JA -a -n "`./which msgfmt`" ]; then
 if [ -z "$locale_dir" ]; then
  for d in /usr/share/locale /usr/local/share/locale; do
   if [ -d "$d/." ]; then
    locale_dir=$d
    break
   fi

  done
 fi

 Echo "Under which directory locale data reside [$locale_dir]? "
 readanswer locale_dir "$locale_dir"
 echo "locale_dir='$locale_dir'" >> config.param

 if [ -n "$locale_dir" -a "X$locale_dir" \!= "X(NONE)" -a -d "$locale_dir/." -a -d po/. ]; then
  use_locale_dir="#define LOCALE_DIR \"$locale_dir\""
  def_locale_dir="$locale_dir"
  def_textdomain="#define W3M_TEXTDOMAIN \"w3m$suffix\""
 elif [ -n "$locale_dir" -a "X$locale_dir" \!= "X(NONE)" ]; then
  if [ \! -d "$locale_dir/." ]; then
   echo "$locale_dir is not a directory"
  else
   echo "no message catalog"
  fi
 fi
fi

if [ "$lang" = "MANY" -o "$lang" = "JA" ]; then
 ask_param "Use 2-byte character for table border, item, etc." kanji_symbols y
else
 kanji_symbols=n
 def_param kanji_symbols n
fi

ask_param "Use Lynx-like key binding as default" use_lynx_key n
if [ "$use_lynx_key" = y ]; then
  keymap_file="keybind_lynx"
else
  keymap_file="keybind"
fi

if [ "$lang" = "JA" ]; then
  if [ "$use_lynx_key" = y ]; then
    helpfile="w3mhelp-lynx_ja.html"
  else
    helpfile="w3mhelp-w3m_ja.html"
  fi
else
  if [ "$use_lynx_key" = y ]; then
    helpfile="w3mhelp-lynx_en.html"
  else  
    helpfile="w3mhelp-w3m_en.html"
  fi
fi

###

echo ""
echo "Let's do some configurations. Choose config option among the list."
echo ""
echo "1 - Baby model    (no color, no menu, no mouse, no cookie, no SSL)"
echo "2 - Little model  (color, menu, no mouse, no cookie, no SSL)"
echo "3 - Mouse model   (color, menu, mouse, no cookie, no SSL)"
echo "4 - Cookie model  (color, menu, mouse, cookie, no SSL)"
echo "5 - Monster model (with everything; you need openSSL library)"
echo "6 - Customize"
echo ""
Echo "Which? "
if [ -n "$dmodel" ]; then
  Echo "(default: $dmodel) "
fi

include_opt=y

while :
do
 readanswer ans "$dmodel"

 if [ -z "$ans" -a -n "$dmodel" ]; then
  ans=$dmodel
 fi

 dmodel_save="$dmodel"
 dmodel=$ans

 case "$ans" in #(
  1) #(
  use_color=n
#  use_menu=n
  use_mouse=n
  use_cookie=n
  use_ssl=n
  use_romaji=n
  customized=y
  ;;

  2) #(
  use_color=y
#  use_menu=y
  use_mouse=n
  use_cookie=n
  use_ssl=n
  use_romaji=n
  customized=y
  ;;

  3) #(
  use_color=y
#  use_menu=y
  use_mouse=y
  use_cookie=n
  use_ssl=n
  use_romaji=n
  customized=y
  ;;

  4) #(
  use_color=y
#  use_menu=y
  use_mouse=y
  use_cookie=y
  use_ssl=n
  use_romaji=n
  customized=y
  ;;

  5) #(
  use_color=y
#  use_menu=y
  use_mouse=y
  use_cookie=y
  use_ssl=y
  use_romaji=y
  customized=y
  ;;

  6) #(
  if [ "X$dmodel_save" = X6 ]; then
   customized=y
  else
   customized=n
  fi

  ;;

  *)
  echo "Please input 1-6."
  Echo "Which? "
  continue
  ;;
 esac

 break
done

if [ -n "$dmodel" ]; then
 echo "dmodel=$dmodel" >> config.param
fi

save_all_yes=$all_yes

if [ "$customized" = y ]; then
 all_yes=1
fi

ask_param "Use color ESC sequence for kterm/pxvt" use_color y
ask_param "Use mouse (requires xterm/kterm/gpm/sysmouse)" use_mouse n
# ask_param "Use popup menu" use_menu y
ask_param "Use cookie" use_cookie n
ask_param "Use SSL (need OpenSSL library; Please see http://www.openssl.org/)" use_ssl n
ask_param "Use romaji search (using migemo; Please see http://migemo.namazu.org/)" use_romaji n
def_romaji_filter=${def_romaji_filter:-"migemo -t egrep /usr/local/share/migemo/migemo-dict"}
echo "def_romaji_filter='$def_romaji_filter'" >> config.param

all_yes=$save_all_yes

if [ "$use_ssl" = y ]; then
 find_ssl
fi

if [ "$use_ssl" = y ]; then
  ask_param "SSL verification support (SSL library >= version 0.8)" use_ssl_verify n
  ask_param "Digest Auth support" use_digest_auth y
else
  use_ssl_verify=n
  def_param "use_ssl_verify" n
  use_digest_auth=n
  def_param "use_digest_auth" n
fi

#ask_param "Inline image support" use_image n
init_use_image="$use_image"
if [ "x$use_image" = x ]; then
 use_image=y
 init_use_image=n
fi
if [ "$use_image" = y ]; then
  def_use_image="#define USE_IMAGE"
  imgtarget='$(IMGDISPLAY)'
  ask_param "X11 inline image support (you need Imlib1 or GdkPixbuf library)" use_w3mimg_x11 "$init_use_image"
  d_w3mimg_fb=n
#  case $sysname in
#  Linux|linux|LINUX)
#    if test -c /dev/fb0; then
#      d_w3mimg_fb=y
#    fi
#    ask_param "Linux Framebuffer inline image support (you need Imlib2 or GdkPixbuf)" use_w3mimg_fb $d_w3mimg_fb
#    if [ "$use_w3mimg_fb" = y ]; then
#	ask_param "setuid w3mimgdisplay to open /dev/fb0?" w3mimgdisplay_setuid  y
#    fi
#    ;;
#  esac
fi

if [ "$use_image" = y ]; then
 if [ "$use_w3mimg_x11" = y ]; then
  :
 elif [ "$use_w3mimg_fb" = y ]; then
  :
 else
  use_image=n
 fi
fi

if [ "$use_image" = y ]; then
 def_param "use_image" y
else
 def_param "use_image" n
 def_use_image="#undef USE_IMAGE"
 imgtarget=''
 use_w3mimg_x11=n
 use_w3mimg_fb=n
fi

if [ "$use_color" = y ]; then
  ask_param "ANSI color escape sequences support" use_ansi_color n
else
  use_ansi_color=n
  def_param "use_ansi_color" n
fi

# protocols?
ask_param "Use w3mmail.cgi" use_w3mmailer y
ask_param "NNTP support" use_nntp $include_opt
ask_param "Gopher support" use_gopher $include_opt

# obsoleted?
# ask_param "Use matrix in rendering table" use_matrix n

ask_param "Use mark operation" use_mark $include_opt

if [ "$use_image" = y ]; then
  ask_param "X-Face support (you need uncompface)" use_xface n
else
  use_xface=n
fi

### only use config.param
def_param use_dict y
def_param use_history y
def_param use_bg_color y
def_param format_nice n
def_param id_ext y
def_param use_bufinfo y
def_param use_egd y
def_param enable_remove_trailingspaces n
def_param menu_thin_frame n
def_param emacs_like_lineedit $include_opt
def_param vi_prec_num $include_opt
def_param ftppass_hostnamegen $include_opt

def_param table_expand n
def_param table_no_compact n
def_param nowrap y
def_param matrix y

if [ -z "$ded" ]; then ded=`./which \vi`; fi
if [ -n  "`echo $ded | grep 'no'`" ]; then ded=vi; fi
echo "Input your favorite editor program."
Echo "(Default: $ded) "
readdir "$ded"
editor=$_dir
echo "ded='$editor'" >> config.param

if [ "$use_w3mmailer" != 'y' ]; then
 if [ -z "$dmail" ]; then
  if ./which \mailx > /dev/null; then
   dmail=`./which \mailx` 
  else
   dmail=`./which \mail`
  fi

  if [ -n  "`echo $dmail | grep 'no'`" ]; then dmail=mailx; fi
 fi

 echo "Input your favorite mailer program."
 Echo "(Default: $dmail) "
 readdir "$dmail"
 mailer=$_dir
else
 mailer="$dmail"
fi

echo "dmail='$mailer'" >> config.param

if [ -z "$dbrowser" ]; then
 if ./which netscape > /dev/null; then
  dbrowser=`./which netscape`
 elif ./which iexplore > /dev/null; then
  dbrowser=`./which iexplore`
 else
  dbrowser=`./which lynx`
 fi

 if [ -n  "`echo $dbrowser | grep 'no'`" ]; then dbrowser=netscape; fi
fi

echo "Input your favorite external browser program."
Echo "(Default: $dbrowser) "
readdir "$dbrowser"
brz=$_dir
echo "dbrowser='$brz'" >> config.param

def_image_viewer=${def_image_viewer:-xv}
echo "def_image_viewer='$def_image_viewer'" >> config.param
def_audio_player=${def_audio_player:-showaudio}
echo "def_audio_player='$def_audio_player'" >> config.param

if [ -z "$dcc" ]; then 
 if [ -n "$CC" ]; then
  dcc="$CC"
 elif ./which gcc >/dev/null ; then
  dcc=gcc
 else
  dcc=cc
 fi
fi

echo "Input your favorite C-compiler."
Echo "(Default: $dcc) "
readanswer cc "$dcc"
echo "dcc='$cc'" >> config.param

if [ -z "$dcpp" ]; then
 if ./which cpp >/dev/null; then
  dcpp=cpp
 else
  dcpp="$cc -E"
 fi
fi

echo "Input your favorite C Preprocessor."
Echo "(Default: $dcpp) "
readanswer cpp "$dcpp"
echo "dcpp='$cpp'" >> config.param

if [ -z "$dcflags" ]; then
 dcflags=${CFLAGS-"-O"}
fi

echo "Input your favorite C flags."
Echo "(Default: $dcflags) "
readanswer cflags "$dcflags"
echo "dcflags='$cflags'" >> config.param

bsdinclude=''

if [ ! -f /usr/include/netinet/in.h ]; then
 if [ -f /usr/include/bsd/netinet/in.h ]; then
  bsdinclude='-I/usr/include/bsd'
 elif [ -f /usr/bsdinclude/netinet/in.h ]; then
  bsdinclude='-I/usr/bsdinclude'
 else
  echo "It seems you don't have some include files for networking."
 fi
fi

termlib=''

cat > _zmachdep.c << EOF
main()
{
  char bp[100];
  tgetent(bp,getenv("TERM"));
}
EOF

if [ -z "$dtermlib" ]; then
 TERM_LIBS='termcap termlib terminfo mytinfo curses ncurses'

 for lib in $TERM_LIBS; do
  for tlibdir in /lib /usr/lib /usr/local/lib /usr/ucblib /usr/ccslib /usr/ccs/lib $prefix/lib; do
   if [ -f $tlibdir/lib$lib.a -o -f $tlibdir/lib$lib.$so_ext ]; then
    # check if the lib works...
    Echo "Terminal library -l$lib found at $tlibdir, "

    if $cc $cflags -o _zmachdep _zmachdep.c -l$lib > /dev/null 2>&1; then
     echo "and it seems to work."
     termlib=-l$lib
    else
     echo "but it doesn't seem to work."
    fi
   fi
  done
 done

 if [ -z "$termlib" ]; then
  Echo "termcap/curses library not found; I hope -ltermcap works."
  termlib='-ltermcap'
 fi

 dtermlib=$termlib
fi

echo 'Which terminal library do you want to use? (type "none" if you do not need one)'
Echo "(default: $dtermlib) "
readanswer termlib "$dtermlib"

if [ "$termlib" = none ]; then
 termlib=""
else
 echo "dtermlib='$termlib'" >> config.param
fi

## Setup for math library
if [ $sysname = Rhapsody -o $sysname = "Mac OS" ]; then
 echo "MacOS X doesn't need -lm."
 mathlib=""
else
 mathlib="-lm"
fi

## look for GPM library
use_gpm=""
gpmlib=""

if [ "$use_mouse" = y ]; then
 for tlibdir in /lib /usr/lib /usr/local/lib $prefix/lib; do
  if [ -f $tlibdir/libgpm.a -o -f $tlibdir/libgpm.so ]; then
   echo "GPM library found."
   use_gpm="#define USE_GPM"
   gpmlib="-lgpm"
  fi
 done
fi

case $sysname in #(
 freebsd|FreeBSD) #(
 use_sysmouse="#define USE_SYSMOUSE"
 ;;

 *)
 use_sysmouse="#undef USE_SYSMOUSE"
 ;;
esac

extlib=''

case $sysname in #(
 *bsd|*BSD) #(
 searchlibs=""
 ;;

 *)
 searchlibs="bsd BSD 44bsd socket nsl"
 ;;
esac

for lib in $searchlibs; do
 for tlibdir in /lib /usr/lib /usr/local/lib /usr/ucblib /usr/ccslib /usr/ccs/lib $prefix/lib; do
  if [ -f $tlibdir/lib$lib.a -o -f $tlibdir/lib$lib.$so_ext ]; then
   extlib="$extlib -l$lib"
   break
  fi
 done
done

if [ $sysname = "HP-UX" ]; then
 extlib="$extlib -ldld"
fi

if [ -n "$extlib" ]; then
 echo "additional library found: $extlib"
fi

extlib="$extlib $ldflags"

## Search pthread, Linux requires this (at least ia64,alpha,s390?). 
## Other platform?
pthreadlib=

case $sysname in #(
 Linux|linux|LINUX)
 for tlibdir in /lib /usr/lib; do
  if [ -f $tlibdir/libpthread.a -o -f $tlibdir/libpthread.$so_ext ]; then
   echo "$tlibdir/libpthread found"
   pthreadlib="-L$tlibdir -lpthread"
   break
  fi
 done
 ;;
esac

gclib=''
gcinclude=''
gctarget=''
gcversion=

for tlibdir in ${gc_libdir} /lib /usr/lib /usr/local/lib /usr/ucblib /usr/ccslib /usr/ccs/lib $prefix/lib ${HOME}/lib; do
 if [ -f $tlibdir/libgc.a -o -f $tlibdir/libgc.$so_ext ]; then
  echo "$tlibdir/libgc found"
  gclib="-L$tlibdir -lgc"
  break
 fi
done

for inc in \
 ${gc_includedir} \
 ./gc/include \
 /usr/include \
 /usr/include/gc \
 /usr/local/include \
 /usr/local/include/gc \
 $prefix/include \
 ${HOME}/include \
 ; do
 if [ -f $inc/gc.h ]; then
  echo "$inc/gc.h found"
  gcinclude=$inc
  break
 fi
done

case $sysname in #(
 linux|Linux|LINUX|aix|Aix|AIX) #(

 if [ \! -f "$gcinclude/private/gc_priv.h" ]; then
  case $cflags in #(
   *DEBIAN*) #(
   # on Debian, we can use libgc*.deb
   :;;

   *)
   # these OS requires gcmain.c, which include gc/gc_priv.h
   # therefore we use gc library comes with w3m
   echo "Your OS is $sysname; using gc library comes with w3m."
   gclib=""
   ;;

  esac
 fi
 ;;

esac

if [ -n "$gclib" -a -n "$gcinclude" ]; then
 cat > _zmachdep.c << EOF
#include <gc.h>
main()
{
  extern unsigned GC_version;
  printf("%d%02d%03d\n",(GC_version>>16)&0xff,(GC_version>>8)&0xff,GC_version&0xff);
}
EOF

 if $cc $cflags -I$gcinclude -o _zmachdep _zmachdep.c $gclib > /dev/null 2>&1; then
  echo "and it seems to work."
  gcversion=`./_zmachdep`
 fi
fi

if [ -z "$gcversion" ]; then
 if [ "X$gcinclude" = X./gc/include ]; then
  gclib="gc/gc.a"
  gctarget="$gclib"
 else
  echo "You first need to install Boehm-GC library,"
  echo "(see http://www.hpl.hp.com/personal/Hans_Boehm/gc/)"

  if [ -n "$gcinclude" -a \! -f $gcinclude/private/gc_priv.h ]; then
   echo "Warning: You probably need to issue"

   if [ \! -d $gcinclude/private/. ]; then
    echo " mkdir $gcinclude/private"
    echo and
   fi

   echo " cp include/gc/private/gc_priv.h to $gcinclude/private"
  fi

  rm -f XXMakefile config.h
  exit 1
 fi
fi

# arch specific flags for libgc
gc_cflags=''

if [ "$gclib" = "gc/gc.a" ]; then
 patchfile=""

 case "$platform:$sysname" in #(
  ia64:Linux|alpha:Linux|s390*:Linux) #(
  gc_cflags="-DGC_LINUX_THREADS -D_REENTRANT"
  ;;

  R3000:*System_V*|R4000:UNIX_SYSV|R*000:UNIX_SV)
  # EWS-4800
  gc_cflags=-Dmips
  ;;
 esac

 # Apply patch.
 if [  ! -f patch_done -a -n "$patchfile" -a -f "$patchfile" ]; then
  patch -lp0 < $patchfile
  echo "dpatch='$patch'" >> config.param
  touch patch_done
 fi
fi

echo "GC_version is $gcversion."
cflags="$cflags -I$gcinclude"

echo "Input additional LD flags other than listed above, if any:"

if [ -z "$dldflags" ]; then
 dldflags=${LDFLAGS-""}
fi

if [ -n "$dldflags" ]; then
 Echo "(default: $dldflags) : "
else
 Echo ": "
fi

readanswer ldflags "$dldflags" ok

if [ -z "$ldflags" ]; then
 ldflags=$dldflags
fi

echo "dldflags='$ldflags'" >> config.param

echo "Checking machine dependency."

###### mime.types

MIME_TYPES=""

if [ "X$mimetypes" \!= X ]; then
 MIME_TYPES="$mimetypes"
else
 for d in \
  /etc /usr/lib /usr/local/lib /usr/local/etc $prefix/lib /usr/local/etc/apache \
  /usr/local/lib/mosaic /usr/local/mosaic /usr/local/netscape /usr/local/lib/netscape
 do
  if [ -f $d/mime.types ]; then
   MIME_TYPES="$d/mime.types"
  fi
 done
fi

if [ -z "$MIME_TYPES" ]; then
 echo "Global mime.types not found; Hope /usr/local/lib/mime.types works."
 MIME_TYPES=/usr/local/lib/mime.types
fi

####### ranlib

if ./which ranlib > /dev/null; then
 echo "You have ranlib."
 ranlib_cmd=ranlib
else
 if [ $sysname = "OS/2" ]; then
  ranlib_cmd=rem
 else
  echo "You don't have ranlib."
  ranlib_cmd=:
 fi
fi

####### mkdir -p

if mkdir -p hogege; then
 echo "You have mkdir -p."
 MKDIR="mkdir -p"
else
 MKDIR="mkdir"
fi

rm -rf hogege 2>&1 >/dev/null

####### perl

perl=`./which perl`

if [ `expr "$perl" : 'not found'` != 0 ]; then
 echo "You don't have perl."
 perl=/usr/local/bin/perl
 have_perl=n
else
 echo "You have perl."
 have_perl=y
fi

def_param use_help_cgi $have_perl

####### long long
cat > _zmachdep.c <<EOF
main()
{
  int i = sizeof(unsigned long long);
}
EOF
if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1
then
    echo "You have long long"
    def_have_long_long="typedef long long clen_t;"
    cat > _zmachdep.c <<EOF
#include <stdlib.h>
main()
{
    const char *s = "1";
    long long ll = strtoll(s, NULL, 10);
}
EOF
    if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1
    then
	echo "You have strtoll()"
	def_have_strtoll="#define HAVE_STRTOLL"
    else
	echo "You don't have strtoll()"
	def_have_strtoll="#undef HAVE_STRTOLL"
    fi
    cat > _zmachdep.c <<EOF
#include <sys/types.h>
#include <stdlib.h>
#include <limits.h>
main()
{
    const char *s = "1";
    quad_t q = strtoq(s, NULL, 10);
}
EOF
    if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1
    then
	echo "You have strtoq()"
	def_have_strtoq="#define HAVE_STRTOQ"
    else
	echo "You don't have strtoq()"
	def_have_strtoq="#undef HAVE_STRTOQ"
    fi
else
    echo "You don't have long long"
    def_have_long_long="typedef long clen_t;"
fi

####### strcasecmp

cat > _zmachdep.c << EOF
#include <string.h>
main()
{
  int i;
  i = strcasecmp("abc","def");
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have strcasecmp()."
 def_have_strcasecmp="#define HAVE_STRCASECMP"
else
 echo "You don't have strcasecmp()."
 def_have_strcasecmp="#undef HAVE_STRCASECMP"
fi

####### strcasestr
cat > _zmachdep.c << EOF
#include <string.h>
main()
{
  int i;
  i = strcasestr("abc","def");
}
EOF
if $cc $cflags -D_GNU_SOURCE -o _zmachdep _zmachdep.c > /dev/null 2>&1
then
  echo "You have strcasestr()."
  def_have_strcasestr="#define HAVE_STRCASESTR"
else
  echo "You don't have strcasestr()."
  def_have_strcasestr="#undef HAVE_STRCASESTR"
fi

####### strchr

cat > _zmachdep.c << EOF
#include <string.h>
main()
{
  char *p, *q = "abc";
  p = strchr(q,'c');
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have strchr()."
 def_have_strchr="#define HAVE_STRCHR"
else
 echo "You don't have strchr()."
 def_have_strchr="#undef HAVE_STRCHR"
fi

####### strerror

cat > _zmachdep.c << EOF
main()
{
  int i;
  i = strerror(0);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have strerror()."
 def_have_strerror="#define HAVE_STRERROR"
else
 echo "You don't have strerror()."
 def_have_strerror="#undef HAVE_STRERROR"
fi

####### sys_errlist

cat > _zmachdep.c << EOF
main()
{
  extern char sys_errlist[];
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have sys_errlist[]."
 def_have_syserrlist="#define HAVE_SYS_ERRLIST"
else
 echo "You don't have sys_errlist[]."
 def_have_syserrlist="#undef HAVE_SYS_ERRLIST"
fi

####### bcopy

cat > _zmachdep.c << EOF
main()
{
  char x[1],y[1];
  bzero(x,1);
  bcopy(x,y,1);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have bcopy()."
 def_have_bcopy="#define HAVE_BCOPY"
else
 echo "You don't have bcopy()."
 def_have_bcopy="#undef HAVE_BCOPY"
fi

####### waitpid

cat > _zmachdep.c << EOF
#include <sys/types.h>
#include <sys/wait.h>

main()
{
    pid_t pid;
    int status;
    if ((pid = fork()) == 0) {
       sleep(10);
       exit(1);
    }
    while(waitpid(pid,&status,WNOHANG));
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have waitpid()."
 def_have_waitpid="#define HAVE_WAITPID"
else
 echo "You don't have waitpid()."
 def_have_waitpid="#undef HAVE_WAITPID"
fi

####### wait3

cat > _zmachdep.c << EOF
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#ifndef NULL
#define NULL 0
#endif

main()
{
    int pid;
    int status;
    if ((pid = fork()) == 0) {
       sleep(10);
       exit(1);
    }
    while(wait3(&status,WNOHANG,NULL) > 0);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have wait3()."
 def_have_wait3="#define HAVE_WAIT3"
else
 echo "You don't have wait3()."
 def_have_wait3="#undef HAVE_WAITPID"
fi

####### strftime

cat > _zmachdep.c << EOF
#include <time.h>

main()
{
    time_t ct;
    struct tm *tm;
    char t[80];
    time(&ct);
    strftime(t, 80, "%a, %d %b %Y %H:%M:%S GMT",gmtime(&ct));
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have strftime()."
 def_have_strftime="#define HAVE_STRFTIME"
else
 echo "You don't have strftime()."
 def_have_strftime="#undef HAVE_STRFTIME"
fi

####### getcwd

cat > _zmachdep.c << EOF
#include <sys/param.h>
#include <unistd.h>
main()
{
#ifdef MAXPATHLEN
  char path[MAXPATHLEN];
  getcwd(path,MAXPATHLEN);
#else
  gwtcwd(NULL, 0);
#endif
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have getcwd()."
 def_have_getcwd="#define HAVE_GETCWD"
else
 echo "You don't have getcwd()."
 def_have_getcwd="#undef HAVE_GETCWD"
fi

####### getwd

cat > _zmachdep.c << EOF
main()
{
  char path[64];
  getwd(path);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have getwd()."
 def_have_getwd="#define HAVE_GETWD"
else
 echo "You don't have getwd()."
 def_have_getwd="#undef HAVE_GETWD"
fi

####### symlink
cat > _zmachdep.c << EOF
main()
{
  char path[64],lpath[64];
  symlink(path,lpath);
}
EOF
if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1
then
  echo "You have symlink()."
  def_have_symlink="#define HAVE_SYMLINK"
else
  echo "You don't have symlink()."
  def_have_symlink="#undef HAVE_SYMLINK"
fi

####### readlink

cat > _zmachdep.c << EOF
main()
{
  char path[64],lpath[64];
  readlink(path,lpath,64);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have readlink()."
 def_have_readlink="#define HAVE_READLINK"
else
 echo "You don't have readlink()."
 def_have_readlink="#undef HAVE_READLINK"
fi

####### lstat
cat > _zmachdep.c << EOF
#include <sys/types.h>
#include <sys/stat.h>
main()
{
  struct stat st;
  char path[64];
  lstat(path,&st);
}
EOF
if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1
then
  echo "You have lstat()."
  def_have_lstat="#define HAVE_LSTAT"
else
  echo "You don't have lstat()."
  def_have_lstat="#undef HAVE_LSTAT"
fi

####### setenv

cat > _zmachdep.c << EOF
#include <stdlib.h>
main()
{
  setenv("HOGE","hoge",1);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have setenv()."
 def_have_setenv="#define HAVE_SETENV"
else
 echo "You don't have setenv()."
 def_have_setenv="#undef HAVE_SETENV"
fi

####### putenv

cat > _zmachdep.c << EOF
#include <stdlib.h>
main()
{
  putenv("HOGE=hoge");
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have putenv()."
 def_have_putenv="#define HAVE_PUTENV"
else
 echo "You don't have putenv()."
 def_have_putenv="#undef HAVE_PUTENV"
fi

####### sigsetjmp

cat > _zmachdep.c << EOF
#include <setjmp.h>
main()
{
  jmp_buf env;
  if (sigsetjmp(env,1) != 0) {
    exit(0);
  }
  siglongjmp(env,1);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have sigsetjmp()."
 def_setjmp="#define SETJMP(env) sigsetjmp(env,1)"
 def_longjmp="#define LONGJMP(env,val) siglongjmp(env,val)"
 def_jmpbuf="#define JMP_BUF sigjmp_buf"
else
 echo "You don't have sigsetjmp()."
 def_setjmp="#define SETJMP(env) setjmp(env)"
 def_longjmp="#define LONGJMP(env,val) longjmp(env,val)"
 def_jmpbuf="#define JMP_BUF jmp_buf"
fi

####### srand48

cat > _zmachdep.c << EOF
#include <stdlib.h>
main()
{
  srand48(0);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have srand48()."
 def_have_srand48="#define HAVE_SRAND48"
else
 echo "You don't have srand48()."
 def_have_srand48="#undef HAVE_SRAND48"
fi

####### srandom

cat > _zmachdep.c << EOF
#include <stdlib.h>
main()
{
  srandom(0);
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have srandom()."
 def_have_srandom="#define HAVE_SRANDOM"
else
 echo "You don't have srandom()."
 def_have_srandom="#undef HAVE_SRANDOM"
fi

####### getpassphrase

 cat > _zmachdep.c << EOF
#include <unistd.h>
main()
{
  getpassphrase("");
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have getpassphrase()."
 def_have_getpassphrase="#define HAVE_GETPASSPHRASE"
else
 echo "You don't have getpassphrase()."
 def_have_getpassphrase="#undef HAVE_GETPASSPHRASE"
fi

####### chdir

cat > _zmachdep.c <<EOF
#include <stdio.h>
#include <unistd.h>
main()
{
 int (*c)() = chdir;
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have chdir()."
 def_have_chdir="#define HAVE_CHDIR"
else
 echo "You don't have chdir()."
 def_have_chdir="#undef HAVE_CHDIR"
fi

####### fclose

cat > _zmachdep.c << EOF
#include <stdio.h>
#include <stdlib.h>
main()
{
  void (*c)() = fclose;
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "fclose() is declared."
 dcl_fclose=''
else
 echo "fclose() is not declared."
 dcl_fclose='void fclose(FILE*);'
fi

####### pclose

cat > _zmachdep.c << EOF
#include <stdio.h>
#include <stdlib.h>
main()
{
  void (*c)() = pclose;
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "pclose() is declared."
 dcl_pclose=''
else
 echo "pclose() is not declared."
 dcl_pclose='void pclose(FILE*);'
fi

####### termios/termio/sgtty

def_term_if='#define SGTTY'

if [ $sysname = "HP-UX" ]; then
 echo "Your OS is HP-UX; using termio"
 def_term_if="#define HAVE_TERMIO_H"
elif [ $sysname = "CYGWIN" ]; then
 echo "Your OS is CYGWIN; using termios"
 def_term_if="#define HAVE_TERMIOS_H"
elif [ $sysname = "OS/2" ]; then
 echo "Your OS is OS/2; using termios"
 def_term_if='#define HAVE_TERMIOS_H'
elif [ -r /usr/include/termios.h ]; then
 echo "You have termios."
 def_term_if='#define HAVE_TERMIOS_H'
elif [ -r /usr/include/termio.h ]; then
 echo "You have termio."
 def_term_if='#define HAVE_TERMIO_H'
elif [ -r /usr/include/sgtty.h ]; then
 echo "You have sgtty."
 def_term_if='#define HAVE_SGTTY_H'
else
 echo "Do you have tty interface? I can't find one but I hope sgtty works..."
fi

####### dirent/direct

def_dir_if=''

if [ $sysname = "CYGWIN" ]; then
 echo "Your OS is CYGWIN; using dirent.h"
 def_dir_if='#define HAVE_DIRENT_H'
elif [ $sysname = "OS/2" ]; then
 echo "Your OS is OS/2; using dirent.h"
 def_dir_if='#define HAVE_DIRENT_H'
elif [ -r /usr/include/dirent.h ]; then
 echo "You have dirent.h."
 def_dir_if='#define HAVE_DIRENT_H'
elif [ -r /usr/include/sys/dir.h ]; then
 echo "You have sys/dir.h."
 def_dir_if=''
else
 echo "Do you have directory interface? I can't find one but I hope sys/dir.h works..."
fi

# check signal handler

do_sigtest int int

if [ $stat = 0 -a -z "$warning" ]; then
 echo 'signal handler is int handler(int).'
 def_sig_type='typedef int MySignalHandler;'
 def_sig_arg='#define SIGNAL_ARG int _dummy'
 def_sig_arglist='#define SIGNAL_ARGLIST 0'
 def_sig_return='#define SIGNAL_RETURN return 0'
else
 do_sigtest int void

 if [ $stat = 0 -a -z "$warning" ]; then
  echo 'signal handler is int handler(void).'
  def_sig_type='typedef int MySignalHandler;'
  def_sig_arg='#define SIGNAL_ARG void'
  def_sig_arglist='#define SIGNAL_ARGLIST'
  def_sig_return='#define SIGNAL_RETURN return 0'
 else
  do_sigtest void int

  if [ $stat = 0 -a -z "$warning" ]; then
   echo 'signal handler is void handler(int).'
   def_sig_type='typedef void MySignalHandler;'
   def_sig_arg='#define SIGNAL_ARG int _dummy'
   def_sig_arglist='#define SIGNAL_ARGLIST 0'
   def_sig_return='#define SIGNAL_RETURN return'
  else
   do_sigtest void void

   if [ $stat = 0 -a -z "$warning" ]; then
    echo 'signal handler is void handler(void).'
   else
    echo 'I could not find the type of signal handler. I hope void handler(void) works.'
   fi

   def_sig_type='typedef void MySignalHandler;'
   def_sig_arg='#define SIGNAL_ARG void'
   def_sig_arglist='#define SIGNAL_ARGLIST'
   def_sig_return='#define SIGNAL_RETURN return'
  fi
 fi
fi

# check for float.h

cat > _zmachdep.c << EOF
#include <float.h>
main()
{
  ;
}
EOF

if $cc $cflags -c _zmachdep.c > /dev/null 2>&1; then
 echo "You have float.h."
 def_have_float_h='#define HAVE_FLOAT_H'
else
 def_have_float_h='#undef HAVE_FLOAT_H 1'
fi

####### check for sys/select.h

cat > _zmachdep.c <<EOF
#include <sys/select.h>
main()
{ 
  ;
}
EOF

if $cc $cflags -c _zmachdep.c > /dev/null 2>&1; then
 echo "You have sys/select.h."
 def_sys_select_h='#define HAVE_SYS_SELECT_H'
else
 def_sys_select_h='#undef HAVE_SYS_SELECT_H'
fi

####### setpgrp(pid, pgrp) or setpgrp() ?

cat > _zmachdep.c << EOF
#include <unistd.h>
int main(){
 int pid;
 if((pid = fork()) == 0 )
   setpgrp();
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have setpgrp()."
 def_have_setpgrp='#define HAVE_SETPGRP'
else
 cat > _zmachdep.c << EOF
#include <unistd.h>
int main(){
 int pid;
 if((pid = fork()) == 0 )
   setpgrp( 0, 0 );
}
EOF

 if $cc $cflags -o _zmachdep _zmachdep.c $extlib > /dev/null 2>&1; then
  echo "You have setpgrp( pid, pgrp )."
  def_have_setpgrp='#define HAVE_SETPGRP'
  def_setpgrp='#define SETPGRP() setpgrp( 0, 0 )'
 else
  def_have_setpgrp=''
  def_setpgrp='#define SETPGRP() /* no setpgrp() */'
 fi
fi

###### va_copy() (in XPG6?)

cat > _zmachdep.c <<EOF
#include <stdarg.h>
#include <stdio.h>

main()
{
 va_list a, b;

 va_copy(a, b);
 return 0;
}
EOF

if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
 echo "You have va_copy()."
 def_have_va_copy='#define HAVE_VA_COPY'
 def_va_copy=
else
 echo "You do not have va_copy()."
 def_have_va_copy='#undef HAVE_VA_COPY'

cat > _zmachdep.c <<EOF
#include <stdarg.h>
#include <stdio.h>

main()
{
 va_list a, b;

 __va_copy(a,b);
 return 0;
}
EOF

 if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
  echo "You have __va_copy()."
  def_va_copy='#define va_copy(dest, src) __va_copy(dest, src)'
 else

cat > _zmachdep.c <<EOF
#include <stdarg.h>
#include <stdio.h>

main()
{
 va_list a, b;

 a = b;
 return 0;
}
EOF

  if $cc $cflags -o _zmachdep _zmachdep.c > /dev/null 2>&1; then
   echo "Your va_list is not an array."
   def_va_copy='#define va_copy(dest, src) ((dest) = (src))'
  else
   echo "Your va_list is an array."
   def_va_copy='#define va_copy(dest, src) (memcpy(&(dest), &(src), sizeof(va_list)))'
  fi
 fi
fi

###### IPv6 support check

cat > _zmachdep.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
main()
{
 if (socket(AF_INET6, SOCK_STREAM, 0) < 0)
   exit(1);
 else
   exit(0);
}
EOF

def_ipv6="#undef INET6"
v6_ss_family=""
v6lib=''

if $cc $cflags -o _zmachdep _zmachdep.c $extlib > /dev/null 2>&1; then
 if ./_zmachdep; then
  def_ipv6="#define INET6"
 else
  # even if build machine doesnt have IPv6 support, it may works
  # Required for package builds
  if [ "$use_ipv6" = "y" ]; then
   echo "Your build machine doesn't have IPv6 support, but it is enabled by config.param"
   def_ipv6="#define INET6"
   echo "use_ipv6=y" >> config.param
  fi
 fi

 case $sysname in #(
  *BSD|*bsd)
    cat > _zmachdep.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

struct addrinfo *hints, **res;

int main() {
    getaddrinfo("null", "null", hints, res);
}
EOF

    if $cc $cflags -o _zmachdep _zmachdep.c $extlib > /dev/null 2>&1; then
     echo "You have getaddrinfo() in libc."
    else
     for tlibdir in /usr/local/v6/lib /usr/local/lib /usr/lib; do
      if [ -r $tlibdir/libinet6.a ]; then
       if [ "$tlibdir" != "/usr/lib" ]; then
	v6lib="-L$tlibdir"
       fi

       v6lib="$v6lib -linet6"

       if $cc $cflags -o _zmachdep _zmachdep.c $extlib $v6lib > /dev/null 2>&1; then
	echo "You have getaddrinfo() in libinet6."
       fi

       break
      fi
     done

     if [ "X$v6lib" = "X" ]; then
      echo "You don't have getaddrinfo()."
      def_ipv6="#undef INET6"
     fi
  fi

  ;;
  CYGWIN*)
  def_ipv6="#undef INET6"
  ;;
 esac
fi

####### ss_family or __ss_family ?

if [ "$def_ipv6" = "#define INET6" ]; then
 cat > _zmachdep.c <<EOF
#include <sys/socket.h>
int main() {
    struct sockaddr_storage ss;
    int i = ss.ss_family;
}
EOF

 if $cc $cflags -o _zmachdep _zmachdep.c $extlib > /dev/null 2>&1; then
  echo "You have ss_family."
 else
  cat > _zmachdep.c <<EOF
#include <sys/socket.h>
int main() {
    struct sockaddr_storage ss;
    int i = ss.__ss_family;
}
EOF

  if $cc $cflags -o _zmachdep _zmachdep.c $extlib > /dev/null 2>&1; then
   echo "You have __ss_family."
   def_ipv6_ss_family="#define ss_family __ss_family"
  else
   echo "You don't have ss_family."
   def_def_ipv6="#undef INET6"
  fi
 fi
fi

if [ "$def_ipv6" = "#undef INET6" ]; then
 echo "You don't have IPv6 support."
else
 echo "You have IPv6 support."
fi

# libintl.so, libintl.a, and locale.h
if [ -n "$def_locale_dir" ]; then
 do_localetest
fi

def_use_w3mimg_x11="#undef USE_W3MIMG_X11"
def_use_w3mimg_fb="#undef USE_W3MIMG_FB"
def_use_imlib='#undef USE_IMLIB'
def_use_gdkpixbuf='#undef USE_GDKPIXBUF'
def_use_imlib2='#undef USE_IMLIB2'
imgx11cflags=''
imgfbcflags=''
imgx11ldflags=''
imgfbldflags=''
imgobjs='w3mimg/w3mimg.o'

if [ "$use_image" = y ]; then
  if [ "$use_w3mimg_x11" = y ]; then
      if find_gdkpixbuf; then
	  def_use_w3mimg_x11="#define USE_W3MIMG_X11"
	  def_use_gdkpixbuf='#define USE_GDKPIXBUF'
	  imgobjs="$imgobjs w3mimg/x11/x11_w3mimg.o"
	  imgx11cflags="`${GDKPIXBUF_CONFIG} --cflags`"
	  imgx11ldflags="`${GDKPIXBUF_CONFIG} --libs` -lgdk_pixbuf_xlib"
      elif find_imlib; then
	  def_use_w3mimg_x11="#define USE_W3MIMG_X11"
	  def_use_imlib='#define USE_IMLIB'
	  imgobjs="$imgobjs w3mimg/x11/x11_w3mimg.o"
	  imgx11cflags="`${IMLIB_CONFIG} --cflags`"
	  imgx11ldflags="`${IMLIB_CONFIG} --libs`"
      else
	  use_w3mimg_x11=n
	  echo "Disable X11 inline image"
      fi
  fi
  if [ "$use_w3mimg_fb" = y ]; then
      if find_gdkpixbuf; then
	  def_use_w3mimg_fb="#define USE_W3MIMG_FB"
	  imgobjs="$imgobjs w3mimg/fb/fb_w3mimg.o w3mimg/fb/fb.o w3mimg/fb/fb_img.o"
	  def_use_gdkpixbuf='#define USE_GDKPIXBUF'
	  imgfbcflags="`${GDKPIXBUF_CONFIG} --cflags`"
	  imgfbldflags="`${GDKPIXBUF_CONFIG} --libs`"
      elif find_imlib2; then
	  def_use_imlib2='#define USE_IMLIB2'
	  imgobjs="$imgobjs w3mimg/fb/fb_w3mimg.o w3mimg/fb/fb.o w3mimg/fb/fb_img.o"
	  def_use_imlib2='#define USE_IMLIB2'
	  imgfbcflags="`${IMLIB2_CONFIG} --cflags`"
	  imgfbldflags="`${IMLIB2_CONFIG} --libs`"
      else
	  use_w3mimg_fb=n
	  echo "Disable Linux framebuffer inline image"
      fi
  fi
  install_w3mimgdisplay='$(INSTALL_PROGRAM)'
  if [ "$use_w3mimg_fb" = y ]; then
      if [ "$w3mimgdisplay_setuid" = y ]; then
	  install_w3mimgdisplay='$(INSTALL) -o root -m 4755 -s'
      fi
  fi
fi

rm -f _zmachdep$extension _zmachdep.c _zmachdep.o

echo "------------ Configuration done ------------"

# set model name
case $dmodel in #(
 1) modelname=baby;; #(
 2) modelname=little;; #(
 3) modelname=mouse;; #(
 4) modelname=cookie;; #(
 5) modelname=monster;; #(
 6) modelname=custom;;
esac

cur_ver=`sed -n 's/^#define CURRENT_VERSION *"w3m\/\([^"]*\)".*$/\1/p' version.c`

for h_in in w3mhelp-*_*.html.in; do
 h=`expr "$h_in" : '\(.*\)\.in'`
 sed -e 's/@CURRENT_VERSION@/'$cur_ver'/' $h_in > $h
done

cat > extrvers.c << EOF
#include <stdio.h>
#include <string.h>
#include "version.c"
main()
{
  char *p = strchr(w3m_version,'/');
  if (p == NULL)
    printf("unknown\n");
  else
    printf("%s\n",p+1);
}
EOF

$cc $cflags $mb_cflag -o extrvers$extension extrvers.c  > /dev/null 2>&1
w3mversion=`./extrvers`
echo "Current w3m version is $w3mversion."
rm -f extrvers.c extrvers$extension

if [ -f fix-cookie.sh ]; then
 sh fix-cookie.sh -doit no -quiet yes
 echo
fi

echo "Making system wide browsecap"

if [ "X$ded" = X ]; then
 echo 'x-w3m-edit/*; $EDITOR %s ;needsterminal ;x-w3m-internal' >browsecap
elif expr "$ded" : '.*%s.*'; then
 echo "x-w3m-edit/*; $ded ;needsterminal ;x-w3m-internal" >browsecap
else
 echo "x-w3m-edit/*; $ded %s ;needsterminal ;x-w3m-internal" >browsecap
fi

echo 'x-w3m-edit/inputtext;;x-w3m-internal' >>browsecap

if [ "X$use_w3mmailer" = Xy ]; then
 echo "mailto/*; $libdir/w3mmail.cgi?%u ;x-w3m-cgioutput" >>browsecap
elif [ "X$dmail" = X ]; then
 echo 'mailto/*; $MAILER %s ;needsterminal' >>browsecap
elif expr "$dmail" : '.*%s.*'; then
 echo "mailto/*; $dmail ;needsterminal" >>browsecap
else
 echo "mailto/*; $dmail %s ;needsterminal" >>browsecap
fi

echo "Making TABMakefile"

if [ -x $mk_btri_dir/mk_btri ]; then
 btri_feature=`$mk_btri_dir/mk_btri -? </dev/null`

 if [ "0$btri_feature" -lt 1 ]; then
  echo "$mk_btri_dir/mk_btri does not enough ability we need,"
  echo "so we will use included newer version."
  mk_btri_cmd=./mk_btri
 else
  mk_btri_cmd=$mk_btri_dir/mk_btri
 fi
else
 mk_btri_cmd=./mk_btri
fi

cat >do_mk_btri.sh <<END_OF_DO_MK_BTRI
$dcpp "\$1" | sed -n "/^[ 	]*[\\"'%]/p" | $mk_btri_cmd
END_OF_DO_MK_BTRI

cat >TABMakefile <<END_OF_TABMAKEFILE1
do_mk_btri.sh: $mk_btri_cmd
	touch \$@

ce_tab.h: do_mk_btri.sh ce_tab.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh ce_tab.mk_btri.h > \$@-new && mv \$@-new \$@

media_tab.h: do_mk_btri.sh media_tab.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh media_tab.mk_btri.h > \$@-new && mv \$@-new \$@

headertab.h: do_mk_btri.sh headertab.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh headertab.mk_btri.h > \$@-new && mv \$@-new \$@

tagtable.h: do_mk_btri.sh tagtable.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh tagtable.mk_btri.h > \$@-new && mv \$@-new \$@

tagattrtab.h: do_mk_btri.sh tagattrtab.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh tagattrtab.mk_btri.h > \$@-new && mv \$@-new \$@

funcname.h: do_mk_btri.sh funcname.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh funcname.mk_btri.h > \$@-new && mv \$@-new \$@

schemetab.h: do_mk_btri.sh schemetab.mk_btri.h scheme.h
	rm -f \$@
	sh do_mk_btri.sh schemetab.mk_btri.h > \$@-new && mv \$@-new \$@

defaultguess.h: do_mk_btri.sh defaultguess.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh defaultguess.mk_btri.h > \$@-new && mv \$@-new \$@

http_auto_header.h: do_mk_btri.sh http_auto_header.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh http_auto_header.mk_btri.h > \$@-new && mv \$@-new \$@

http_version.h: do_mk_btri.sh http_version.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh http_version.mk_btri.h > \$@-new && mv \$@-new \$@

entity_tab.h: do_mk_btri.sh entity_tab.mk_btri.h entity.h
	rm -f \$@
	sh do_mk_btri.sh entity_tab.mk_btri.h > \$@-new && mv \$@-new \$@

rc_name.h: do_mk_btri.sh rc_name.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh rc_name.mk_btri.h > \$@-new && mv \$@-new \$@
END_OF_TABMAKEFILE1

if [ "X$lang" = XMANY ]; then
 cat >>TABMakefile <<END_OF_TABMAKEFILE_MORE1

rc_enc_name.h: do_mk_btri.sh rc_enc_name.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh rc_enc_name.mk_btri.h > \$@-new && mv \$@-new \$@

locale2mime.h: do_mk_btri.sh locale2mime.mk_btri.h
	rm -f \$@
	sh do_mk_btri.sh locale2mime.mk_btri.h > \$@-new && mv \$@-new \$@

rc.o: rc.h rc_name.h rc_enc_name.h locale2mime.h
END_OF_TABMAKEFILE_MORE1
else
 cat >>TABMakefile <<END_OF_TABMAKEFILE_MORE2

rc.o: rc.h rc_name.h
END_OF_TABMAKEFILE_MORE2
fi

echo "Extracting config.h"
cat > config.h << END_OF_CONFIG_H
/*
 * Configuration for w3m
 */

#ifndef _CONFIGURED_
#define _CONFIGURED_

/* User Configuration */

/* 
   If you define DICT, you can use dictionary look-up function
   in w3m. See README.dict for detail.
*/
$def_use_dict

/*
   If you want to load and save URL history.
 */
$def_use_history

/*
   USE_BG_COLOR enables w3m to set background color.
 */
$def_use_bg_color

/*
 * Do word fill
 */
$def_format_nice

/*
 * Enable id attribute
 */
$def_id_ext

/*
 * Save Current-buffer Information
 */
$def_use_bufinfo

/*
 * Support EGD (Entropy Gathering Daemon)
 */
$def_use_egd

/*
 * thin chars for menu frame
 */
$def_menu_thin_frame

/*
 */
$def_table_expand
$def_table_no_compact
$def_nowrap
$def_matrix

/**********************************************************/
#ifdef makefile_parameter

SUFFIX = $suffix
BIN_DIR = $bindir
AUXBIN_DIR = $auxbindir
LIB_DIR = $libdir
HELP_DIR = $helpdir
MAN_DIR = $mandir
MAN1_DIR = \$(MAN_DIR)/man1
MAN1_JA_DIR = \$(MAN_DIR)/ja/man1
ETC_DIR = $sysconfdir
RC_DIR = ~/.w3m$suffix
TMP_DIR = \$(RC_DIR)
HELP_FILE = $helpfile
SYS_LIBRARIES = $gpmlib $termlib $ssllib $v6lib $intllib
LOCAL_LIBRARIES = $extlib $mb_ldflag
CC = $cc
MYCFLAGS = $cflags $bsdinclude $sslinclude $mb_cflag
LDFLAGS = $ldflags
GCCFLAGS = $cflags -I./\$(srcdir)/include -DATOMIC_UNCOLLECTABLE -DNO_SIGNALS -DNO_EXECUTE_PERMISSION -DSILENT -DALL_INTERIOR_POINTERS
KEYBIND_SRC = $keymap_file.c
KEYBIND_OBJ = $keymap_file.o
EXT=$extension
MATHLIB=$mathlib
AUXBIN_TARGETS=$imgtarget
LIB_TARGETS=\$(BOOKMARKER) \$(HELPER)
IMGCFLAGS=$imgx11cflags $imgfbcflags
IMGLDFLAGS=$imgx11ldflags $imgfbldflags
IMGX11CFLAGS=$imgx11cflags
IMGFBCFLAGS=$imgfbcflags
IMGOBJS=$imgobjs
GC_CFLAGS=$gc_cflags
GCLIB=$gclib $pthreadlib
GCTARGET=$gctarget
RANLIB=$ranlib_cmd
MKDIR=$MKDIR
PERL=$perl
VERSION=$w3mversion
MODEL=$sysname.$platform-$modelname-$lang
INSTALL_W3MIMGDISPLAY=$install_w3mimgdisplay
LOCALE_DIR=$def_locale_dir
ALIEN_OBJS=$alien_objs
ROMAJI_FILTER=$def_romaji_filter
#else
$special_sys

#define DISPLAY_CODE '$display_code'
#define SYSTEM_CODE '$system_code'

#define JA 0
#define EN 1
#define MANY 2
$def_lang
$def_kanji_symbols
$use_alg
$use_locale_dir
$def_textdomain
$def_use_color
$def_use_ansi_color
$def_emacs_like_lineedit
$def_vi_prec_num
$def_use_mark
$def_use_mouse
$def_use_gpm
$def_use_sysmouse
#define USE_MENU /* $def_use_menu */
$def_use_cookie
$def_use_digest_auth
$def_use_ssl
$def_use_ssl_verify
$def_use_romaji
$def_ftppass_hostnamegen
$def_use_w3mmailer
$def_use_nntp
$def_use_gopher
$def_use_image
$def_use_w3mimg_x11
$def_use_w3mimg_fb
$def_w3mimgdisplay_setuid
$def_use_imlib
$def_use_gdkpixbuf
$def_use_imlib2
$def_use_help_cgi
$def_use_xface

#define DEF_EXT_BROWSER "$brz"

/* fallback viewer. mailcap override these configuration */
#define DEF_IMAGE_VIEWER	"$def_image_viewer"
#define DEF_AUDIO_PLAYER	"$def_audio_player"

/* for USE_ROMAJI */
#define DEF_ROMAJI_FILTER	"$def_romaji_filter"

#define AUXBIN_DIR   "$auxbindir"
#define LIB_DIR      "$libdir"
#define HELP_DIR     "$helpdir"
#define ETC_DIR	     "$sysconfdir"
#define HELP_FILE    "w3mhelp.html"
#define HELP_CGI     "w3mhelp"
#define W3MCONFIG    "w3mconfig"
#define W3MMESSAGES  "w3mmessages"
#define W3MMENU      "w3mmenu"
#define IMGDISPLAY   "w3mimgdisplay"
#define XFACE2XPM    "xface2xpm"

#define RC_DIR       "~/.w3m$suffix"
#define TMP_DIR      RC_DIR
#define BOOKMARK     "bookmark.html"
#define CONFIG_FILE  "config"
#define MESSAGES_FILE  "messages"
#define KEYMAP_FILE  "keymap"
#define MENU_FILE    "menu"
#define MOUSE_FILE   "mouse"
#define COOKIE_FILE  "cookie"
#define HISTORY_FILE "history"

#define PASSWD_FILE RC_DIR "/passwd"
#define PRE_FORM_FILE RC_DIR "/pre_form"
#define USER_MAILCAP RC_DIR "/mailcap"
#define SYS_MAILCAP  ETC_DIR "/mailcap"
#define GLOBAL_MAILCAP  "/etc/mailcap"
#define USER_MIMETYPES "~/.mime.types"
#define SYS_MIMETYPES "$MIME_TYPES"
#define USER_BROWSECAP RC_DIR "/browsecap"
#define SYS_BROWSECAP  ETC_DIR "/browsecap"
#define USER_LOCALE2MIME RC_DIR "/locale2mime"
#define SYS_LOCALE2MIME  ETC_DIR "/locale2mime"

#define DEF_SAVE_FILE "index.html"

$def_use_binstream
$def_term_if
$def_dir_if
$def_have_long_long
$def_have_strtoll
$def_have_strtoq
$def_have_atoll
$def_have_atoq
$def_have_strcasecmp
$def_have_strcasestr
$def_have_strchr
$def_have_strerror
$def_have_syserrlist
$def_have_bcopy
$def_have_waitpid
$def_have_wait3
$def_have_strftime
$def_have_getcwd
$def_have_getwd
$def_have_symlink
$def_have_readlink
$def_have_lstat
$def_have_setenv
$def_have_putenv
$def_have_srand48
$def_have_srandom
$def_have_getpassphrase
$def_have_chdir
$dcl_fclose
$dcl_pclose
$def_setjmp
$def_longjmp
$def_jmpbuf
$def_have_float_h
$def_have_sys_select_h

$def_sig_type
$def_sig_arg
$def_sig_arglist
$def_sig_return
$def_have_setpgrp
$def_setpgrp
$def_have_va_copy
$def_va_copy
/*
   If you want to use IPv6, define this symbol.
 */
$def_ipv6
$def_ipv6_ss_family
 
/** dont edit below **/
#ifndef HAVE_SRAND48
#ifdef HAVE_SRANDOM
#define srand48 srandom
#define lrand48 random
#else /* HAVE_SRANDOM */
#define USE_INCLUDED_SRAND48
#endif /* HAVE_SRANDOM */
#endif

#if defined( __CYGWIN32__ ) && !defined( __CYGWIN__ )
#define __CYGWIN__
#endif

#if defined( __CYGWIN__ ) || defined( __EMX__ )
#define SUPPORT_DOS_DRIVE_PREFIX 1
#endif

#if defined( __CYGWIN__ )
#define SUPPORT_NETBIOS_SHARE 1
#if LANG == JA || LANG == MANY
#define SUPPORT_WIN9X_CONSOLE_MBCS 1
#endif
#endif

#if defined(__DJGPP__)
#define DEFAULT_TERM	"dosansi"
#else
#define DEFAULT_TERM	0	/* XXX */
#endif

#if defined(__EMX__) /* use \$extension? */
#define GUNZIP_CMDNAME  "gzip"
#define BUNZIP2_CMDNAME "bzip2"
#define W3MBOOKMARK_CMDNAME	"w3mbookmark.exe"
#define W3MHELPERPANEL_CMDNAME	"w3mhelperpanel.exe"
#define DEV_NULL_PATH	"nul"
#define DEV_TTY_PATH	"con"
#define CGI_EXTENSION	".cmd"
#define USE_PATH_ENVVAR
#else
#define GUNZIP_CMDNAME  "gunzip"
#define BUNZIP2_CMDNAME "bunzip2"
#define W3MBOOKMARK_CMDNAME	"w3mbookmark"
#define W3MHELPERPANEL_CMDNAME	"w3mhelperpanel"
#define DEV_NULL_PATH	"/dev/null"
#define DEV_TTY_PATH	"/dev/tty"
#define CGI_EXTENSION	".cgi"
#endif

#define PATH_SEPARATOR	':'
#define GUNZIP_NAME  "gunzip"
#define BUNZIP2_NAME "bunzip2"

#endif /* makefile_parameter */
#endif /* _CONFIGURED_ */

END_OF_CONFIG_H

echo ''
echo 'config.h is created. See config.h for further configuration.'
echo ''

echo 'Configuration done. Just type "make".'
