#!/bin/sh
#
#   EnergyMech, IRC Bot software
#   Copyright (c) 1997-2018 proton
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

umask 077

compile=no
install=no
silentopt=no
try_libmusl=no

# default optimization goal, speed = -O2, size = -Os
optitype=size
cc_arch_flag=

set_feature_defaults() {
  ft_alias=$ft_default
  ft_botnet=$ft_default
  ft_bounce=$ft_default
  ft_chanban=$ft_default
  ft_ctcp=$ft_default
  ft_dccfile=$ft_default
  ft_debug=$ft_default
  ft_dynamode=$ft_default
  ft_dyncmd=$ft_default
  ft_greet=$ft_default
  ft_hostinfo=$ft_default
  ft_ircd_ext=$ft_default
  ft_md5=$ft_default
  ft_netcfg=$ft_default
  ft_newbie=$ft_default
  ft_note=$ft_default
  ft_notify=$ft_default
  ft_perl=$ft_default
  ft_python=$ft_default
  ft_rawdns=$ft_default
  ft_redirect=$ft_default
  ft_seen=$ft_default
  ft_session=$ft_default
  ft_sha=$ft_default
  ft_stats=$ft_default
  ft_suppress=$ft_default
  ft_tcl=$ft_default
  ft_telnet=$ft_default
  ft_toybox=$ft_default
  ft_trivia=$ft_default
  ft_uptime=$ft_default
  ft_urlcapture=$ft_default
  ft_web=$ft_default
  ft_wingate=$ft_default
}

