#!/bin/csh
# Crude rude so easy to abuse install script # 2
# for release with ircII2.2
set path=(/usr/ucb /bin /usr/bin /usr/local/bin /usr/new .)
unset noclobber

alias getme 'grep \!* | awk '\''{printf("%s\n",$3)}'\'' | head -1'

if -e Makefile.bak then
	echo "Makefile.bak exists, moving on"
else
	echo "Creating Makefile.bak so as to not screw stuff up."
	cp Makefile Makefile.bak
endif
if -e config.h.bak then
	echo "config.h.bak exists, moving on"
else
	echo "Creating config.h.bak so as to not screw stuff up."
	cp config.h config.h.bak
endif

sleep 1
clear
cat << END_OF_SPEECH
============================ The Beginning =================================
Welcome to the install script.
Aren't you just giddy with anticipation?
Anyway.... this script assumes you are on a BSD unix box which means
that it doesn't change the OS specific defines in the Makefile.
If you know you have a weird system then you will have to edit the
Makefile by hand and define the necessary things for your machine.
Here we go.
----------------------------------------------------------------------------
END_OF_SPEECH
echo "Lemme find some stuff."
set homedir = `echo ~`
set temp = `which sed`
if ("$temp" == "") then
	echo "I don't work without 'sed'.  It might be in a funny directory"
	echo "which you need to add to the path variable at the top of this"
	echo "script."
	exit(1)
endif
set findawk = `which awk`
if ("$temp" == "") then
	echo "I don't work without 'awk'.  It might be in a funny directory"
	echo "which you need to add to the path variable at the top of this"
	echo "script."
	exit(1)
endif
# Another speech for yah.. 
cat << END_OF_SPEECH
Your home directory is $homedir
Now I need to know if you are going to be installing this in a system 
supported directory such as /usr/local/bin and /usr/local/lib or in
your own directory. Say 'home' for your directory, 'system' for the system
dirs. Default answers are shown within []'s and can be used just by 
pressing the RETURN key and nothing else. This will save alot of time.
END_OF_SPEECH

echo -n "home/system [home]?"
set where = $<
if ("$where" == "") then
	echo "Assuming you are installing in home dirs."
	set where = "home"
endif
while ( ( "$where" != "home" ) && ( "$where" != "system" ) )
	echo -n "'home' or 'system' please. >"
	set where = $<
end
echo "Thank you."
echo "Installing in $where directories......"
sleep 1
clear
set answer = "no"
while ("$answer" != "yes")
# Speech speech.. Let's hear another one.
cat << END_OF_SPEECH
-----------------------------------------------------------------------------
This part is important.  There is a directory which irc needs which I will
refer to as the IRCLIB directory.  EMEMBER this reference as I will mention 
it further along. This directory ussually contains at least two other 
directories 'script' and 'translation' and hopefully 'help'.  It also is the 
home of the ircII.motd file.  I'm going to ask where you want it, and you can 
chose the default value just by pressing return or enter your own, but remember 
this one has to be right for everything to work.
END_OF_SPEECH

if ("$where" == "home") then
	echo -n "Enter full pathname [$homedir/IRC] ?"
	set irclib = $<
	if ("$irclib" == "") then
		set irclib = "$homedir/IRC"
	endif
else
	echo -n "Enter full pathname [`getme IRCII_LIBRARY Makefile.bak`] ?"
	set irclib = $<
	if ("$irclib" == "") then
		set irclib = "`getme IRCII_LIBRARY Makefile.bak`"
	endif
endif
echo "You're irclib will be at $irclib."
echo "---------------------------------------------------------------------------"

# Set some stuff up for our cute foreach() loop below
# think of these as sideways arrays.
set ins_name = (INSTALL_EXECUTABLE INSTALL_IRCSERV INSTALL_IRCFLUSH INSTALL_WSERV)
set ins_varname = (irc_executable irc_serv irc_flush irc_wserv)
set ins_path = (irc ircserv ircflush wserv)

cat << END_OF_SPEECH
The following can be answered directly by supplying a full path name
for where you want things, or you can just chose the defaults by pressing
return. 
END_OF_SPEECH

if ("$where" == "home") then
	set basepath = $homedir/bin/
else
	set basepath = /usr/local/lib/irc/
endif

