#!/bin/sh
#
# this script is a front end to `configure' which also asks a few
# other questions such as default server, help service, etc.
# $Id: easyinst,v 2.5 1994/02/06 05:13:19 mrgreen Exp $
#
# $defserver
# $sysinstall (exists and is nonempty if system install)
# $prefix
# $bindir
# $libdir
# $help_service
#
# Eventually, if easyinst.status defines no previous $libdir,
# and the environment variable $IRCLIB is set and non-nul,
# $libdir will default to that.
#

#
# Check if this is run through sh...
#
export PATH || \
(echo "This shell does not look like sh... buggy OS."; sh $0; kill $$)

#
# List of HELP SERVICES
#
help_services="NULL IrcIIhelp help_US help_UK help_AU help_EU"

#
# File to save configuration in...
#
savefile=easyinst.status

#
# Checking out how to suppress new lines...
#
if [ "`echo -n a`" = "a" ]; then
  n='-n'
  c=''
else
  n=''
  c='\c'
fi

#
# Trick to load old easyinst values :)
#
if [ -r "$savefile" ]; then
  echo "Loading previous easyinst choices..."
  trap "rm -f $savefile; echo Fatal error. Please restart easyinst." 0
  . ./$savefile || \
    (echo Fatal error. Please restart easyinst.; rm -f ${savefile}; kill \$\$)
  trap 0
fi

cat << EOF

Welcome to the easy install script.

I will ask you a few questions. Most of the time, a default value will
be printed, and you'll just need to hit the 'Return' key to confirm.

If you are satisfied with your choices, I will then run a script for you
to work out some of the hairier parts of your system.

	Good luck!

EOF
echo $n "Press 'Return' to go on: $c"
read answer

# we have to get this from the user:
#  default server, default help service, and where to install things.

# first we get the default server.

cat << EOF

I need to know the name of the irc server you intend to use as your
primary link.  You should have done a little research by this time to see
what servers are in your area. You should always pick the absolute closest
server to you.  You can use an IP address instead of a name as well.

EOF

while true; do
  echo "Please type the name of a server."
  echo $n "[${defserver-no default}]: $c"
  read answer
  case "$answer" in
  "")
    if [ "$defserver" ]; then break; fi
    ;;
  *)
    defserver="$answer"
    break
    ;;
  esac
done

# got a default server, must be time to get the installation dir.

cat << EOF

Ok, we've got a default server now, next we have to choose where things
are going to go.  First i need to know if you are compiling ircii for
yourself, or for the system.

EOF

while true; do
  echo "Do you want to install ircii for the system ? (Yes/No)"
  if [ "$prefix" ]; then
    echo "If you want to keep old directory values, just press 'Return'."
  fi
  if [ "$sysinstall" ]; then
    echo $n "[Yes]: $c"
  else
    echo $n "[No]: $c"
  fi
  read answer
  case "$answer" in
  y*|Y*)
    sysinstall=nap
    prefix="${prefix-/usr/local}"
    break
    ;;
  n*|N*)
    sysinstall=
    prefix="${prefix-${HOME}}"
    break
    ;;
  *)
    prefix="${prefix-${HOME}}"
    break
    ;;
  esac
done

cat << EOF

What's the destination area (prefix) of the installation ?
Such a directory will probably have two subdirectories,
something like 'bin' for the binaries, and 'lib/irc' for the libraries.
EOF
echo $n "[${prefix}]: $c"
read answer
#
# If we have an answer, reset the other variables so that they get computed.
#
if [ "$answer" ]; then
  prefix="$answer"
  bindir=
  libdir=
fi

if [ -z "$bindir" ]; then
  bindir="${prefix}/bin"
fi
echo ""
echo "Where do you want to install the ircII binaries ?"
echo $n "[${bindir}] $c"
read answer
if [ "$answer" ]; then bindir="$answer"; fi

#
# Algorithm used:
# Check if directory ${prefix}/lib exists...
#   Yes -> install in ${prefix}/lib/irc
#    No -> install in ${prefix}/IRC
#