for opt
do

  case "$opt" in
    -*=*)
      optarg=`echo $opt | sed 's/.*=//;'` ;;
    *)
      optarg= ;;
  esac

  yesno=
  case "$opt" in
    --with=* )		yesno=yes ;;
    --enable=* )	yesno=yes ;;
    --without=* )	yesno=no ;;
    --disable=* )	yesno=no ;;
  esac

  if [ "$yesno" = yes -o "$yesno" = no ]; then
    IFSBACKUP=$IFS
    IFS='=,'
    notfirst=
    for nn in $opt
    do
      if [ "$notfirst" ]; then
        case "$nn" in
          alias )	ft_alias=$yesno ;;
          botnet )	ft_botnet=$yesno ;;
          bounce )	ft_bounce=$yesno ;;
	  chanban )	ft_chanban=$yesno ;;
          ctcp )	ft_ctcp=$yesno ;;
          dccfile )	ft_dccfile=$yesno ;;
          debug )	ft_debug=$yesno ;;
          dynamode )	ft_dynamode=$yesno ;;
          dyncmd )	ft_dyncmd=$yesno ;;
          greet )	ft_greet=$yesno ;;
          hostinfo )	ft_hostinfo=$yesno ;;
          idwrap )	ft_idwrap=$yesno ;;
          ircd_ext )	ft_ircd_ext=$yesno ;;
	  libmusl )	try_libmusl=$yesno ;;
          md5 )		ft_md5=$yesno ;;
	  netcfg )	ft_netcfg=$yesno ;;
          newbie )	ft_newbie=$yesno ;;
          note )	ft_note=$yesno ;;
          notify )	ft_notify=$yesno ;;
          perl )	ft_perl=$yesno ;;
	  python )	ft_python=$yesno ;;
          rawdns )	ft_rawdns=$yesno ;;
          redirect )	ft_redirect=$yesno ;;
          seen )	ft_seen=$yesno ;;
          session )	ft_session=$yesno ;;
          sha )		ft_sha=$yesno ;;
          stats )	ft_stats=$yesno ;;
	  suppress )	ft_suppress=$yesno ;;
          tcl )		ft_tcl=$yesno ;;
          telnet )	ft_telnet=$yesno ;;
          toybox )	ft_toybox=$yesno ;;
          trivia )	ft_trivia=$yesno ;;
          uptime )	ft_uptime=$yesno ;;
          urlcapture )	ft_urlcapture=$yesno ;;
          web )		ft_web=$yesno ;;
          wingate )	ft_wingate=$yesno ;;
        esac
      else
        notfirst=yes
      fi
    done
    IFS=$IFSBACKUP
  fi

  case "$opt" in
    --enable-*=*)
      feature=`echo $opt | sed 's/^\-\-enable\-//; s/=.*$//'` ;;
    --enable-*)
      feature=`echo $opt | sed 's/^\-\-enable\-//;'` ;;
    --disable-*)
      feature=`echo $opt | sed 's/^\-\-disable\-//;'` ;;
    --with-*=*)
      feature=`echo $opt | sed 's/^\-\-with\-//; s/=.*$//'` ;;
    --with-*)
      feature=`echo $opt | sed 's/^\-\-with\-//;'` ;;
    --without-*)
      feature=`echo $opt | sed 's/^\-\-without\-//;'` ;;
    *)
      feature=___none___ ;;
  esac

  case "$feature" in
    alias | botnet | bounce | chanban | ctcp | dccfile | debug | dynamode | dyncmd | greet | hostinfo | idwrap | ircd_ext | libmusl | md5 \
    | netcfg | newbie | note | notify | perl | profiling | python | rawdns | redirect | seen | session | sha | stats | suppress | tcl \
    | telnet | toybox | trivia | uptime | urlcapture | web | wingate )
      case _"$optarg"_ in
        _yes_ | _no_ | __ )
          ;;
        *)
	  if [ "$feature" = libmusl ]; then
	    try_libmusl="$optarg"
	  else
	    echo "unknown argument for $feature":" $optarg"
	    echo "Usage: configure [options]"
	    echo "use \"$0 --help\" for help"
	    exit 1
	  fi
      esac
      ;;
    ___none___ )
      ;;
    *)
      echo "unknown feature: $feature"
      echo "Usage: configure [options]"
      echo "use \"$0 --help\" for help"
      exit 1
  esac

  case "$opt" in
    --with=* | --without=* | --enable=* | --disable=* ) ;;
    --enable-* | --with-*)
      case "$feature"_"$optarg" in
        alias_yes | alias_ )		ft_alias=yes ;;
        alias_no )			ft_alias=no ;;
        botnet_yes | botnet_ )		ft_botnet=yes ;;
        botnet_no )			ft_botnet=no ;;
        bounce_yes | bounce_ )		ft_bounce=yes ;;
        bounce_no )			ft_bounce=no ;;
	chanban_yes | chanban_ )	ft_chanban=yes ;;
	chanban_no )			ft_chanban=no ;;
        ctcp_yes | ctcp_ )		ft_ctcp=yes ;;
        ctcp_no )			ft_ctcp=no ;;
        dccfile_yes | dccfile_ )	ft_dccfile=yes ;;
        dccfile_no )			ft_dccfile=no ;;
        debug_yes | debug_ )		ft_debug=yes ;;
        debug_no )			ft_debug=no ;;
        dynamode_yes | dynamode_ )	ft_dynamode=yes ;;
        dynamode_no )			ft_dynamode=no ;;
        dyncmd_yes | dyncmd_ )		ft_dyncmd=yes ;;
        dyncmd_no )			ft_dyncmd=no ;;
        greet_yes | greet_ )		ft_greet=yes ;;
        greet_no )			ft_greet=no ;;
        hostinfo_yes | hostinfo_ )	ft_hostinfo=yes ;;
        hostinfo_no )			ft_hostinfo=no ;;
        idwrap_yes | idwrap_ )		ft_idwrap=yes ;;
        idwrap_no )			ft_idwrap=no ;;
        ircd_ext_yes | ircd_ext_ )	ft_ircd_ext=yes ;;
        ircd_ext_no )			ft_ircd_ext=no ;;
	libmusl_yes | libmusl_ )	try_libmusl=/usr/local/musl/bin/musl-gcc ;;
	libmusl_no )			try_libmusl=no ;;
        md5_yes | md5_ )		ft_md5=yes ;;
        md5_no )			ft_md5=no ;;
	netcfg_yes | netcfg_ )		ft_netcfg=yes ;;
	netcfg_no )			ft_netcfg=no ;;
        newbie_yes | newbie_ )		ft_newbie=yes ;;
        newbie_no )			ft_newbie=no ;;
        note_yes | note_ )		ft_note=yes ;;
        note_no )			ft_note=no ;;
        notify_yes | notify_ )		ft_notify=yes ;;
        notify_no )			ft_notify=no ;;
        perl_yes | perl_ )		ft_perl=yes ;;
        perl_no )			ft_perl=no ;;
	python_yes | python_ )		ft_python=yes ;;
	python_no )			ft_python=no ;;
        rawdns_yes | rawdns_ )		ft_rawdns=yes ;;
        rawdns_no )			ft_rawdns=no ;;
        redirect_yes | redirect_ )	ft_redirect=yes ;;
        redirect_no )			ft_redirect=no ;;
        seen_yes | seen_ )		ft_seen=yes ;;
        seen_no )			ft_seen=no ;;
        session_yes | session_ )	ft_session=yes ;;
        session_no )			ft_session=no ;;
        sha_yes | sha_ )		ft_sha=yes ;;
        sha_no )			ft_sha=no ;;
        stats_yes | stats_ )		ft_stats=yes ;;
        stats_no )			ft_stats=no ;;
	suppress_yes | suppress_ )	ft_suppress=yes ;;
	suppress_no )			ft_suppress=no ;;
        tcl_yes | tcl_ )		ft_tcl=yes ;;
        tcl_no )			ft_tcl=no ;;
        telnet_yes | telnet_ )		ft_telnet=yes ;;
        telnet_no )			ft_telnet=no ;;
        toybox_yes | toybox_ )		ft_toybox=yes ;;
        toybox_no )			ft_toybox=no ;;
        trivia_yes | trivia_ )		ft_trivia=yes ;;
        trivia_no )			ft_trivia=no ;;
        uptime_yes | uptime_ )		ft_uptime=yes ;;
        uptime_no )			ft_uptime=no ;;
        urlcapture_yes | urlcapture_ )	ft_urlcapture=yes ;;
        urlcapture_no )			ft_urlcapture=no ;;
        web_yes | web_ )		ft_web=yes ;;
        web_no )			ft_web=no ;;
        wingate_yes | wingate_ )	ft_wingate=yes ;;
        wingate_no )			ft_wingate=no ;;
        profiling_yes | profiling_ )	ft_prof=yes ;;
        profiling_no )			ft_prof=no ;;
      esac
    ;;
    --disable-* | --without-*)
      case "$feature" in
        alias )        ft_alias=no ;;
        botnet )       ft_botnet=no ;;
        bounce )       ft_bounce=no ;;
	chanban )      ft_chanban=no ;;
        ctcp )         ft_ctcp=no ;;
        dccfile )      ft_dccfile=no ;;
        debug )        ft_debug=no ;;
        dynamode )     ft_dynamode=no ;;
        dyncmd )       ft_dyncmd=no ;;
        greet )        ft_greet=no ;;
        hostinfo )     ft_hostinfo=no ;;
        idwrap )       ft_idwrap=no ;;
        ircd_ext )     ft_ircd_ext=no ;;
	libmusl )	try_libmusl=no ;;
        md5 )          ft_md5=no ;;
	netcfg )	ft_netcfg=no ;;
        newbie )       ft_newbie=no ;;
        note )         ft_note=no ;;
        notify )       ft_notify=no ;;
        perl )         ft_perl=no ;;
        profiling )    ft_prof=no ;;
	python )       ft_python=no ;;
        rawdns )       ft_rawdns=no ;;
        redirect )     ft_redirect=no ;;
        seen )         ft_seen=no ;;
        session )      ft_session=no ;;
        sha )          ft_sha=no ;;
        stats )        ft_stats=no ;;
	suppress )     ft_suppress=no ;;
        tcl )          ft_tcl=no ;;
        telnet )       ft_telnet=no ;;
        toybox )       ft_toybox=no ;;
        trivia )       ft_trivia=no ;;
        uptime )       ft_uptime=no ;;
        urlcapture )   ft_urlcapture=no ;;
        web )          ft_web=no ;;
        wingate )      ft_wingate=no ;;
      esac
    ;;
    --compile)		compile=yes ;;
    --install)		install=yes ;;
    --silence=options)	silentopt=yes ;;
    --md5=internal)	ft_md5=internal ;;
    --sha=internal)	ft_sha=internal ;;
    --use-cpuflags)	cc_arch_opt=yes ;;
    --no-cpuflags)	cc_arch_opt=no ;;
    --use-optimize)	cc_optimize_opt=yes ;;
    --no-optimize)	cc_optimize_opt=no ;;
    --optimize=speed)	optitype=speed ;;
    --optimize=size)	optitype=size ;;
    --use-gnudebug)	cc_g_opt=yes ;;
    --no-gnudebug)	cc_g_opt=no ;;
    --use-warnflag)	cc_wall_opt=yes ;;
    --no-warnflag)	cc_wall_opt=no ;;
    --use-pipeflag)	cc_pipe_opt=yes ;;
    --no-pipeflag)	cc_pipe_opt=no ;;
    --use-ofp)		cc_ofp_opt=yes ;;
    --no-ofp)		cc_ofp_opt=no ;;
    --default-yes)	ft_default=yes set_feature_defaults ;;
    --default-no)	ft_default=no set_feature_defaults ;;
    --help | -h)
        cat <<__EOT__
Usage: configure [options]
Configuration:
  --help                   print this message