foreach index (1 2 3 4)
	echo "Where do you want to install the $ins_varname[$index] ?"
	echo -n "Enter full pathname [$basepath$ins_path[$index]] ?"
	set temp = $<
	if ("$temp" == "") then
		set $ins_varname[$index] = "$basepath$ins_path[$index]"
		set temp = "$basepath$ins_path[$index]"
	else
		set $ins_varname[$index] = "$temp"
	endif
	set basepath = `echo "$temp" | sed -e 's;/[a-zA-Z]*$;/;'`
	echo Installing $ins_varname[$index] in `eval echo \$$ins_varname[$index]`
end

clear
cat << END_OF_SPEECH
-----------------------------------------------------------------------------
To recap:
The IRCLIB          is: $irclib
We'll put the 
irc      executable at: $irc_executable
ircserv  executable at: $irc_serv
ircflush executable at: $irc_flush
wserv    executable at: $irc_wserv
END_OF_SPEECH
echo -n "Does this information look correct..<yes/no>  [yes] ?"
set answer = $<
if ("$answer" == "") set answer = "yes"
# end main while() loop 
end
clear
cat << END_OF_SPEECH
-------------------------------------------------------------------------
Ok.. Cool!!!. We're cruising now.  Almost ready to do some real work.
I do need to know the name of a server that 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.  The server choice chosen here can be overridden on the 
command line or with the   IRCLIB/servers file  (remember IRCLIB from above?)
but it is recomended that you get it right the first time here. You can use
an IP address instead of a name as well.
END_OF_SPEECH
set defserver = ""
set answer = "no"
while ("$answer" != "yes")
	while ("$defserver" == "")
		echo -n "Enter server name >"
		set defserver = $<
		if ("$defserver" == "") echo "Try again."
	end
	echo "Your default server is $defserver"
	echo -n "Is this correct <yes/no> [yes] ?"
	set answer = $<
	if ("$answer" == "") set answer = "yes"
end

clear
cat << END_OF_SPEECH
Ok.. Last thing we need to resolve is which help service you are going to use
if you are forced to use a help service.  There are extensive help files
for irc, and it is recomended that you get and install the lastest help files
in your IRCLIB directory, but if for some reason you cannot do so then you 
will be able to talk to a help service somewhere in the world.  The 
current choices will be listed below.
IrcIIhelp is not recomended as it is down 99% of the time, so you should 
chose name from those above closest to your region in the world. There is 
an undefined setting which really shouldn't be used unless you DO have the
real help files installed.
Select a NUMBER from the following choices.   USE ONLY A NUMBER!!
END_OF_SPEECH
set helpservs = (NULL IrcIIhelp help_US help_UK help_AU help_EU)
set index = 0
set answer = "no"
foreach temp ($helpservs)
	set index = `echo $index | awk '{print $1 + 1}'`
	echo "$index) $temp"
end
while ("$answer" != "yes")
	set numanswer = 100
	while (($numanswer < 1) || ($numanswer > $index))
		echo -n "Select a number (1-$index) [1] >"
		set numanswer = $<
	end
	# end of while(numanswer)
	echo "You chose $helpservs[$numanswer] as your help service."
	echo -n "Is this correct <yes/no> [yes] ?"
	set answer = $<
	if ("$answer" == "") set answer = "yes"
end
set help_service = $helpservs[$numanswer]
# End of while (answer)

clear
echo "Ok.. just need to set up some stuff before we make changes"
set irc_executable_ = $irc_executable
set irc_executable = "INSTALL_EXECUTABLE = $irc_executable"
set irc_serv_ = $irc_serv
set irc_serv = "INSTALL_IRCSERV = $irc_serv"
set irc_flush_ = $irc_flush
set irc_flush = "INSTALL_IRCFLUSH = $irc_flush"
set irc_wserv_ = $irc_wserv
set irc_wserv = "INSTALL_WSERV = $irc_wserv"
set irclib_ = $irclib
set irclib = "IRCII_LIBRARY = $irclib"
set defserver = "#define DEFAULT_SERVER "\""$defserver"\"
echo -n "Making changes to Makefile and config.h files now.."
sed -e "s;^INSTALL_EXECUTABLE.*;$irc_executable;"\
    -e "s;^INSTALL_IRCSERV.*;$irc_serv;"\
    -e "s;^INSTALL_IRCFLUSH.*;$irc_flush;"\
    -e "s;^INSTALL_WSERV.*;$irc_wserv;"\
    -e "s;^IRCII_LIBRARY.*;$irclib;" Makefile.bak > Makefile
echo -n ".."
sed -e "s;^#define DEFAULT_SERVER.*;$defserver;"\
    -e "s/^#define.*DEFAULT_HELP_SERVICE */\/\*#define DEFAULT_HELP_SERVICE /"\
    -e "s;^/\*.*\(#define DEFAULT_HELP_SERVICE "\""$help_service"\""\);\1;" \
	config.h.bak > config.h