# If no $libdir is defined at this stage, but there's a $IRCLIB
# in the environment, default to this value.

if [ -z "$libdir" ]; then
  if [ "${IRCLIB}" ]; then
    libdir="${IRCLIB}"
  elif [ -d "${prefix}/lib" ]; then
    libdir="${prefix}/lib/irc"
  else
    libdir="${prefix}/IRC"
  fi
fi

echo ""
echo "Where do you want to install the irc librairies ?"
echo $n "[${libdir}] $c"
read answer
if [ "$answer" ]; then libdir="$answer"; fi

# ok, we have places to put things now.

cat << EOF

Moving right along here.  Time to tell me which help service you'd
like to use.  Please enter the number of the one that is closest to you.
You can choose 0) if you don't know.

EOF

index=0
for a in $help_services; do
  echo "	$index) $a"
  index=`expr $index + 1`
done

index=`expr "$index" - 1`
echo ""
while true; do
  echo $n "Choice (0-$index) [${help_service-no default}]: $c"
  read choice
  if [ -z "$choice" ]; then choice="${help_service-999}"; fi
  if [ "$choice" -gt "$index" ] || [ "$choice" -lt 0 ]; then :; else break; fi
done

help_service="$choice"
sedbit='[^ ]*'
while [ "$choice" -gt 0 ]; do
  choice=`expr $choice - 1`
  sedfull="$sedbit $sedfull"
done

if [ -n "$sedfull" ]; then
  defhelp=`echo $help_services | sed -e "s/$sedfull//" -e "s/ .*//"`
else
  defhelp=`echo $help_services | sed -e "s/ .*//"`
fi

cat << EOF

I will summarize below the various choices you've made.

                Default server: $defserver
                        Prefix: $prefix
    Directory for binary files: $bindir
       Directory for libraries: $libdir
                  Help service: $defhelp

Do you want to save those values for future use and proceed ? (Y/N)
EOF
while true; do
  echo $n "[Yes]: $c"
  read answer
  case "$answer" in
  n*|N*)
    echo "Exiting without saving..."
    exit 1
    ;;
  y*|Y*|"")
    break
    ;;
  esac
done

{
  cat << EOF
#
# ${savefile}
# This file is automagically generated by the command 'easyinst'
# Please do not edit.
#
EOF
  echo '# $Id: easyinst,v 2.5 1994/02/06 05:13:19 mrgreen Exp $'
  echo '# '
  cat << EOF
defserver="${defserver}"
sysinstall="${sysinstall}"
prefix="${prefix}"
bindir="${bindir}"
libdir="${libdir}"
help_service="${help_service}"

EOF
} > ${savefile}

cat << EOF

I just saved for you the current configuration in ${savefile}.
Now I will configure this version of ircii using the 'configure'
script provided.

This may take a few minutes, so sit back an enjoy the show...

EOF

# work out what arguments to call configure with.

IRCLIB="$libdir"

if [ "$defserver" ] || [ "$defhelp" ]; then
  rm -f include/config.h.old
  mv include/config.h include/config.h.old
  cp include/config.h.dist include/config.h
fi

if [ "$defserver" ]; then
  sed -e "s/\(#define[ 	]*DEFAULT_SERVER[	 ]*\).*$/\1 \"$defserver\"/" \
< include/config.h > include/config.h.new
  rm -f include/config.h
  mv -f include/config.h.new include/config.h
fi

if [ "$defhelp" ]; then
  if [ "$defhelp" = "NULL" ]; then
    defhelp="(char *) 0"
  else
    defhelp="\"$defhelp\""
  fi
  sed -e "s/\(#define[ 	]*DEFAULT_HELP_SERVICE[ 	]*\).*$/\1 $defhelp/" \
< include/config.h > include/config.h.new
  rm -f include/config.h
  mv -f include/config.h.new include/config.h
fi

echo calling configure...
. ./configure

# End of script
exit 0