Features and packages:
  --disable-FEATURE		do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]	include FEATURE [ARG=yes]
  --with-FEATURE[=ARG]		include FEATURE [ARG=yes]
  --without-FEATURE		do not include FEATURE (same as --with-FEATURE=no)
  --default-yes			Default answer for all feature selections is Yes
  --default-no			Default answer for all feature selections is No
  --md5=internal		Use internal routines for MD5 password hashes instead of system libraries
  --sha=internal		Use internal routines for SHA password hashes instead of system libraries
  --use-ofp			Try to use compiler flag -fomit-frame-pointer
  --no-ofp			Do not use compiler flag -fomit-frame-pointer
  --with-alias			ALIAS support
  --with-botnet			Botnet support
  --with-bounce
  --with-chanban
  --with-ctcp
  --with-dccfile
  --with-debug			Debug support
  --with-dynamode
  --with-dyncmd			Dynamic command levels support
  --with-greet
  --with-hostinfo		Support for code that reveals/displays/shares information about the host that the bot is running on
  --with-ircd_ext
  --with-libmusl[=/PATH/TO/musl-gcc]
				Try to use libmusl instead of system default
  --with-md5			Support for hashing passwords with the MD5 hashing algorithm
  --with-netcfg			Support for loading config blocks from the internet/other bots
  --with-newbie			Newbie support for extra sanity checks and error messages
  --with-note
  --with-notify
  --with-perl			Perl scripting support
  --with-profiling		Profiling (gcc+gprof)
  --with-python			Python scripting support
  --with-rawdns			Asynchronous DNS lookups
  --with-redirect		Support fo sending command output to other channels/nicks/files
  --with-seen			SEEN support
  --with-session		Session support
  --with-sha			Support for hashing passwords with the SHA (SHA-512)
  --with-stats
  --with-suppress		Command duplication suppression
  --with-tcl			Tcl scripting support
  --with-telnet			Telnet support
  --with-toybox			An assortment of fun commands
  --with-trivia			A trivia game
  --with-uptime			Include code that sends uptime reports to the IRC bot uptime contest server (https://www.eggheads.org/irc/uptime_stats)
  --with-urlcapture		URL capture support
  --with-web			Support for EnergyMech serving documents via the HTTP protocol
  --with-wingate		Wingate support
__EOT__
      exit 0
      ;;
    CC=*)
      CC=`echo $opt | sed 's/CC=//;'`
      ;;
    CFLAGS=*)
      CFLAGS=`echo $opt | sed 's/CFLAGS=//;'`
     echo "cflags is $CFLAGS"
      ;;
    *)
      echo "unknown option: $opt"
      echo "Usage: configure [options]"
      echo "use \"$0 --help\" for help"
      exit 1
  esac
done

if (echo "testing\c"; echo 1,2,3) | grep c > /dev/null; then
  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

echo $ac_n "checking system type ... "$ac_c

cf_ERR=yes
UNAME=`config/which uname`
test -x $UNAME && cf_ERR=

if test -z "$cf_ERR" && cf_ERR=yes && $UNAME -s 1> /dev/null 2> /dev/null; then
  cf_SYS=`$UNAME -s`
  cf_ERR=
fi

if test -z "$cf_ERR" && cf_ERR=yes && $UNAME -m 1> /dev/null 2> /dev/null; then
  cf_MACH=`$UNAME -m`
  cf_ERR=
fi

case _"$cf_ERR"_ in
  _yes_ )
    echo $ac_t unknown
    echo "Unable to determine system type."
    echo "Dont know how to compile the EnergyMech."
    echo "exiting ..."
    exit 1
    ;;
esac

if [ "$cf_SYS" = AIX ]; then
  cf_MACH=
  cf_SYSMACH="$cf_SYS"
else
  cf_SYSMACH="$cf_SYS"'-'"$cf_MACH"
fi

echo $ac_t "$cf_SYSMACH"

CCshort=
has_musl=no
#
#  if directed to use libmusl instead of (g)libc ...
#
if [ ! "$try_libmusl" = no ]; then
  echo $ac_n "checking for libmusl gcc wrapper ... "$ac_c
  mgcc=`config/which musl-gcc`
  if [ -x "$mgcc" ]; then
    CC="$mgcc"
  fi
  if [ -x "$try_musl" ]; then
    CC="$try_musl"
  fi
  if [ -x "$try_musl/musl-gcc" ]; then
    CC="$try_musl/musl-gcc"
  fi
  if [ -x "$try_musl/bin/musl-gcc" ]; then
    CC="$try_musl/bin/musl-gcc"
  fi
  if [ -x /opt/musl/bin/musl-gcc ]; then
    CC=/opt/musl/bin/musl-gcc
  fi
  CCshort=$CC
fi

#
#  a C compiler is good to have
#
if [ -z "$CCshort" ]; then
  echo $ac_n "checking for C compiler ... "$ac_c
  if [ "$CC" ]; then
    CCshort="$CC"
    CC=`config/which $CC`
  fi
  if [ ! -x "$CC" ]; then
    CCshort=gcc
    CC=`config/which gcc`
  fi
  if [ ! -x "$CC" ]; then
    CCshort=cc
    CC=`config/which cc`
    if [ ! -x "$CC" ]; then
      echo $ac_t "not found"
      echo "A working C compiler is needed to compile the EnergyMech"
      echo "exiting ..."
      exit 1
    fi
  fi
fi
CClong="$CC"
CC="$CCshort"
echo $ac_t "$CC"

#
# set up things for test compiling
#
TESTO=./test$$.o
TESTP=./test$$
rm -f $TESTO $TESTP

#
#  does the C compile work?
#
TESTC=config/cc.c
echo $ac_n "checking whether $CC works or not ... "$ac_c
$CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
WORK=no
if [ -x "$TESTP" ]; then
  res=_`$TESTP`_
  WORK=yes
  case $res in
    _yes_ )
      ;;
    _gnucc_ )
      cf_GNUCC=yes
      ;;
    _gnucc95_ )
      cf_GNUCC=95
      ;;
    _gnucc33_ )
      cf_GNUCC=33
      ;;
  esac
fi

rm -f $TESTP

if [ "$WORK" = yes ]; then
  echo $ac_t yes
  compiler=$CC
else
  echo $ac_t no
  echo ''
  echo "A working C compiler is needed to compile the EnergyMech"
  echo "exiting ..."
  echo ''
  exit 1
fi

#
#  compiler flags
#
echo $ac_n "checking C compiler flags ... "$ac_c

#
#  user defined CFLAGS
#
cf_cflags=
if [ -n "$CFLAGS" ]; then
  if $CC $CFLAGS -c -o $TESTO $TESTC 1> /dev/null 2> /dev/null && test -r $TESTO; then
    cf_cflags="$CFLAGS"
    echo $ac_n "$CFLAGS "$ac_c
  fi
fi

#
#  -g
#
if [ -z "$cf_cflags" -a -z "$cc_g_opt" ]; then
  cc_g_opt=no
  if $CC -g -c -o $TESTO $TESTC 1> /dev/null 2> /dev/null && test -r $TESTO; then
    cc_g_opt=yes
    cc_g_flag=-g
    echo $ac_n "-g "$ac_c
  fi
fi

#
# GNU GCC
#
if [ -z "$cf_cflags" -a -n "$cf_GNUCC" ]; then
  #
  #  -Wall -Wshadow
  #
  if [ -r .use_warn -o -n "$cc_wall_opt" ]; then
    if [ -z "$cc_wall_opt" -o -z "$cc_wshadow_opt" ]; then
      if $CC -Wall -Wshadow -S -o - -xc /dev/null 1> /dev/null 2> /dev/null; then
        cc_wall_flag="-Wall"
        cc_wshadow_flag="-Wshadow"
        echo $ac_n "-Wall -Wshadow "$ac_c
      fi
    fi
  fi
  if [ "$cc_wall_flag" = -Wall -a "$cf_GNUCC" = 33 ]; then
    cc_fnostrictalias="-fno-strict-aliasing"
    echo $ac_n "-fno-strict-aliasing "$ac_c
  fi
  #
  #  -mtune=i386/i486/i586/i686/core2
  #
  test -z "$cc_arch_opt" && cc_arch_opt=yes
  if [ "$cc_arch_opt" = yes ]; then
    case _"$cf_GNUCC"_"$cf_MACH"_ in
      _95_i486_ | _33_i486_ )
        if $CC -march=i486 -S -o - -xc /dev/null 1> /dev/null 2> /dev/null; then
          cc_arch_flag="-march=i486"
        fi
        ;;
      _95_i[56789]86_ | _33_i[56789]86_ )
        if $CC -march=i586 -S -o - -xc /dev/null 1> /dev/null 2> /dev/null; then
          cc_arch_flag="-march=i586"
        fi
        ;;
      _33_x86_64_ )
        if $CC -march=native -mtune=native -S -o - -xc /dev/null 1> /dev/null 2> /dev/null; then
          cc_arch_flag="-march=native -mtune=native"
	fi
    esac
    echo $ac_n "$cc_arch_flag "$ac_c
  fi
  #
  #  -fomit-frame-pointer
  #
  if [ -z "$cc_ofp_opt" -o "$cc_ofp_opt" = yes ]; then
    if $CC -fomit-frame-pointer -c -o $TESTO $TESTC 1> /dev/null 2> /dev/null && test -r $TESTO; then
      cc_ofp_flag=-fomit-frame-pointer
      echo $ac_n "-fomit-frame-pointer "$ac_c
    fi
  fi