echo ".."
# Implementing the help service shit is help on earth..but it WORKS!!!.

echo "Ok.. It's time to compile.  Are you sure you got everything correct"
echo "by this point?  If no, then answering 'no' at the next prompt will"
echo "leave this script, and you can try the whole thing over again."
echo -n "Are you ready to try to compile <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Ok, Maybe it wasn't time.  If you want to try again,"
	echo "just execute this script again the same way you did before"
	echo "and answer 'yes' here next time"
	exit(0)
endif
echo "Making...."
make
if ($status != 0) then
cat << END_OF_SPEECH
/-\|/-\|/-\|/-\|/-\|/-\|/-\|= ERROR =|/-\|/-\|/-\|/-\|/-\|/-\|/-\
There appear to have been errors in the compile which could have been caused by 
anything number of things from an odd machine or OS to running this config 
script with bad options.  A syntax error probably means you messed up one of 
the options.  'Text file busy' means you need to delete the old  source/irc 
file before you run this script.  Ignore 'warning's such as Illegal combination 
of pointer and int as they mean very little.

I suggest you take a look through the above compiler output and see if you can 
spot any problems.  If nothing is real obvious you should run this script again 
and make sure it is all right.  Otherwise talk to someone on irc or your local 
C guru and see what you can find out"
END_OF_SPEECH
	exit(1)
endif
clear
echo "-----------------------------------------------------------------------"
echo "Ok.. It appears to have compiled ok.  'Warning's you can ignore for"
echo "for the most part."
echo " "
echo "Now you need to decide if you are really ready to install the various"
echo "files and which ones you want and need."

echo "You can try to install everything at once right now or walk through"
echo "the various choices.  If you just hit return I will assume you want"
echo "to go through them one by one.  Type 'yes' to try to install it all."
echo -n "Try to install everything now <yes/no> [no] ?"
set answer = $<
if ("$answer" == "yes") then
	echo "Installing everything now...."
	make installeverything
else

echo "You do need to install the irc binary"
echo -n "Do this now <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Skipping irc binary"
else
	echo "Installing binary at $irc_executable_"
	make install
endif
echo "You also need to install the script directory"
echo -n "Do this now <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Skipping script directory"
else
	echo "Installing script directory at $irclib_/script"
	make installscript
endif
echo "You DO need to install the irc translation directory"
echo -n "Do this now <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Skipping translation directory"
else
	echo "Installing translation directory at $irclib_/translation"
	make installtranslation
endif

echo "You do not need to install the 'ircserv' program and this can"
echo "be skipped but can be useful if you use ^Z to shell escape alot"
echo -n "Do this now <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Skipping ircserv program"
else
	echo "Installing ircserv binary at $irc_serv_"
	make installserv
endif

echo "You also do not need to install the 'ircflush' program unless you"
echo "plan to run various C programs and such from inside irc for long"
echo "periods of time, such as robots."
echo -n "Do this now <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Skipping ircflush program"
else
	echo "Installing ircflush binary at $irc_flush_"
	make installflush
endif

echo "The wserv program is to allow you to use the /window create "
echo "function which works with screen(1) or X terminals.  If you"
echo "don't use either of these environments often, then it probably"
echo "isn't necessary."
echo -n "Do this now <yes/no> [yes] ?"
set answer = $<
if ("$answer" == "no") then
	echo "Skipping wserv program"
else
	echo "Installing wserv binary at $irc_wserv_"
	make installwserv
endif

# This is the endif for installeverything
endif

# All done.. 
clear
cat << 'LAST_SPEECH'
ALL DONE!!!!!!!   Yippy Yah.. 
The only thing you need to do now, is make sure that the directory where
you installed the irc binary is in your 'path' variable which is ussually
defined in your ~/.cshrc or ~/.login file.  A common default is for them 
to automatically put ~/bin  or ($HOME)/bin  in your path when your account
is created so that is why the defaults for this script use the 'bin'
directory.  Once you are sure you have the path variable set up, you need to 
make sure your .cshrc and .login file have been loaded with the new path.
Use     source .cshrc; source .login     to reload them.
Then type 'rehash' at the prompt and you should be ready to use irc.

I STRONGLY suggest that you go and get the ircII help files from your
local monster irc ftp site and install them
The increase in speed and ease of use when they are installed is well
worth the added size
'LAST_SPEECH'

echo "Exiting......"
exit(0)
