#!/bin/sh
#
# 4m v2.15 (setup)
# (C) Copyright 1990 by Carrick Sean Casey
# (C) Copyright 1993 by Scott Chasin and Michaeljon Miller
#
# 3-22-93 See file COPYING for copyright information.
#
# Setup/installation script


version=`grep VERSION h/version.h | awk '{print $3}' | tr -d '"'` 
homedir=${HOME}
username=`whoami`
today=`date`
cflags=""
ranlib="/bin/ranlib"
otherlibs="-ltermcap"
resolvlib=""
extra=""
 
# $HOME doesnt exist?
if [ "${homedir}" = "" ]; then homedir=`pwd`; fi
 
# defaults

defextra="" 
defsystem="SUN"
defbindir="${homedir}"
defmandir="${homedir}"
deflibdir="${homedir}"
defranlib="/bin/ranlib"
defcompiler="cc"
config="${homedir}/.4mrc"
defserver="cns.cscns.com"
defport="6677"
defpagesize="0"
defgroup="1"

# try to determine which form of echo to use
case "`echo -n`" in
  -n) echo="echo" ; c="\c" ;;
   *) echo="echo -n" ; c="" ;;
esac

#
#       Get the system type.
#
 
cat <<EOF

4m V${version} installation started on ${today}.
 
This script will ask you some questions. Default answers are provided in
brackets [like this], and will be used if you just type return.
 
Please enter your system type. The following systems are recognized:
 
BSD             Berkeley 4.x, 386BSD
DGUX            Aviion DG/UX
SUN             SunOs 4.0.x, SunOs 4.1.x
SOLARIS         SunOs 5.x (Solaris 2.x)
ULTRIX          DEC Vax Ultrix 4.x
AIX             IBM's AIX Unix
SYSV            AT&T System V and clones
HPUX            HP/UX Hewlett-Packard Unix v8.x and earlier
HPUX9           HP/UX Hewlett-Packard Unix v9.x
IRIX            Silicon Graphics Irix
DYNIX           Dynix on a Sequent Symmetry V1.x
PYR             Pyramid Unix
NEXT            NeXTStep 2.0
HCX             HCX Unix
APOLLO          HP/Apollo Domain Sr10.3
ESIX		ESIX System V/386 Release 4.0 
MACH386         Mach386 BSD
 
EOF
 
$echo "System type? [${defsystem}]: ${c}"
read system
system=`echo "$system" | tr 'a-z' 'A-Z'`
if [ -z "${system}" ]; then
        system="$defsystem"
fi
 
 
#
#       Make system dependent options 
#
 
case $system in
BSD)
	   cflags="${cflags} -DBSD"
	   lflags="-u"
	   ;;

DGUX)
	   cflags="${cflags} -DDGUX"
            defranlib="/bin/true"
	   ;;

SYSV)
	   cflags="${cflags} -DSYSV"
	   otherlibs="-lc -lsocket -ltermcap"
           defranlib="/bin/true"
	   ;;

ULTRIX)
	   cflags="${cflags} -DULTRIX"
           defranlib="/usr/bin/ranlib"
	   ;;
AIX)
           cflags="${cflags} -DAIX -w-"
           defranlib="/usr/bin/ranlib"
           ;;
SUN)
	   cflags="${cflags} -DSUN"
           defranlib="/bin/ranlib"
	   ;;
PYR)
	   otherlibs="-lc -lpyr -ltermcap"
           defranlib="/usr/bin/ranlib"
	   cflags="${cflags} -DPYR"
	   ;;
HPUX)
	   cflags="${cflags} -DHPUX"
	   otherlibs="-lBSD -ltermcap"
           defranlib="/bin/true"
	   lflags="-u"
	   ;;
HPUX9)
	   cflags="${cflags} -DHPUX9"
	   otherlibs="-ltermcap"
           defranlib="/bin/true"
	   lflags="-u"
	   ;;
DYNIX)
	   otherlibs="-lc -lseq -lsocket -linet -lnsl -ltermcap"
	   cflags="${cflags} -DDYNIX"
	   lflags="-u"
	   ;;
IRIX)
	   cflags="${cflags} -DIRIX"
	   if [ `/bin/uname -r | sed 's/\..*//'` = "4" ]; then
              cflags="-cckr ${cflags}"
           fi
           defranlib="/bin/true"
           otherlibs="${otherlibs} -lmalloc -lsun -ltermcap"
           lflags="-u"
           ;;