fi

if [ -z "$cf_cflags" -a -z "$cc_pipe_opt" ]; then
  if $CC -pipe -c -o $TESTO $TESTC 1> /dev/null 2> /dev/null && test -r $TESTO; then
    cc_pipe_flag="-pipe"
    echo $ac_n "-pipe "$ac_c
  fi
fi

oflag="-O2"
if [ "$optitype" = size ]; then
  oflag="-Os"
fi

if [ -z "$cf_cflags" -a -z "$cc_optimize_opt" ]; then
  if $CC $oflag -o $TESTO $TESTC 1> /dev/null 2> /dev/null; then
    cc_optimize_flag="$oflag"
    echo $ac_n "$oflag "$ac_c
  fi
  if [ -z "$cc_optimize_flag" ]; then
    if $CC -O -o $TESTO $TESTC 1> /dev/null 2> /dev/null; then
      cc_optimize_flag="-O"
      echo $ac_n "-O "$ac_c
    fi
  fi
fi

echo $ac_t ""

if [ "$ft_prof" = yes ]; then
  echo $ac_n "checking profiling support ... "$ac_c
  if $CC -pg -o $TESTO $TESTC 1> /dev/null 2> /dev/null; then
    cc_ofp_flag=
    cc_pg_flag="-pg"
    cc_pg_define="-D__profiling__"
    libpgdl=
    echo $ac_t yes
  else
    if $CC -pg -o $TESTO $TESTC -ldl 1> /dev/null 2> /dev/null; then
      cc_ofp_flag=
      cc_pg_flag="-pg"
      cc_pg_define="-D__profiling__"
      libpgdl="-ldl"
      echo $ac_t "-ldl"
    else
      echo $ac_t no
    fi
  fi
fi

rm -f $TESTO $TESTP

#
#  32bit? 64bit?
#

TESTC=config/ptr_size.c

if [ -z "$ptr_size" ]; then
  ptr_size=unknown
  echo $ac_n "checking pointer size ... "$ac_c
  $CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
  if [ -x $TESTP ]; then
    ptr_size=`$TESTP`
  fi
  echo $ac_t "$ptr_size"
fi

case _"$ptr_size"_ in
  _8_ )
    PTSIZE_DEFINE32='#undef PTSIZE_32BIT'
    PTSIZE_DEFINE64='#define PTSIZE_64BIT'
    ;;
  * )
    PTSIZE_DEFINE32='#define PTSIZE_32BIT'
    PTSIZE_DEFINE64='#undef PTSIZE_64BIT'
    ;;
esac

rm -f $TESTP

#
#  big/little endian
#

TESTC=config/endian.c

if [ -z "$endian" ]; then
  endian=unknown
  echo $ac_n "checking endianness ... "$ac_c
  $CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
  if [ -x $TESTP ]; then
    endian=`$TESTP`
    if [ "$endian" = 1 ]; then
      endian="little"
    else
      endian="big"
    fi
  fi
  echo $ac_t "$endian endian"
fi

case "$endian" in
  little )
    LIT_END="#define LITTLE_ENDIAN"
    BIG_END="#undef BIG_ENDIAN"
    ;;
  * )
    LIT_END="#undef LITTLE_ENDIAN"
    BIG_END="#define BIG_ENDIAN"
    ;;
esac

rm -f $TESTP

#
#  Some processors like Sparc, will cause a bus error (SIGBUS) if you try to access unaligned memory
#

has_unaligned=no
UNALIGNED_MEM='#undef UNALIGNED_MEM'
TESTC=config/unaligned.c

echo $ac_n "checking if cpu can access unaligned memory ... "$ac_c

$CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
if [ "`$TESTP`" = yes ]; then
  has_unaligned=yes
  UNALIGNED_MEM='#define UNALIGNED_MEM'
fi
echo $ac_t "$has_unaligned"

rm -f $TESTP

#
#  is there a sendfile() system call?
#

has_sendfile=no
DEF_SENDFILE='#undef HAS_SENDFILE'
TESTC=config/sendfile.c

echo $ac_n "checking if system has sendfile() ... "$ac_c

$CC -c $TESTC -o $TESTO 1> /dev/null 2> /dev/null
if [ -r $TESTO ]; then
  has_sendfile=yes
  DEF_SENDFILE='#define HAS_SENDFILE'
fi
echo $ac_t "$has_sendfile"

rm -f $TESTO

#
#  where is inet_addr() ?
#

has_inet_addr=no
TESTC=config/inet_addr.c

echo $ac_n "checking for inet_addr() ... "$ac_c

$CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
if [ -x $TESTP ]; then
  has_inet_addr=yes
else
  $CC -o $TESTP $TESTC -lnsl 1> /dev/null 2> /dev/null
  if [ -x $TESTP ]; then
    has_inet_addr=-lnsl
    libnsl=-lnsl
  fi
fi
echo $ac_t "$has_inet_addr"
rm -f $TESTP

#
#  where is inet_aton() ?
#

has_inet_aton=no
TESTC=config/inet_aton.c

echo $ac_n "checking for inet_aton() ... "$ac_c

$CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
if [ -x $TESTP ]; then
  has_inet_aton=yes
else
  $CC -o $TESTP $TESTC -lresolv 1> /dev/null 2> /dev/null
  if [ -x $TESTP ]; then
    has_inet_aton=-lresolv
    libresolv=-lresolv
  fi
fi
echo $ac_t "$has_inet_aton"
rm -f $TESTP

#
#  where is socket() ?
#

has_socket=no
TESTC=config/socket.c

echo $ac_n "checking for socket() ... "$ac_c

$CC -o $TESTP $TESTC 1> /dev/null 2> /dev/null
if [ -x $TESTP ]; then
  has_socket=yes
else
  $CC -o $TESTP $TESTC -lsocket 1> /dev/null 2> /dev/null
  if [ -x $TESTP ]; then
    has_socket=-lsocket
    libsocket=-lsocket
  fi
fi
echo $ac_t "$has_socket"
rm -f $TESTP

