#!/bin/sh
#
# this script is a front end to `configure' which also asks a few
# other questions such as default server, bindir, etc.
# $Id: easyinst,v 2.14 1997/10/20 08:01:56 mrg Exp $
#
# $defserver
# $sysinstall (exists and is nonempty if system install)
# $prefix
# $bindir
# $libdir
#
# Eventually, if $HOME/.irc.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 $$)

#
# File to save configuration in...
#

# I guess root doesn't want a /.irc.easyinst.status file created
if test -w /; then
  savefile=/tmp/irc.easyinst.status
else
  savefile=$HOME/.irc.easyinst.status
fi

#
# The famous "no default" string
#
nodef="no default"

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

#
# Set up ~user expansion
cat >.tildexp <<EOF
#!/bin/sh
case "\$1" in
~/*|~)
  echo \$1 | $sed "s|~|\${HOME-\$LOGDIR}|"
  ;;
~*)
  if test -f /bin/csh; then
    /bin/csh -f -c "glob \$1"
    failed=$?
    echo ""
    exit \$failed
  else
    name=\`$expr x\$1 : '..\([^/]*\)'\`
    dir=\`$sed -n -e "/^\${name}:/{s/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\).*"'\$'"/\1/" -e p -e q -e '}' </etc/passwd\`
    if test ! -d "\$dir"; then
      me=\`basename \$0\`
      echo "\$me: can't locate home directory for: \$name" \>\&2
      exit 1
    fi
    case "\$1" in
    */*)
      echo \$dir/\`$expr x\$1 : '..[^/]*/\(.*\)'\`
      ;;
    *)
      echo \$dir
      ;;
    esac
  fi
  ;;
*)
  echo \$1
  ;;
esac
EOF
chmod a+x .tildexp

#
# Set up shell script to read input
#
myecho="case \"\$xxxm\" in
'') echo $n \"\$rp $c\" >&4;;
*)  case \"\$rp\" in
  '') echo $n \"[\$xxxm] $c\";;
  *)
    if test \`echo \"\$rp [\$xxxm]  \" | wc -c\` -ge ${COLUMNS-80}; then
      echo \"\$rp\" >&4
      echo $n \"[\$xxxm] $c\" >&4
    else
      echo $n \"\$rp [\$xxxm] $c\" >&4
    fi
    ;;
  esac;;
esac"
cat <<EOF > .myread
xxxm=\$dflt
$myecho
ans='!'
case "\$fastread" in
yes) case "\$dflt" in
  '') ;;
  *) ans='';
    case "\$silent-\$rp" in
    true-) ;;
    *) echo " " >&4;;
    esac;;
  esac;;
*) case "\$silent" in
  true) case "\$rp" in
    '') ans='';;
    esac;;
  esac;;
esac
while expr "X\$ans" : "X!" >/dev/null; do
  read answ
  set x \$xxxm
  shift
  aok=''; eval "ans=\"\$answ\"" && aok=y
  case  "\$answ" in
  "\$ans")
    case "\$ans" in
    "!")
      sh 1>&4
      echo " "
      $myecho
      ;;
    !*)
      set x \`expr "X\$ans" : "X!\(.*\)\$"\`
      shift
      sh 1>&4 -c "\$*"
      echo " "
      $myecho
      ;;
    esac;;
  *)
    case "\$aok" in
    y)
      echo "*** Substitution done -- please confirm."
      xxxm="\$ans"
      ans=\`echo $n "\$ans$c" | tr '\012' ' '\`
      xxxm="\$ans"
      ans=!
      ;;
    *)
      echo "*** Error -- try again."
      ans=!
      ;;
    esac
    $myecho
    ;;
  esac
  case "\$ans\$xxxm\$nostick" in
  '')
    ans=!
    $myecho
    ;;
  esac
done
case "\$ans" in
'') ans="\$xxxm";;
esac
EOF

trapdeletefiles=".myread .tildexp"

trap '{
  exitstat=$?
  rm -f $trapdeletefiles
  trap "" 0
  echo "Aborting... cleaning up." >&2
  exit $exitstat
}' 0 1 2 15

# Prepare redirection for "silent" mode (to be implemented :)
exec 4>&1

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 get stuck on a question, you may use a ! shell escape to start
a subshell or execute a !command.

Some questions asking for file or directory names allow the use of the ~user
construct to specify the login directory belonging to "user", even if you
don't have a shell which knows about that. They'll be marked "(~user ok)".

The prompter used in this script allows you to use shell variables and
backticks in your answers.  You may use \$1, \$2, etc...  to refer to the words
in the default answer, as if the default line was a set of arguments given to a
script shell.  This means you may also use \$@ to repeat the whole default line,
so you do not have to re-type everything to add something to the default.

Everytime there is a substitution, you will have to confirm.  If there is an
error (e.g. an unmatched backtick), the default answer will remain unchanged
and you will be prompted again.

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
dflt="Press 'Return' to go on"
. ./.myread

#
# Trick to load old easyinst values :)
#
if test -r "$savefile"; then
  echo "
I see a $savefile file."
  dflt="y"
  rp="Do you want to use it to set the defaults?"
  . ./.myread
  case "$ans" in
  n*|N*) echo "Ignoring $savefile." ;;
  *)
    echo "Loading previous easyinst choices..."
    trap "{
      rm -f $savefile
      echo 'Fatal error. Please restart easyinst.' >&2
    }" 0
    . $savefile || \
      (
        echo 'Fatal error. Please restart easyinst.' >&2
        rm -f $savefile
        kill $$ \$\$
      )
    ;;
  esac
  trap 0
fi

# Because the previous branch as squished the trap on exit
trap '{
  exitstat=$?
  rm -f $trapdeletefiles
  trap "" 0
  echo "Aborting... cleaning up." >&2
  exit $exitstat
}' 0 1 2 15

# we have to get this from the user:
#  default server, 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

if test "$defserver"; then
  dflt="$defserver"
else
  dflt="$nodef"
fi
rp="Please type the name of a server."

while true; do
  . ./.myread
  case "$ans" in
  "$nodef")
    continue
    ;;
  /*)
    if test -f "$ans"; then :; else
      echo "$ans doesn't seem to exist right now. You'll need to create it."
    fi
    ;;
  *)
    ;;
  esac
  break
done
defserver="$ans"

# 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
if test "$prefix"; then
  echo "Note: If you want to keep old directory values, just hit 'Return'.
"
  nostick=true
fi

dflt=""
rp="Do you want to install ircii for the system?"
. ./.myread
case "$ans" in
y*|Y*)
  sysinstall=nap
  prefix="/usr/local"
  bindir=
  libdir=
  ;;
n*|N*)
  sysinstall=
  prefix="${HOME}"
  bindir=
  libdir=
  ;;
*)
  prefix="${prefix-${HOME}}"
  ;;
esac
nostick=""

cat << EOF

We now need to find out the destination area of the installation.
Such a directory will probably have two subdirectories,
something like 'bin' for the binaries, and 'lib/irc' for the libraries.

Hit 'Return' to keep previously computed values.
EOF
dflt="$prefix"
rp='
What is the destination prefix of the installation? (~user ok)'
. ./.myread
ans=`./.tildexp "$ans"`

#
# If we have an answer, reset the other variables so that they get computed.
#
case "$ans" in
"$dflt") ;;
*)
  prefix="$ans"
  bindir=
  libdir=
  ;;
esac

if test -z "$bindir"; then
  bindir="${prefix}/bin"
fi

dflt="$bindir"
rp='
Where do you want to install the ircII binaries? (~user ok)'
. ./.myread
ans=`./.tildexp "$ans"`

if test "$ans"; then bindir="$ans"; 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 test -z "$libdir"; then
  if test "${IRCLIB}"; then
    libdir="${IRCLIB}"
  elif test -d "${prefix}/lib"; then
    libdir="${prefix}/lib/irc"
  else
    libdir="${prefix}/IRC"
  fi
fi

dflt="$libdir"
rp='
Where do you want to install the irc libraries? (~user ok)'
. ./.myread
ans=`./.tildexp "$ans"`

if test "$ans"; then libdir="$ans"; fi

# ok, we have places to put things now.

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

EOF

rp='Do you want to save those values for future use and proceed?'
dflt='y'
. ./.myread
case "$ans" in
n*|N*)
  echo "Exiting without saving..."
  exit 1
  ;;
*)
  ;;
esac

{
  cat << EOF
#
# ${savefile}
# This file is automagically generated by the command 'easyinst'
# Please do not edit.
#
EOF
  echo '# $Id: easyinst,v 2.14 1997/10/20 08:01:56 mrg Exp $'
  echo "# Created: `date`"
  echo '# '
  cat << EOF
defserver="${defserver}"
sysinstall="${sysinstall}"
prefix="${prefix}"
bindir="${bindir}"
libdir="${libdir}"

EOF
} > ${savefile}

cat << EOF

I just saved for you the current configuration in
	$savefile

If you have made a typo, hit ^C now, edit this file and restart easyinst.

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.

echo "setting up config.h..."

IRCLIB="$libdir"

if test "$defserver"; then
  config_args="--with-default-server=$defserver"
fi

rm -f $trapdeletefiles
trap 0 1 2 15

export IRCLIB
export bindir

echo "calling configure..."
./configure --prefix=$prefix $config_args

# End of script
exit 0

