#!/bin/sh
#
# this script is a front end to `configure' which also asks a few
# other questions such as default server and Ethiopic font Directory
# EthioIRC installation 

# $defserver
# $log
# $fontdir
#
#

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

#
# 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 EthioIRC installation script.
enk'wan dehna meTu

I will ask you a few questions. 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.

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="melkam edl :) ...Press 'Return' to go on"
. ./.myread

# 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 do 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.
The default server is set to irc.enst.fr in Paris,France.

Suggestions :
For North America :
	irc.colorado.edu
	irc.ecn.uoknor.edu

For Europe :
	irc.funet.fi

until EthioIRCserver ...
You can check http://www.ie2.u-psud.fr/~kitaw/ETHIOIRC/server.lst
EOF

if test "$defserver"; then
  dflt="$defserver"
else
  dflt="irc.enst.fr"
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 font dir.

cat << EOF

Ok, we've got a default server now, next we have to choose where your
Ethiopic fonts are going to go. 
EOF

if test "$fontdir"; then
  dflt="$fontdir"
else
  dflt=`pwd`"/FONTDIR"
fi
rp="Please type where your Ethiopic Font directory should be or leave default"

while true; do
  . ./.myread
  case "$ans" in
  "$nodef")
    continue
    ;;
  /*)
    if test -d "$ans"; then :; else echo "$ans doesn't seem to exist right now. You'll need to create it."
    fi
    ;;
  *)
    ;;
  esac
  break
done
fontdir="$ans"
log=`pwd`"/tmp/log"
eth=`pwd`"/EVIEW/eview"
cat << EOF
You just did the following choices :

                Default server: $defserver
                       fontdir: $fontdir
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

If you have made a typo, hit ^C now, and restart install.

Now I will configure this version of EthioIRC ...

so sit back an enjoy the show :)

EOF


echo "setting up ..."

if test "$defserver"; then
  sed -e "s@\(#define[ 	]*DEFAULTSERVER[	 ]*\).*\$@\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 test "$log"; then
  sed -e "s@\(#define[ 	]*LOGFILENAME[	 ]*\).*\$@\1 \"$log\"@" \
< include/config.h > include/config.h.new
  rm -f include/config.h
  mv -f include/config.h.new include/config.h
fi

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

if test "$fontdir"; then
  sed -e "s@\(#define[ 	]*FONTDIR[	 ]*\).*\$@\1 \"$fontdir\"@" \
< EVIEW/eview.h > EVIEW/eview.h.new
  rm -f EVIEW/eview.h
  mv -f EVIEW/eview.h.new EVIEW/eview.h
fi

#cp include/config.h EVIEW/config.h


echo Cleaning up ...
rm -f $trapdeletefiles
trap 0 1 2 15

echo Attempting Compilation
cd EVIEW
make
cd ..
echo compiling eirc ...
cc -o eirc eirc.c -ltermcap


echo "if compilation succeded type eirc to launch ETHIOIRC";


# End of script
exit 0