#
#  check for SHA capabilities in libc/glibc/libcrypt
#
has_sha=no
has_md5=no
if [ ! "$ft_sha" = no ]; then
  TESTC=config/pw.c
  echo $ac_n "checking for SHA in crypt() ... "$ac_c
  sha_internal=
  crypt_func='-DCRYPT_FUNC=crypt'
  CRYPT_FUNCTION='#define CRYPT_FUNC crypt'
  if [ "$ft_sha" = internal ]; then
    sha_internal=config/sha_internal.c
    crypt_func='-DCRYPT_FUNC=sha_crypt'
    CRYPT_FUNCTION='#define CRYPT_FUNC sha_crypt'
  fi
  $CC -o $TESTP $TESTC $sha_internal $crypt_func 1> /dev/null 2> /dev/null
  if [ ! -x $TESTP ]; then
    libcrypt=-lcrypt
    has_sha=yes
    $CC -o $TESTP $TESTC $crypt_func $libcrypt 1> /dev/null 2> /dev/null
  fi
  if [ ! -x $TESTP ]; then
    libcrypt=/usr/lib/libcrypt.so
    has_sha=yes
    $CC -o $TESTP $TESTC $crypt_func $libcrypt 1> /dev/null 2> /dev/null
  fi
  if [ ! -x $TESTP ]; then
    has_sha=no
    libcrypt=
  fi
  if [ -x $TESTP ]; then
    pwhash=`$TESTP`
    case "$pwhash" in
      MD5 ) has_md5=yes ;;
      SHAMD5 )
        has_md5=yes
        has_sha=yes
      ;;
    esac
  fi
  echo $ac_t "$has_sha"
  [ "$has_sha" = yes -a "$sha_internal" ] && has_sha=internal
  rm -f $TESTP
fi

#
#  check for MD5 capabilities in libc/glibc/libcrypt
#
if [ "$has_md5" = yes ]; then
  echo "checking for MD5 in crypt() ... (cached) yes"
elif [ "$ft_md5" = no ]; then
  has_md5=notest
elif [ ! "$ft_md5" = no ]; then
  TESTC=config/pw.c
  echo $ac_n "checking for MD5 in crypt() ... "$ac_c
  md5_internal=
  crypt_func='-DCRYPT_FUNC=crypt'
  CRYPT_FUNCTION='#define CRYPT_FUNC crypt'
  if [ "$ft_md5" = internal ]; then
    md5_internal=config/md5_internal.c
    crypt_func='-DCRYPT_FUNC=md5_crypt'
    CRYPT_FUNCTION='#define CRYPT_FUNC md5_crypt'
  fi
  $CC -o $TESTP $TESTC $md5_internal $crypt_func 1> /dev/null 2> /dev/null
  if [ ! -x $TESTP ]; then
    libcrypt=-lcrypt
    has_md5=yes
    $CC -o $TESTP $TESTC $crypt_func $libcrypt 1> /dev/null 2> /dev/null
  fi
  if [ ! -x $TESTP ]; then
    libcrypt=/usr/lib/libcrypt.so
    has_md5=yes
    $CC -o $TESTP $TESTC $crypt_func $libcrypt 1> /dev/null 2> /dev/null
  fi
  if [ ! -x $TESTP ]; then
    has_md5=no
    libcrypt=
  fi
  if [ -x $TESTP ]; then
    pwhash=`$TESTP`
    case "$pwhash" in
      MD5 ) has_md5=yes ;;
    esac
  fi
  echo $ac_t "$has_md5"
  [ "$has_md5" = yes -a "$md5_internal" ] && has_md5=internal
  rm -f $TESTP
fi

#
#  check for perl scripting support
#
has_perl=no
perlinclude=
libperl=
if [ "$ft_perl" = no ]; then
  has_perl=notest
elif [ ! "$ft_perl" = no ]; then
  echo $ac_n "checking for perl ... "$ac_c
  pinc1=`perl -MConfig -e 'print $Config{archlib}'`
  if [ -r $pinc1/CORE/perl.h -a -r $pinc1/CORE/EXTERN.h -a -r $pinc1/CORE/XSUB.h ]; then
    perlinclude=$pinc1/CORE
    TESTC=config/perl.c
    if $CC -o $TESTP $TESTC -I$perlinclude -L$perlinclude -lperl 1> /dev/null 2> /dev/null; then
      has_perl=yes
      libperl=-lperl
      I_PERL=-I$perlinclude
      L_PERL=-L$perlinclude
    fi
    rm -f $TESTP
  fi
  echo $ac_t "$has_perl"
fi

#
#  check for tcl
#
has_tcl=no
if [ "$ft_tcl" = no ]; then
  has_tcl=notest
elif [ ! "$ft_tcl" = no ]; then
  echo $ac_n "checking for tcl ... "$ac_c
  TESTC=config/tcl.c
  tclconfig=
  if [ -f /usr/lib/tclConfig.sh ]; then
    tclconfig=tclconfig
    . /usr/lib/tclConfig.sh
  fi
  libtcl=
  inctcl=
  for tclver in $tclconfig empty 8.9 8.8 8.7 8.6 8.5 8.4 8.3 8.2 8.1 8.0
  do
    if [ -z "$libtcl" ]; then
      case "$tclver" in
        "tclconfig")
          TCLLIB=$TCL_LIB_FLAG
          TCLINCLUDE=$TCL_INCLUDE_SPEC
          tclver=$TCL_VERSION
          ;;
        "empty")
          TCLLIB=-ltcl
          TCLINCLUDE=
          ;;
        *)
          TCLLIB=-ltcl$tclver
          TCLINCLUDE=
          ;;
      esac
      if $CC -o $TESTP $TESTC $TCLLIB $TCLINCLUDE 1> /dev/null 2> /dev/null; then
        libtcl=$TCLLIB
        inctcl=$TCLINCLUDE
        has_tcl=$TCLLIB
        test "$tclver" = "empty" && echo $ac_t "yes" || echo $ac_t "yes (version $tclver)"
      fi
    fi
  done
  if [ -z "$libtcl" ]; then
    has_tcl=no
    echo $ac_t "no"
  fi
  rm -f $TESTP
fi

#
#  check for python
#
has_python=no
if [ "$ft_python" = no ]; then
  has_python=notest
elif [ ! "$ft_python" = no ]; then
  echo $ac_n "checking for python ... "$ac_c
  TESTC=config/python.c
  libpython=
  incpython=
  for pyver in 2.9 2.8 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0
  do
    if [ -z "$libpython" ]; then
      PYINCLUDE=python$pyver/Python.h
      PYLIB=-lpython$pyver
      if $CC -o $TESTP $TESTC -include $PYINCLUDE $PYLIB 1> /dev/null 2> /dev/null; then
        libpython=$PYLIB
        incpython="-include $PYINCLUDE"
        has_python=yes
        echo $ac_t "yes (version $pyver)"
      fi
    fi
  done
  if [ -z "$libpython" ]; then
    has_python=no
    echo $ac_t "no"
  fi
  rm -f $TESTP
fi

#
#  can we use our custom little ld script?
#
ldscript=
has_ldscript=no
TESTC=config/ldtest.c

echo $ac_n "checking for friendly ld ... "$ac_c

$CC -o $TESTP $TESTC -Wl,-T,src/ld/elf64-x86-64 1> /dev/null 2> /dev/null
if [ -x $TESTP ]; then
  if [ `$TESTP` = "yes" ]; then
    ldscript='-Wl,-T,ld/elf64-x86-64'
    has_ldscript=yes
  fi
fi
echo $ac_t "$has_ldscript"
rm -f $TESTP

#
#  idwrap
#
has_idwrap=no
def_idwrap='#undef IDWRAP'
IDWRAP_PATH='/* nothing */'

