#!/bin/sh

# Copyright (C) 1999  Hendrik Luebben

# This program is subject to the GNU General Public License
# see Hnews.cpp for details


# If the module "Keys" does not compile correctly on your machine,
# set "NoRaw" to "true" and re-run <Install>.
NoRaw=false


# If your BackSpace keys behaves wrong (i.e.: like Delete) when running
# Hnews, set KeySet="" below
KeySet="-DPC_LINUX"


NIL=/dev/null

if test -z "$1"; then
 echo
 echo "Missing parameter(s); correct call:"
 echo
 echo "            Install \"news.server.name\" [compile-only]"
 echo
 exit
fi


VERSION="2.21"

echo
echo Welcome to the installation script for Hnews v$VERSION !
echo
echo
echo "This computer has the following \"uname\" characteristics (these are"
echo "needed in case some error occurs when trying to compile Hnews):"
uname -a
echo


if test "$2" != "compile-only"; then
######################################################################

# This should, but does not work reliably (telnet return code varies) !!

# check if the specified "default server" really is a news server
#telnet $1 119 >/dev/null 2>/dev/null <<EoT
#quit
#EoT
#retval=`echo "$?"`
#if test ! "$retval" = "1"; then
#  echo
#  echo \"$1\" does not respond.
#  echo
#  echo "Either the server is currently out of order, in that case please"
#  echo "try the installation again later;"
#  echo "or you mistyped the name, in that case please check it and then"
#  echo "restart \"Install\"."
#  echo
#  exit
#else
#  echo News server \"$1\" verified.
#  echo
#fi


sed -V >$NIL 2>$NIL
retval=`echo "$?"`
if test ! "$retval" = "0"; then
 sed >$NIL
 retval=`echo "$?"`
fi
if test ! "$retval" = "0"; then
 echo \"sed\" is not installed on this computer.
 echo "You have to set the correct paths for \"uudecode\", \"mmencode\" and \"rm\","
 echo "as well as the program version ($VERSION) and the default news server by hand."
 echo All these fields are located at the top of \"Hnews.h.org\".
 echo
 echo Then call \"Install\" again as follows:
 echo
 echo "            Install \"news.server.name\" compile-only"
 echo
 exit
else
 p=`echo $PATH | sed s/":"/" "/g`
fi


echo "Trying to set \"$1\" as default server ...  "
sed s/"^#define DefaultServer.*$"/"#define DefaultServer \"$1\""/ Hnews.h.org >tmp
echo "                                                            ok."


echo "Trying to set program version ...  "
sed s/"^#define VERSION.*$"/"#define VERSION \"$VERSION\""/ tmp >tmp1
rm tmp
echo "                                                            ok."

echo "Trying to set path for \"uudecode\" ...  "
uudecode >$NIL 2>$NIL <<EoT
begin 640 uu.test.tmp
#>WT*
 
end
EoT
retval=`echo "$?"`
rm uu.test.tmp
if test "$retval" != "0"; then
  echo
  echo \"uudecode\" is not installed on this computer.
  echo "Hnews will not be able to decode uuencoded images. (They make up about"
  echo "90% of the entire amount of images.)"
  mv tmp1 tmp2
else
  detect=""
  IFS=":"
  for i in $PATH; do
    if test -x $i/uudecode && ! test -d $i/uudecode; then
      detect=$i/uudecode
      #echo $detect
      break
    fi
  done
  pn=`echo $detect | sed s/"\/"/"%%%%%"/g`
  sed s/"^\/\/#define UUDECODE_PATH.*$"/"#define UUDECODE_PATH \"$pn\""/ tmp1 >tmp2
  rm tmp1
  echo "                                                            ok."
fi

echo "Trying to set path for \"mmencode\"...  "
mmencode <$NIL >$NIL 2>$NIL
retval=`echo "$?"`
if test "$retval" != "0"; then
  echo
  echo \"mmencode\" is not installed on this computer.
  echo "Hnews will not be able to decode mmencoded images. (They make up about"
  echo "10% of the entire amount of images.)"
  mv tmp2 tmp3
else
  detect=""
  IFS=":"
  for i in $PATH; do
    if test -x $i/mmencode && ! test -d $i/mmencode; then
      detect=$i/mmencode
      #echo $detect
      break
    fi
  done
  pn=`echo $detect | sed s/"\/"/"%%%%%"/g`
  sed s/"^\/\/#define MMENCODE_PATH.*$"/"#define MMENCODE_PATH \"$pn\""/ tmp2 >tmp3
  rm tmp2
  echo "                                                            ok."
fi

sed s/"%%%%%"/"\/"/g tmp3 >Hnews.h
rm tmp3

######################################################################
fi


echo

CODE=nsl
echo "Looking for libraries:"
echo "  $CODE:"
FNAME="/usr/lib/lib${CODE}.a"
if test -f $FNAME; then
 NSL="-l$CODE"
 echo "                    found, ok"
else
 NSL=""
 echo "                    not detected; probably harmless, though"
fi
CODE=socket
echo "  $CODE:"
FNAME="/usr/lib/lib${CODE}.a"
if test -f $FNAME; then
 SOCKET="-l$CODE"
 echo "                    found, ok"
else
 SOCKET=""
 echo "                    not detected, probably harmless, though"
fi
echo

echo "Compiling Hnews.cpp ...  "
gcc -Wall -c LogBook.cpp
gcc -Wall -c vt100.cpp
gcc -Wall -c ioctl.cpp
if test "$NoRaw" = "true" ; then
 gcc -Wall -DNoRaw LogBook.o vt100.o ioctl.o Hnews.cpp -o Hnews $NSL $SOCKET
else
 gcc -Wall -DPC_LINUX -c Keys.cpp
 gcc -Wall LogBook.o Keys.o vt100.o ioctl.o Hnews.cpp -o Hnews $NSL $SOCKET
fi

retval=`echo "$?"`
if test ! "$retval" = "0"; then
 echo
 echo "Hnews could not be compiled correctly."
 echo
 echo "Please mail the complete output produced by this call of \"Install\" to:"
 echo "   compuman@cs.tu-berlin.de   OR   hendrik@salk.edu"
 echo
 echo "You will receive assistance with this problem as soon as possible."
 echo
 echo
 exit
fi

echo "                                                            done."
echo
echo
rm LogBook.o
if ! test "$NoRaw" = "true" ; then
 rm Keys.o
fi
rm ioctl.o
rm vt100.o
mkdir Hnews.src 2>$NIL
mv Hnews.cpp Hnews.h Hnews.h.org Install Keys.* LogBook.* MsgCodes.h ioctl.* vt100.* Hnews.src

echo "Hnews has been successfully compiled and is ready for use, now !"
echo
echo
echo "All source files for Hnews, which should no longer be needed, have been"
echo "moved to the newly created subdirectory \"Hnews.src\"."
echo "If, for some reason, you should have to re-run \"Install\", you have to"
echo "move them back into your Hnews-directory, first."
echo "To do that, change into that directory and then type:"
echo "             mv Hnews.src/* .  <<Enter>>"
echo "Then proceed with the installation as usual."  
echo
echo
echo "To start Hnews, type:"
echo "             Hnews  <<Enter>>"  
echo
echo