NEXT)
           cflags="-Dconst=\"/**/\" -DNEXT -bsd -traditional"
           otherlibs="-ltermcap"
           lflags="-u"
           ;;
APOLLO)
           cflags="-A nansi -A systype,bsd4.3 -DAPOLLO"
           otherlibs="-ltermcap"
           ;;
ESIX)
	   cflags="${cflags} -DESIX"
           otherlibs="-lc -lnsl -lresolv -lsocket -ltermcap"
           defranlib="/bin/true"
           ;;
HCX)
   	   cflags="${cflags}"
           lflags="-u"
           ;;
MACH386)
           cflags="${cflags} -DMACHBSD -traditional"
           defranlib="/usr/bin/ranlib"
           lflags="-u"
           ;;

SOLARIS)   cflags="${cflags} -DSOLARIS -DSYSV"
           otherlibs="-lsocket -lnsl -ltermcap"
           defranlib="/bin/true"
           ;;

*)
           echo ""
           echo "\"$system\" is not supported."
           exit 1
           ;;
esac
 
#
#       Get location for executable
#
 
cat <<EOF
 
 
When you type "make install" the program and manual page will be
installed.  The default will put the executable in your home directory.

EOF
 
$echo "Directory for executables? [${defbindir}]: ${c}"
read bindir
if [ -z "${bindir}" ]; then
        bindir="${defbindir}"
fi
$echo "Directory for manual page? [${defmandir}]: ${c}"
read mandir
if [ -z "${mandir}" ]; then
        mandir="${defmandir}"
fi
 
#
#       Get compiler to use
#
 
cat <<EOF
 
 
The package will normally be compiled with the regular C compiler, "cc".
You may specify an alternate compiler like GNU's gcc.
 
EOF
 
$echo "Compiler? [${defcompiler}]: ${c}"
read compiler
if [ -z "${compiler}" ]; then
        compiler="${defcompiler}"
fi
 
#
#       Extra options
#
 
cat <<EOF
 
If you use the default, the package will be compiled with "-O -s" to
conserve disk space.
 
EOF
 
if [ -z "${defextra}" -o "${compiler}" != "${defcompiler}" ]; then
        if [ "${compiler}" = "gcc" ]; then
                defextra="-O -s"
        else
                defextra="-O -s"
        fi
fi
        
$echo "Compiler options? [${defextra}]: ${c}"
read extra
if [ -z "${extra}" ]; then
        extra="${defextra}"
fi
cat <<EOF
 
The program "ranlib" helps compile our communications library archive.
Your system MUST have this executable in order to compile 4m. 

Ultrix and AIX store ranlib as /usr/bin/ranlib.  Most other Unix platforms
use /bin/ranlib.  If you are compiling on a SYSV machine, you don't need
to use ranlib, and you may set it to /bin/true.

Setup will try to determine your ranlib default. 

EOF
 
$echo "Location of ranlib? [${defranlib}]: ${c}"
read ranlib
if [ -z "${ranlib}" ]; then
        ranlib=${defranlib}
     if [ -z "${ranlib}" ]; then
        echo ""
        echo "${ranlib} could not be found.  Please ask your system"
        echo "administrator for its location."
        exit 1
     fi
fi
 
cat <<EOF
 
Creating configuration file for compile.

EOF
 
cat > config <<EOF
#
#       THIS IS AN AUTOMATICALLY GENERATED FILE
#
 
# Installing user's home directory
HOME            = ${homedir}
 
# Directory where the client will be installed.
BINDIR          = ${bindir}
 
# Directory where the manual page will be installed.
MANDIR          = ${mandir}

# Compiler to use.
CC              = ${compiler}
 
# Compile options.
CFLAGS          = ${extra} ${cflags}
 
# Other libraries to include.
OTHERLIBS       = ${otherlibs}
 
# Ranlib executable
RANLIB          = ${ranlib}
 
# Resolver library
RESOLVLIB       = ${resolvlib}
 
EOF

cat <<EOF

4m uses a configuration file to specify options for internal 4m
commands and functions.  This file, .4mrc is normally kept in
your HOME directory.

EOF
if [ -f ${config} ]; then
cat <<EOF

Setup has detected a 4m configuration file in your HOME directory.
If you are installing a new version of 4m this file should be
backed up or deleted so setup can create a new configuration
file for you.