if [ -r .use_idwrap -o "$ft_idwrap" = yes ]; then
  echo $ac_n "checking for idwrap path ... "$ac_c
  if test -z "$idwrappath"; then
    idwrappath="/tmp/.ident/"
  fi
  if test -d $idwrappath; then
    IDWRAP_PATH='"'"$idwrappath"'"'
    has_idwrap="$IDWRAP_PATH"
    def_idwrap='#define IDWRAP'
  fi
  echo $ac_t "$has_idwrap"
fi

#
#
#
echo $ac_n "checking for objcopy ... "$ac_c
OBJCOPY=`config/which objcopy`
echo $ac_t "$OBJCOPY"
PROGSIZE=
sizecomment='#'
if [ -r .use_size ]; then
  echo $ac_n "checking for size ... "$ac_c
  PROGSIZE=`config/which size`
  echo $ac_t "$PROGSIZE"
  [ ! "$PROGSIZE" = "not found" ] && sizecomment=
fi

objcomment=
[ "$OBJCOPY" = "not found" ] && objcomment='#'

MD5_C=
MD5_O=
if [ "$ft_md5" = internal ]; then
  MD5_C=lib/md5.c
  MD5_O=lib/md5.o
fi

SHA_C=
SHA_O=
if [ "$ft_sha" = internal ]; then
  SHA_C=lib/sha1.c
  SHA_O=lib/sha.o
fi

out=echo
if [ "$silentopt" = yes ]; then
  out=/bin/true
fi

#
#   is this a git environment
#
def_git='#undef HAVE_GIT'
echo $ac_n "checking for git ... "$ac_c
GITEXE=`config/which git`
if [ -x $GITEXE -a -r .git ]; then
	def_git='#define HAVE_GIT'
fi
echo $ac_t "$GITEXE"

#
#  multiple cpus/cores for multiple parallell compiler jobs (make -j#)
#
cores=1
if [ -r /proc/cpuinfo ]; then
  echo $ac_n "checking for multiple cpus or multiple cores ... "$ac_c
  cores=`cat /proc/cpuinfo | grep '^processor' | wc -l`
  echo $ac_t "$cores cpus/cores"
fi

makejobs=
if [ ! "$cores" = 1 ]; then
  makejobs="-j$cores"
fi

echo
$out "Do you want ..."
$out

def_alias='#undef ALIAS'
unset ans
$out $ac_n "[STABLE] Alias support? ............................. [Y/n] "$ac_c
test "$ft_alias" && $out "$ft_alias" && ans=$ft_alias
test -z "$ft_alias" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_alias='#define ALIAS'

def_rawdns='#undef RAWDNS'
unset ans
$out $ac_n "[ BETA ] Async DNS support? ......................... [y/N] "$ac_c
test "$ft_rawdns" && $out "$ft_rawdns" && ans=$ft_rawdns
test -z "$ft_rawdns" && read ans
test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_rawdns='#define RAWDNS'

def_botnet='#undef BOTNET'
unset ans
$out $ac_n "[STABLE] Botnet support? ............................ [Y/n] "$ac_c
test "$ft_botnet" && $out "$ft_botnet" && ans=$ft_botnet
test -z "$ft_botnet" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_botnet='#define BOTNET'

def_chanban='#undef CHANBAN'
unset ans
$out $ac_n "[STABLE] Channel ban support? ....................... [Y/n] "$ac_c
test "$ft_chanban" && $out "$ft_chanban" && ans=$ft_chanban
test -z "$ft_chanban" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_chanban='#define CHANBAN'

def_suppress='#undef SUPPRESS'
unset ans
$out $ac_n "[ ALPHA] Command duplication suppression? ........... [Y/n] "$ac_c
test "$ft_suppress" && $out "$ft_suppress" && ans=$ft_suppress
test -z "$ft_suppress" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_suppress='#define SUPPRESS'

def_redirect='#undef REDIRECT'
unset ans
$out $ac_n "[STABLE] Command output redirect? ................... [Y/n] "$ac_c
test "$ft_redirect" && $out "$ft_redirect" && ans=$ft_redirect
test -z "$ft_redirect" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_redirect='#define REDIRECT'

def_ctcp='#undef CTCP'
unset ans
$out $ac_n "[STABLE] CTCP? ...................................... [Y/n] "$ac_c
test "$ft_ctcp" && $out "$ft_ctcp" && ans=$ft_ctcp
test -z "$ft_ctcp" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_ctcp='#define CTCP'

def_dccfile='#undef DCC_FILE'
unset ans
$out $ac_n "[STABLE] DCC file support? .......................... [Y/n] "$ac_c
test "$ft_dccfile" && $out "$ft_dccfile" && ans=$ft_dccfile
test -z "$ft_dccfile" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_dccfile='#define DCC_FILE'

def_debug='#undef DEBUG'
unset ans
$out $ac_n "[STABLE] Debug support? ............................. [y/N] "$ac_c
test "$ft_debug" && $out "$ft_debug" && ans=$ft_debug
test -z "$ft_debug" && read ans
test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_debug='#define DEBUG'

def_dynamode='#undef DYNAMODE'
unset ans
$out $ac_n "[STABLE] Dynamic channel limit (+l)? ................ [Y/n] "$ac_c
test "$ft_dynamode" && $out "$ft_dynamode" && ans=$ft_dynamode
test -z "$ft_dynamode" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_dynamode='#define DYNAMODE'

def_dyncmd='#undef DYNCMD'
unset ans
$out $ac_n "[STABLE] Dynamic command levels support? ............ [Y/n] "$ac_c
test "$ft_dyncmd" && $out "$ft_dyncmd" && ans=$ft_dyncmd
test -z "$ft_dyncmd" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_dyncmd='#define DYNCMD'

def_greet='#undef GREET'
unset ans
$out $ac_n "[STABLE] Greet support? ............................. [Y/n] "$ac_c
test "$ft_greet" && $out "$ft_greet" && ans=$ft_greet
test -z "$ft_greet" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_greet='#define GREET'

def_web='#undef WEB'
unset ans
$out $ac_n "[ ALPHA] HTTP server support? ....................... [y/N] "$ac_c
test "$ft_web" && $out "$ft_web" && ans=$ft_web
test -z "$ft_web" && read ans
test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_web='#define WEB'

def_hostinfo='#undef HOSTINFO'
unset ans
$out $ac_n "[ BETA ] Host info support? ......................... [Y/n] "$ac_c
test "$ft_hostinfo" && $out "$ft_hostinfo" && ans=$ft_hostinfo
test -z "$ft_hostinfo" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_hostinfo='#define HOSTINFO'

def_bounce='#undef BOUNCE'
unset ans
$out $ac_n "[STABLE] IRC proxy support? ......................... [Y/n] "$ac_c
test "$ft_bounce" && $out "$ft_bounce" && ans=$ft_bounce
test -z "$ft_bounce" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_bounce='#define BOUNCE'

def_ircd_ext='#undef IRCD_EXTENSIONS'
unset ans
$out $ac_n "[ BETA ] IRCD extensions support? ................... [Y/n] "$ac_c
test "$ft_ircd_ext" && $out "$ft_ircd_ext" && ans=$ft_ircd_ext
test -z "$ft_ircd_ext" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_ircd_ext='#define IRCD_EXTENSIONS'

def_netcfg='#undef NETCFG'
unset ans
$out $ac_n "[ ALPHA] Netcfg support? ............................ [Y/n] "$ac_c
test "$ft_netcfg" && $out "$ft_netcfg" && ans=$ft_netcfg
test -z "$ft_netcfg" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_netcfg='#define NETCFG'

def_newbie='#undef NEWBIE'
unset ans
$out $ac_n "[STABLE] Newbie support? ............................ [Y/n] "$ac_c
test "$ft_newbie" && $out "$ft_newbie" && ans=$ft_newbie
test -z "$ft_newbie" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_newbie='#define NEWBIE'

def_note='#undef NOTE'
unset ans
$out $ac_n "[STABLE] Note support? .............................. [Y/n] "$ac_c
test "$ft_note" && $out "$ft_note" && ans=$ft_note
test -z "$ft_note" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_note='#define NOTE'

def_notify='#undef NOTIFY'
unset ans
$out $ac_n "[STABLE] Notify support? ............................ [Y/n] "$ac_c
test "$ft_notify" && $out "$ft_notify" && ans=$ft_notify
test -z "$ft_notify" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_notify='#define NOTIFY'

def_md5='#undef MD5CRYPT'
unset ans
$out $ac_n "[STABLE] Password hashing with MD5? (less secure) ... [Y/n] "$ac_c
if [ "$has_md5" = no ]; then
  $out 'no (unsupported)'
elif [ "$has_md5" = notest ]; then
  $out 'no'
else
  test "$ft_md5" && $out "$ft_md5" && ans=$ft_md5
  test -z "$ft_md5" && read ans
  test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes -o "$ans" = internal && def_md5='#define MD5CRYPT'
fi

def_sha='#undef SHACRYPT'
unset ans
$out $ac_n "[ BETA ] Password hashing with SHA? (best) .......... [Y/n] "$ac_c
if [ "$has_sha" = no ]; then
  $out 'no (unsupported)'
else
  test "$ft_sha" && $out "$ft_sha" && ans=$ft_sha
  test -z "$ft_sha" && read ans
  test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes -o "$ans" = internal && def_sha='#define SHACRYPT'
fi

def_perl='#undef PERL'
unset ans
$out $ac_n "[ ALPHA] Scripting with Perl? ....................... [y/N] "$ac_c
if [ "$has_perl" = no ]; then
  $out 'no (unsupported)'
elif [ "$has_perl" = notest ]; then
  $out 'no'
else
  test "$ft_perl" && $out "$ft_perl" && ans=$ft_perl
  test -z "$ft_perl" && read ans
  test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_perl='#define PERL'
fi

def_python='#undef PYTHON'
unset ans
$out $ac_n "[ BETA ] Scripting with Python? ..................... [y/N] "$ac_c
if [ "$has_python" = no ]; then
  $out 'no (unsupported)'
elif [ "$has_python" = notest ]; then
  $out 'no'
else
  test "$ft_python" && $out "$ft_python" && ans=$ft_python
  test -z "$ft_python" && read ans
  test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_python='#define PYTHON'
fi
if [ "$def_python" = '#undef PYTHON' ]; then
  libpython=
  incpython=
fi

def_tcl='#undef TCL'
unset ans
$out $ac_n "[ BETA ] Scripting with Tcl? ........................ [y/N] "$ac_c
if [ "$has_tcl" = no ]; then
  $out 'no (unsupported)'
elif [ "$has_tcl" = notest ]; then
  $out 'no'
else
  test "$ft_tcl" && $out "$ft_tcl" && ans=$ft_tcl
  test -z "$ft_tcl" && read ans
  test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_tcl='#define TCL'
fi
if [ "$def_tcl" = '#undef TCL' ]; then
  libtcl=
  inctcl=
fi

def_seen='#undef SEEN'
unset ans
$out $ac_n "[STABLE] Seen support? .............................. [y/N] "$ac_c
test "$ft_seen" && $out "$ft_seen" && ans=$ft_seen
test -z "$ft_seen" && read ans
test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_seen='#define SEEN'

def_session='#undef SESSION'
unset ans
$out $ac_n "[STABLE] Session support? ........................... [Y/n] "$ac_c
test "$ft_session" && $out "$ft_session" && ans=$ft_session
test -z "$ft_session" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_session='#define SESSION'

def_stats='#undef STATS'
unset ans
$out $ac_n "[STABLE] Statistics support? ........................ [Y/n] "$ac_c
test "$ft_stats" && $out "$ft_stats" && ans=$ft_stats
test -z "$ft_stats" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_stats='#define STATS'

def_telnet='#undef TELNET'
unset ans
$out $ac_n "[STABLE] Telnet support? ............................ [Y/n] "$ac_c
test "$ft_telnet" && $out "$ft_telnet" && ans=$ft_telnet
test -z "$ft_telnet" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_telnet='#define TELNET'

def_trivia='#undef TRIVIA'
unset ans
$out $ac_n "[STABLE] Trivia support? ............................ [y/N] "$ac_c
test "$ft_trivia" && $out "$ft_trivia" && ans=$ft_trivia
test -z "$ft_trivia" && read ans
test "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_trivia='#define TRIVIA'

def_toybox='#undef TOYBOX'
unset ans
$out $ac_n "[STABLE] Toybox fun and games? ...................... [Y/n] "$ac_c
test "$ft_toybox" && $out "$ft_toybox" && ans=$ft_toybox
test -z "$ft_toybox" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_toybox='#define TOYBOX'

def_uptime='#undef UPTIME'
unset ans
$out $ac_n "[STABLE] Uptime support? ............................ [Y/n] "$ac_c
test "$ft_uptime" && $out "$ft_uptime" && ans=$ft_uptime
test -z "$ft_uptime" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_uptime='#define UPTIME'

def_urlcapture='#undef URLCAPTURE'
unset ans
$out $ac_n "[ BETA ] URL capture support? ....................... [Y/n] "$ac_c
test "$ft_urlcapture" && $out "$ft_urlcapture" && ans=$ft_urlcapture
test -z "$ft_urlcapture" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_urlcapture='#define URLCAPTURE'

def_wingate='#undef WINGATE'
unset ans
$out $ac_n "[STABLE] WinGate support? ........................... [Y/n] "$ac_c
test "$ft_wingate" && $out "$ft_wingate" && ans=$ft_wingate
test -z "$ft_wingate" && read ans
test -z "$ans" -o "$ans" = y -o "$ans" = Y -o "$ans" = yes -o "$ans" = YES -o "$ans" = Yes && def_wingate='#define WINGATE'

$out ''
echo "Creating src/Makefile"

O_FLAGS="$cc_optimize_flag $cc_arch_flag $cc_march_flag $cc_ofp_flag"
W_FLAGS="$cc_wall_flag $cc_wshadow_flag $cc_fnostrictalias"
libflags="$libcrypt $libtcl $libnsl $libsocket $libresolv $libpython $libperl"
lflags="$cc_g_flag -o"
cprof="$cc_pg_flag $cc_pg_define"
lprof="$cc_pg_flag $cc_pg_define $libpgdl"

if [ -z "$cf_cflags" ]; then
  cf_cflags='$(PIPEFLAG) $(GDBFLAG) $(WARNFLAG) $(OPTIMIZE)'
fi