Type "sh setup" after this has been completed.

EOF
    exit 1
fi
cat <<EOF

Setup will now ask you specific options to configure 4m for
your personal use.  Your choices will be saved to a .4mrc
file in your HOME directory.

In order for you to communicated with other 4m users, setup needs
to know which 4m server to connect you to.  If you don't know the
location of any 4m servers, just hit enter to select the default.

EOF
     $echo "Server? ["${defserver}"]: ${c}"
     read server
     if [ -z "${server}" ]; then
        server="${defserver}"
     fi
     echo ""
     $echo "Port? ["${defport}"]: ${c}"
     read port
     if [ -z "${port}" ]; then
        port="${defport}"
     fi

cat <<EOF

You should now pick a nickname or alias that you would liked to
be identified as.  Some users use their realnames, or funny
fictional characters (Hosehead, Parrot, Mj, Rizz, etc).

If you can't think of anything right now, your login id is
accepted as default.

EOF
     $echo "Your nickname? ["${username}"]: ${c}"
     read nickname
     if [ -z "${nickname}" ]; then
        nickname="${username}"
     fi
cat <<EOF

When you run 4m you are instantly thrown into a group.  A group
is similar to an "electronic CB channel".  Whatever you say in
a group goes out to the rest of that group.  Some groups are
divided by topics/interests.

Setup needs to know what group to put you in when you run 4m.
Most users keep the default and join group "1" upon execution.

EOF
     $echo "Default group? ["${defgroup}"]: ${c}"
     read group
     if [ -z "${group}" ]; then
        group="${defgroup}"
     fi
cat <<EOF

Creating your initial 4m configuration rc file ..

EOF

cat > ${config} <<EOF

# 4mrc file V${version} - Created on ${today}
#
# Options that require a boolean value can be either
# set to: true/false, 0/1, on/off, yes/no, etc.

# Default server to use.
server: ${server}

# Port where 4m server listens (normally 6677).
port: ${port}

# Nickname to use while in 4m.  If left blank, your system
# login ID will be used.
nickname: ${nickname}

# Initial group to join upon entering 4m.
group: ${group}

# Name of default logfile when invoked with /log.
logfile: 4m_log

# Restrict
# If True, 4m will disable all internal commands that allow
# users access to a shell in some way (/log, /shell, etc).
restrict: false

# Metacharacters
# The following metacharacters allow the user to configure
# 4m dynamically.

# %T  Current Time      %M  Current Time (Military)
# %@  Time online       %m  Mail waiting?
# %I  Input status      %V  Client Version
# %S  Current server    %P  Current port
# %%  Percent symbol    ^   Inverse toggle
# %&  Mail count        %N  Name of last user message
#                       %#  Last message (open/private)

# Status bar (Termcap feature only)
# Configure your status bar.  If left blank, default will be used.
statusbar: ^ %T [%@]  %C   Group: %G  (+%O)   %A     %I %m^

# Open message format
# Format of messages from other users sent to your group.
openmsg: <%N> %#

# Private message format
# Format of messages from other users send to you only.
privmsg: <*%N*> %#

# Default screen length.
# Set this equal to 0 to not display messages a page at a time.
pagesize: 0

# If echoback is set to true (default) text you send
# will be echoed back to you by the server.
echoback: true

# Number of lines in replay buffer.
bufferlines: 255

# Verify exit on quiting.
verifyquit: false

# Are beeps audible?
beeps: true

# Editor beeps.
# When a mistake is made, or input space has been
# exceeded, the editor will warn you with a beep.
editorbeep: true

# Command aliases
# Provides aliases for internal 4m commands (Also /alias internally).
# format: alias command <args>
#alias: finger shell finger
#alias: clap action claps.

# Notification list
# When set here or with the /notify command, the server will
# notify you of all logins and logouts of users specified in your
# notification list.
# Format: nickname or hostname (Wildcards allowed)
#notify: Mj
#notify: *.edu

# Startup command execution
# Allows internal 4m commands to be executed at startup
#execute: /groups

EOF
  	echo "4m configuration file written to ${config}."

#
#       All done. Tell the user what to do next.
#
 
cat <<EOF
 
 
Setup script complete.
 
Now you can type:
 
make            Compile 4m.
make install    Compile and install 4m and manual pages.
make clean      Clean up the mess that compile generates.
 
EOF