echo '# This file is generated from Makefile.in' > src/Makefile
sed "
s%@CC@%$CCshort%;
s%@ldscript@%$ldscript%;
s%@pipeflag@%$cc_pipe_flag%;
s%@gdbflag@%$cc_g_flag%;
/@cprof@/      { s,@cprof@,$cprof,;         };
/@lprof@/      { s,@lprof@,$lprof,;         };
/@lflags@/     { s,@lflags@,$lflags,;       };
/@libflags@/   { s,@libflags@,$libflags,;   };
/@W_FLAGS@/    { s/@W_FLAGS@/$W_FLAGS/;     };
/@O_FLAGS@/    { s/@O_FLAGS@/$O_FLAGS/;     };
/@CFLAGS@/     { s/@CFLAGS@/$cf_cflags/;    };
/@I_PERL@/     { s,@I_PERL@,$I_PERL,;       };
/@L_PERL@/     { s,@L_PERL@,$L_PERL,;       };
/@PYINCLUDE@/  { s,@PYINCLUDE@,$incpython,; };
/@TCLINCLUDE@/ { s,@TCLINCLUDE@,$inctcl,;   };
s|@MD5_C@|$MD5_C|;
s|@SHA_C@|$SHA_C|;
s|@MD5_O@|$MD5_O|;
s|@SHA_O@|$SHA_O|;
s%@oc@%$objcomment%;
s%@sz@%$sizecomment%;
s%@makejobs@%$makejobs%;
s/[ 	]*\$//g;
" < src/Makefile.in >> src/Makefile || exit 1

echo "Creating src/config.h"

CRYPT_HAS_SHA='#undef CRYPT_HAS_SHA'
CRYPT_HAS_MD5='#undef CRYPT_HAS_MD5'
CRYPT_HAS_DES='#undef CRYPT_HAS_DES'
[ "$has_sha" = yes ] && CRYPT_HAS_SHA='#define CRYPT_HAS_SHA'
[ "$has_md5" = yes ] && CRYPT_HAS_MD5='#define CRYPT_HAS_MD5'
[ "$has_des" = yes ] && CRYPT_HAS_DES='#define CRYPT_HAS_DES'

echo '/* This file is generated from config.h.in */' > src/config.h
sed "
   s|@DEF_ALIAS@|$def_alias|;
  s|@DEF_BOTNET@|$def_botnet|;
  s|@DEF_BOUNCE@|$def_bounce|;
 s|@DEF_CHANBAN@|$def_chanban|;
    s|@DEF_CTCP@|$def_ctcp|;
 s|@DEF_DCCFILE@|$def_dccfile|;
   s|@DEF_DEBUG@|$def_debug|;
  s|@DEF_DYNCMD@|$def_dyncmd|;
s|@DEF_DYNAMODE@|$def_dynamode|;
   s|@DEF_GREET@|$def_greet|;
s|@DEF_HOSTINFO@|$def_hostinfo|;
  s|@DEF_IDWRAP@|$def_idwrap|;
s|@DEF_IRCD_EXT@|$def_ircd_ext|;
     s|@DEF_MD5@|$def_md5|;
     s|@DEF_SHA@|$def_sha|;
  s|@DEF_NETCFG@|$def_netcfg|;
  s|@DEF_NEWBIE@|$def_newbie|;
    s|@DEF_NOTE@|$def_note|;
  s|@DEF_NOTIFY@|$def_notify|;
    s|@DEF_PERL@|$def_perl|;
  s|@DEF_PYTHON@|$def_python|;
  s|@DEF_RAWDNS@|$def_rawdns|;
s|@DEF_REDIRECT@|$def_redirect|;
    s|@DEF_SEEN@|$def_seen|;
 s|@DEF_SESSION@|$def_session|;
   s|@DEF_STATS@|$def_stats|;
s|@DEF_SUPPRESS@|$def_suppress|;
     s|@DEF_TCL@|$def_tcl|;
  s|@DEF_TELNET@|$def_telnet|;
  s|@DEF_TOYBOX@|$def_toybox|;
  s|@DEF_TRIVIA@|$def_trivia|;
  s|@DEF_UPTIME@|$def_uptime|;
s|@DEF_URLCAPTURE@|$def_urlcapture|;
     s|@DEF_WEB@|$def_web|;
 s|@DEF_WINGATE@|$def_wingate|;

s|@DEF_GIT@|$def_git|;
s|@LIT_END@|$LIT_END|;
s|@BIG_END@|$BIG_END|;
s|@DEF_CRYPT_FUNCTION@|$CRYPT_FUNCTION|;
s|@CRYPT_HAS_SHA@|$CRYPT_HAS_SHA|;
s|@CRYPT_HAS_MD5@|$CRYPT_HAS_MD5|;
s|@CRYPT_HAS_DES@|$CRYPT_HAS_DES|;
s|@IDWRAP_PATH@|$IDWRAP_PATH|;
s|@PTSIZE_DEFINE32@|$PTSIZE_DEFINE32|;
s|@PTSIZE_DEFINE64@|$PTSIZE_DEFINE64|;
s|@UNALIGNED_MEM@|$UNALIGNED_MEM|;
" < src/config.h.in >> src/config.h

save_myconfig() {
  names="alias botnet bounce chanban ctcp dccfile debug dynamode dyncmd greet hostinfo idwrap ircd_ext libmusl md5
    netcfg newbie note notify perl profiling python rawdns redirect seen session sha stats suppress tcl
    telnet toybox trivia uptime urlcapture web wingate"
  echo "./configure \\" > ./myconfig
  chmod 755 ./myconfig
  for uu in $names
  do
    eval v="\$ft_$uu"
    case "$v" in
      yes) echo "--with-"$uu" \\" >> ./myconfig ;;
      no) echo "--without-"$uu" \\" >> ./myconfig ;;
    esac
  done
  test "$compile" = yes && echo "--compile \\" >> ./myconfig
  test "$install" = yes && echo "--install \\" >> ./myconfig
  test "$silentopt" = yes && echo "--silence=options \\" >> ./myconfig
  test "$ft_md5" = internal && echo "--md5=internal \\" >> ./myconfig
  test "$ft_sha" = internal && echo "--sha=internal \\" >> ./myconfig
  test "$optitype" = speed && echo "--optimize=speed \\" >> ./myconfig
  test "$optitype" = size && echo "--optimize=size \\" >> ./myconfig
  test "$cc_ofp_opt" = yes && echo "--use-ofp \\" >> ./myconfig
  test "$cc_ofp_opt" = yes && echo "--no-ofp \\" >> ./myconfig
  test ! "$try_libmusl" = no -a -x "$CC" && echo "--with-libmusl="$CC" \\" >> ./myconfig
}

if [ -w ./myconfig -o ! -e ./myconfig ]; then
  save_myconfig
fi

if [ "$install" = yes ]; then
  make $makejobs -C src energymech
  exit
fi

if [ "$compile" = yes ]; then
  make $makejobs -C src energymech
  exit
fi

$out ''
$out 'All done. You can now "make install"'
if [ ! "$cores" = 1 ]; then
  $out "For speedy compiling, use \`\`make -j$cores''"
fi
$out ''
$out "Your chosen ./configure options have been saved to ./myconfig"
$out 'if you wish to save ./myconfig in its current state, chmod -w ./myconfig'
$out ''
$out 'Submit your bugreports at https://github.com/MadCamel/energymech/issues'
$out ''
$out 'You have read the README file I hope?'
$out ''
