#!/bin/sh

#
# Script to install admin mod v2.x
# Alfred Reynolds (alfred@mazuma.net.au) 
# Florian Zschocke (darope@adminmod.org)
# Copyright (C) 2000-2004
#


#
# debugging
#
# set to doit="echol"  for debugging
# set to doit=  for execution
#
doit=
debug=


# clear the screen
clear

# make sure we are running with bash
if [ x"$BASH" = 'x' ] ; then
    # we are not running as bash, probably xBSD
    # check if we have one
    am_bash=`(bash --version) 2> /dev/null`
    if [ x"$am_bash" = 'x' ] ; then
	echo "This is a script for the Bash. You need to have the Bash"
	echo "shell installed in order to run this script. Please install"
	echo "the Bash (Bourne Again SHell) before running this script."
	exit
    else
	exec bash $0 "$@"
    fi
fi



# check if we run under bash version 1
# if so, we have to disable some features
if [ x"${BASH_VERSION%%.*}" = 'x1' -o x"${BASH_VERSION%%.*}" = 'x' ] ; then
	echo " "
	echo "************************** NOTICE ********************************"
	echo "   You are running an old and outdated version version of Bash."
	echo "   It is recommended that you upgrade your Bash to version 2 or"
	echo "   higher. I will have to disable some features of this installer"
	echo "   in order to work with your Bash version. Consider updating"
	echo "   your Bash."
	echo "******************************************************************"
	echo " "
	echo " "
	
	# set a variable that we aren't runnig bash 2 or higher
	bashver1=1
fi 	


# check if we are called from a package or from the source tree
if [ -d "commonlib" ] ; then
	echo "Detecting source install... ok"
	sourceInst=1
fi

FALLBACK_VERSION="2.50.50"

# Check if we run on an AMD64 machine
arch=`uname -m`
if [ x"$arch" = "xx86_64" ] ; then
	modarch="_amd64"
	amxext="amx64"
else
	modarch="_i386"
	amxext="amx"
fi


TMPDIR=${TMPDIR:-/tmp}
TEMPFILE=${TMPDIR}/tmp.admin.$$

liblist_gam_extra="gamedll_linux \"dlls/metamod_i386.so\""
hlds_exec="hlds"

if [ -n "$sourceInst" ] ; then
	amDBdir="tools/amdd"
	dotFile=""
else
	amDBdir=".amdd"
	fdot="."
fi
xdbFile="xdbbase"
amidb="amidb.xml"

ULF_base="$amDBdir/update."

mmBaseDir="addons/metamod"
mmDllDir="dlls"
mmDso="metamod${modarch}.so"

amBaseDir="addons/adminmod"
amDllDir="dlls"
amConfDir="config"
amAmxDir="scripts"
amDataDir="data"
amDso="admin_MM${modarch}.so"
amConfigFile="adminmod.cfg"

if [ -n "$sourceInst" ] ; then
	myAMdllDir="dlls/adminmod"
	myAMXdllDir="dlls/amxdll"
	myMMdllDir="../metamod/dlls"
else
	myAMdllDir="dlls"
	myAMXdllDir="dlls"
	myMMdllDir="dlls"
fi

# 
# STEP 1
#
# Check for needed utilities
#
# this the return value of a function
__ret=""

# fill the findpath
findpath=""
ifs=$IFS
IFS=:
for adir in ${UNSET:-/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/bin} ; do
	adddir=$adir
	for dir in $PATH ; do
		if [ "$dir" = "$adddir" ] ; then
			adddir=""
			break
		fi
	done
	[ -d "$adddir" ] && findpath="$findpath $adddir"
done
IFS=$ifs


# build a $echoE variable that 
# will allow the echoe procedure to work well.
FindEchoE()
{
   # build a list of potential echos, starting with sh built-in
   elist="echo"
	local ifs=$IFS
	IFS=":"
   for dir in $PATH; do
      [ -x "$dir/echo" ] && elist="$elist $dir/echo"
   done
	IFS=$ifs

   # work through list, see if one of them groks -e
   for echo in $elist; do
      result=`$echo -e hello\tworld`
      if [ "$result" != "-e hello" -a "$result" != "hello\tworld" ]; then
         echoE="$echo -e"
         break
      fi
   done
} 


# echo which interprets tab
echoe()
{
	if [ "$echoE" != "" ] ; then
		$echoE "$*"
	else
		# this works since we require sed to be available anyway.
		echo "$*" | $sed 's/\\t/	/g'
	fi
} 



# build a $echoN variable and $echoEnd combination that 
# will allow the echon procedure to work well.
FindEchoN()
{
   # build a list of potential echos, starting with sh built-in
   echoEnd=""
   elist="echo"
	local ifs=$IFS
	IFS=":"
   for dir in $PATH; do
      [ -x "$dir/echo" ] && elist="$elist $dir/echo"
   done
	IFS=$ifs

   # work through list, see if one of them groks -n
   for echo in $elist; do
      result=`$echo -n hello`
      if [ "$result" != "-n hello" ]; then
         echoN="$echo -n"
         break
      fi
   done
   if [ "$echoN" = "" ]; then
      echoN=echo
      echoEnd="\c"
      result=`$echoN "testing"$echoEnd`
      if [ "$result" != "testing" ]; then
         unset echoEnd
      fi
   fi
} 

# echo without return
miln=0
echon()
{
	$echoN "$*$echoEnd"
	miln=1
} 


# echo with newline first
echol() {
	if [ $miln -eq 1 ] ; then
		echo ""
		miln=0
	fi
	echo $*
}


FindEchoN
FindEchoE
# assume tab width of 8
tw=8

# A function to fail on missing required commands
function req_miss() {
	echol ""
	echo " ERROR"
	echo "  I cannot find the program $1 which is needed by this"
	echo "  installation script. Your Linux installation seems to"
	echo "  be incomplete. I'm sorry, but you either have to install"
	echo "  the program $1 (which should be included in every"
	echo "  standard Linux installation) or follow the directions"
	echo "  in the manual to install Admin Mod by hand."
	echo " "
	exit 1
}



# have a function to check for commands
locate=""
find=""
function check_cmd() {
	local loc
	local list
	local file
	__ret=""
	if [ $# -lt 1 ] ; then
		return 1
	fi
	# check in path
	loc=`type -p $1`
	if [ x"$loc" != x ] ; then
		__ret=$loc
		return 0
	fi

	loc=`type -t $1`
	if [ x"$loc" = xalias ] ; then
		__ret=$1
		return 0
	fi

	# check with find
	if [ x"$find" != x ] ; then
		list=`$find $findpath -name $cmd 2> /dev/null`
		for file in $list; do
			if [ -x $file ] ; then
				__ret=$file
				return 0
			fi
		done
	fi

	# check with locate
	if [ x"$locate" != x ] ; then
		list=`$locate -q -r "bin.*/${cmd}\$" 2> /dev/null`
		for file in $list; do
		echo "checking $file"
			if [ -x $file ] ; then
				__ret=$file
				return 0
			fi
		done
	fi

	return 1
}


# a function to check if one version is older than another
function is_older_than() {
	local first=$1
	local second=$2
	local major1
	local minor1
	local patch1
	local major2
	local minor2
	local patch2
	local rest



	# older versions have no version string
	if [ ! -z "$first" -a  -z "$second" ] ; then
		# second is older since it has no version string. return false
		return 1
	elif [ -z "$first" -a ! -z "$second" ] ; then
		# first is older since it has no version string. return true
		return 0
	elif [ -z "$first" -a -z "$second" ] ; then
		# no version strings available. return false.
		return 1
	fi


	if ( echo $first | $grep '[0-9][0-9]*\.[0-9][0-9]' ) >/dev/null 2>&1 ; then
		if ( echo $second | $grep '[0-9][0-9]*\.[0-9][0-9]' ) >/dev/null 2>&1 ; then
			# this number is of the format xx.yy.zz
			major1=`echo $first | $sed 's/^\([0-9][0-9]*\).*/\1/'`
			rest=`echo $first | $sed 's/^\([0-9][0-9]*\)\.\{0,1\}\(.*\)/\2/'`
			minor1=`echo $rest | $sed 's/^\([0-9][0-9]*\).*/\1/'`
			rest=`echo $rest | $sed 's/^\([0-9][0-9]*\)\.\{0,1\}\(.*\)/\2/'`
			patch1=`echo $rest | $sed 's/^\([0-9][0-9]*\).*/\1/'`

			[ -z "$major1" ] && major1=0
			[ -z "$minor1" ] && minor1=0
			[ -z "$patch1" ] && patch1=0

			# this number is of the format xx.yy.zz
			major2=`echo $second | $sed 's/^\([0-9][0-9]*\).*/\1/'`
			rest=`echo $second | $sed 's/^\([0-9][0-9]*\)\.\{0,1\}\(.*\)/\2/'`
			minor2=`echo $rest | $sed 's/^\([0-9][0-9]*\).*/\1/'`
			rest=`echo $rest | $sed 's/^\([0-9][0-9]*\)\.\{0,1\}\(.*\)/\2/'`
			patch2=`echo $rest | $sed 's/^\([0-9][0-9]*\).*/\1/'`

			[ -z "$major2" ] && major2=0
			[ -z "$minor2" ] && minor2=0
			[ -z "$patch2" ] && patch2=0


			# compare major
			if [ "$major1" -gt "$major2" ] ; then
				return 1 # first is not older. return false
			elif [ "$major1" -lt "$major2" ] ; then
				return 0 # first is older. return true
			fi

			# major is equal, compare minor
			if [ "$minor1" -gt "$minor2" ] ; then
				return 1 # first is not older. return false
			elif [ "$minor1" -lt "$minor2" ] ; then 
				return 0 # first is older. return true
			fi

			# minor a equal, compare patch
			if [ "$patch1" -gt "$patch2" ] ; then
				return 1 # first is not older. return false
			elif [ "$patch1" -lt "$patch2" ] ; then
				return 0 # first is older. return true
			fi

			# versions are equal, return true. 
			# not quite correct but serves our purpose
			return 0
		fi

	fi

	# No xx.yy.zz format. Lets compress the version string and compare then.
	major1=`echo $first | $sed 's/[^0-9]//g'`
	major2=`echo $second | $sed 's/[^0-9]//g'`

	if [ "$major1" -lt "$major2" ] ; then
		return 0  # first is older
	fi

	# first is not older or we failed. return false.
	return 1

}




echon "Setting up installation...."

# locate locate
if check_cmd "locate" ; then
	locate="$__ret"
fi

# find find
if check_cmd "find" ; then
	l_fres=`$__ret ${__ret%find} -name ${__ret#${__ret%find}}`
	if [ x"$l_fres" = "x$__ret" ] ; then
		find="$__ret"
	fi
fi



# we need sed
if check_cmd "sed" ; then
	l_fres=`echo "this is a string" | $__ret -e 's/is //g' -e 's/a *\(string\)/at \1/'`
	if [ "$l_fres" = "that string" ] ; then
		sed="$__ret"
		#echol "we got $cmd: $sed"
	else
		req_miss "'sed'"
	fi
else
	req_miss "'sed'"
fi

# we need grep
if check_cmd "grep" ; then
	l_fres=`echo "this test string" | $__ret 'test'`
	if [ x"$l_fres" = "xthis test string" ] ; then
		grep="$__ret"
		#echol "we got $cmd: $grep"
	else
		req_miss "'grep'"
	fi
else
	req_miss "'grep'"
fi

# we need cat
if check_cmd "cat" ; then
	if ( $__ret $0 | $grep IdentTest ) >/dev/null 2>&1 ; then
		cat="$__ret"
		#echol "we got $cmd: $cat"
	else
		req_miss "'cat'"
	fi
else
	req_miss "'cat'"
fi

# we need cmp or diff
if check_cmd "cmp" ; then
	if $__ret -s $0 $0 >/dev/null 2>&1 ; then
		cmp="$__ret -s"
		#echol "we got $cmd: $cat"
	elif check_cmd "diff" ; then
		if $__ret -q $0 $0 >/dev/null 2>&1 ; then
			cmp="$__ret -q"
		else
			req_miss "'cmp'"
		fi
	else
		req_miss "'cmp'"
	fi
elif check_cmd "diff" ; then
	if $__ret -q $0 $0 >/dev/null 2>&1 ; then
		cmp="$__ret -q"
	else
		req_miss "'diff'"
	fi
else
	req_miss "'diff'"
fi

# we need cp
if check_cmd "cp" ; then
	cp="$__ret -f"
	#echol "we got: $cp"
else
	req_miss "'cp'"
fi

# we need rm
if check_cmd "rm" ; then
	rm="$__ret -f"
	#echol "we got: $rm"
else
	req_miss "'rm'"
fi

# we need mv
if check_cmd "mv" ; then
	mv="$__ret"
	#echol "we got: $mv"
else
	req_miss "'mv'"
fi

# we need mkdir
if check_cmd "mkdir" ; then
	pushd /tmp > /dev/null 
	$__ret -p this/that
	$__ret -p this/that
	popd > /dev/null
	if [ -d /tmp/this/that ] ; then
		mkdir="$__ret"
		$rm -rf /tmp/this
	else
		req_miss "'mkdir' supporting option '-p'"
	fi
	#echol "we got: $mkdir"
else
	req_miss "'mkdir'"
fi

# we check expr
if check_cmd "expr" ; then
	expr="$__ret"
	#echol "we got $cmd: $expr"
else
	#echol "we aint got $cmd"
:
fi

# we need basename
if check_cmd "basename" ; then
	basename="$__ret"
	#echol "we got $cmd: $basename"
elif [ x"$expr" != x ] ; then
	basename="Basename"
else
	req_miss "'basename' or 'expr'"
fi

# we need dirname
if check_cmd "dirname" ; then
	dirname="$__ret"
	#echol "we got $cmd: $dirname"
elif [ x"$expr" != x ] ; then
	dirname="Dirname"
else
	req_miss "'dirname' or 'expr'"
fi


# we check for ident
if check_cmd "ident" ; then
	$IdentTest: install $
	if ( $__ret $0 | $grep IdentTest ) >/dev/null 2>&1 ; then
		ident="$__ret"
		#echol "we got ident: $ident"
	fi
else
	#echol "we aint got ident"
:
fi


# we check for strings
if check_cmd "strings" ; then
	if ( $__ret $0 | $grep StringsTest ) >/dev/null 2>&1 ; then
		strings="$__ret"
		#echol "we got strings: $strings"
	fi
else
	#echol "we aint got strings"
:
fi


# we check for tail
if check_cmd "tail" ; then
	l_fres=`$cat $0 | $__ret -n 1`
	if [ "$l_fres" = "#this_is_the_last_line" ] ; then
		tail="$__ret"
		#echol "we got tail: $tail"
	fi
else
	#echol "we aint got tail"
:
fi




#
# In case dirname is not on system
#
Dirname () {
   $expr ${1-.}'/' : '\(/\)[^/]*//*$' \
        \| ${1-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
        \| .
}
 
#
# In case basename is not on system
#
Basename () {
   $expr ${1} : '.*//*\([^/][^/]*$\)' \
        \| $1
}
 
FindTopDir() {
 
   command=$1
 
   case "$command" in
      /*)
         foundtopdir=`$dirname $command`
         foundtopdir=`$dirname $foundtopdir`
         ;;
      ../*)
         foundtopdir=`pwd`/`$dirname $command`
         foundtopdir=`(cd ${foundtopdir}/.. ; pwd )`
         ;;
      ./*)
         foundtopdir=`$dirname $command`
         if [ "$foundtopdir" = "" ]; then
            foundtopdir=`pwd`
         else
            foundtopdir=`pwd`/`expr ${foundtopdir} : '\.\/\(.*\)'`
         fi
         foundtopdir=`(cd ${foundtopdir}/.. ; pwd )`
         ;;
      *)
         foundtopdir=`pwd`
         ;;
   esac

 
   if [ ! -f "$foundtopdir"/$hlds_exec ]; then
      foundtopdir="no default"
   fi
}



# check if a file ends properly with a newline or if it is a broken DOS file.
function file_ends_properly() {
	local line

	# only works i we got 'tail'. Otherwise return false.
	[ -z "$tail" ] && return 1

	echo "newline" > /tmp/amTMP.$$_2
	line=`$cat $1 /tmp/amTMP.$$_2 | $tail -n 1`
	$rm -f /tmp/amTMP.$$_2

	if ( echo $line | $grep "^newline" ) >/dev/null 2>&1 ; then
		return 0  # return true
	else
		return 1  # return false
	fi
}



#
# compile the plugins in a plugin source directory
function compile_plugins() {
	local res

	pushd $1 >/dev/null 2>&1
	./compile_all
	res=$?

	popd >/dev/null 2>&1
	return $res
}




 
#
# STEP 2
#
# Lets get oriented where we are and if we are complete
#
FindTopDir $0
 

MYDIR=`$dirname $0`

progname=`$basename $0`


# Get the version numbers from the versions file
if [ -n "$sourceInst" -o ! -f $amDBdir/versions ] ; then
	if [ ! -z "$ident" ] ; then  # use ident
		MMver=`$ident $MYDIR/$myMMdllDir/$mmDso 2>/dev/null | $grep Pg: | $sed 's/.*-- \([0-9][0-9]*\.[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\) |.*/\1/'`
	elif [ ! -z "$strings" ] ; then # use strings
		MMver=`$strings $MYDIR/$myMMdllDir/$mmDso | $grep '\$Pg: Metamod.* \\$' |  $sed 's/.*-- \([0-9][0-9]*\.[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\) .*/\1/'`
	else
		MMver=`$grep -a "\$Pg: Metamod --" $MYDIR/$myMMdllDir/$mmDso |  $sed 's/.*Metamod -- \([0-9][0-9]*\.[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\) \|.*/\1/g'`
	fi

	if [ x"$MMver" != 'x' ] ; then
		if [ -z "$sourceInst" ] ; then
			echo "MM_VERSION=\"$MMver\"" > $amDBdir/versions
		else 
			MM_VERSION="$MMver"
			echon " Metamod version $MM_VERSION  "
		fi
	else 
		echon " Metamod version UNKNOWN  "
	fi

	if [ ! -z "$ident" ] ; then  # use ident
		AMver=`$ident $MYDIR/$myAMdllDir/$amDso 2>/dev/null | $grep Pg: | $sed 's/.*-- \(b\{0,1\}[0-9][0-9]*\.\{0,1\}[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\).*/\1/'`
	elif [ ! -z "$strings" ] ; then # use strings
		AMver=`$strings  $MYDIR/$myAMdllDir/$amDso | $grep '\$Pg: Admin Mod.* \\$' | $sed 's/.*-- \(b\{0,1\}[0-9][0-9]*\.\{0,1\}[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\).*/\1/'`
	else
		AMver=`$grep -a "\$Pg: Admin Mod --" $MYDIR/$myAMdllDir/$amDso |  $sed 's/.*-- \(b\{0,1\}[0-9][0-9]*\.\{0,1\}[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\).*/\1/'`
	fi

	if [ x"$AMver" != 'x' ] ; then
		if [ -z "$sourceInst" ] ; then
			echo "AM_VERSION=\"$AMver\"" >> $amDBdir/versions
		else 
			AM_VERSION="$AMver"
			echon " Admin Mod version $AM_VERSION  "
		fi
	else 
		echon " Admin Mod version UNKNOWN  "
	fi
fi

source $amDBdir/versions
VERSION=${AM_VERSION:-$FALLBACK_VERSION}


if [ -n "$bashver1" ] ; then
	echo " ok (limited functionality)"
else
	echo " ok "
fi


if [ "$1" = "-h" ]; then
	echo " "
	echo "Usage: $progname [mod_name]";
	echo "  where 'mod_name' is the directory name of the MOD";
	echo "  to install Admin Mod for (e.g. cstrike)";
	echo " "
	exit;
fi

if [ $# -gt 0 ] ; then
	mod_name=`$basename $1 2> /dev/null`
	premod_dir=`$dirname $1 2> /dev/null`
else
	mod_name=""
	premod_dir=""
fi








#
# Check for our files
#
echo ""

$cat <<EOF
               Admin Mod Installation Script
                       Version $VERSION

   This script will install Admin Mod version $VERSION on your
   system. It will ask you for the directory that you installed
   the Half-Life server in. To accept defaults just press Return.
   After the binaries are installed you should read the docu-
   mentation and configure Admin Mod before you use it for the 
   first time.

EOF


echon " Shall I proceed with the installation? (y/n) [y] "
read answer
if [ "$answer" = "n" ] ; then
  echo""
  exit
fi


if [ ! -d "${MYDIR}/${myAMdllDir}" ]; then
	echo " "
	echo " "
	echo " Couldn't find Admin Mod dlls. Your package is incomplete. ";
	echo " Try redownloading it from http://www.adminmod.org. Exiting.";
	echo " "
	exit;
fi

if [ ! -f "${MYDIR}/scripting/binaries/plugin_base.${amxext}" ]; then
	
	echo " "
	echo " "
	echo "  Couldn't find the base plugin. You have to compile the plugins before ";
	if [ -n "$sourceInst" ] ; then
		echo "  installing Admin Mod. I will now change into the scripting/examples"
		echo "  directory and attempt to compile all plugins."
		echo " "
		echon "Shall I proceed to compile the plugins? (y/n) [y] "
		read answer
		if [ "$answer" = "n" ] ; then
			echo "  Please change into the scripting/examples directory and execute"
			echo "  the compile_all script to compile the base plugins."
			echo "  Exiting."
			exit;
		else
			echo " "
			if compile_plugins "scripting/examples" ; then
				echo "  Everything went smoothly, will now continue."
			else 
				echo " "
				echo "ERROR"
				echo "  An error occured while compiling the plugins";
				echo "  Please change into the scripting/examples directory and execute"
				echo "  the compile_all script to compile the base plugins."
				echo "  Exiting."
				echo " "
				exit;
			fi
		fi
			
	else
		echo "  installing Admin Mod. Go into the scripting/examples directory";
		echo "  and execute ./compile_all.";
		echo "  Exiting.";
		echo " "
		exit;
	fi
fi



echo ""
echon "Reading installation configuration data..."
#
# Check if we are using the xdb
# 
if [ -z "$bashver1" -a -f $amDBdir/${fdot}$xdbFile ] ; then
	. $amDBdir/${fdot}$xdbFile
	#echo "Set up XDB functions"
	xdb_on=1

	# check if we already have an amidb available
	if [ -f $amDBdir/$amidb ] ; then
		if xdb_parse_full_tree $amDBdir/$amidb ; then 
			amidb_on=1
		fi
	fi
else
	echo "No XDB support"
fi


#
# Check if we might want to go into update mode
#

# find the last update file. Easy, just run through all and 
# reassign the variable
echon "..."
update_file=""
for file in ${ULF_base}* ; do
	update_file=$file
done


echo " done"



#
# STEP 3
#
# Check for the HLDS and MOD directory
#

echon "Trying to locate your HLDS..."


if [ "$foundtopdir" = "no default" ] ; then

	# Check if we have a server we already installed for
	if [ ! -z "$amidb_on" ] ; then
		if xdb_find_server '*' ; then
			for serv in ${xdb_funcret} ; do
				xdb_get_server_path $serv
				if [ ! -z "$xdb_funcret" -a -x $xdb_funcret/$hlds_exec ] ; then
					first_path=$xdb_funcret
					listed_server=$serv
					break
				fi
			done
		fi
	fi
	if [ -z "$first_path" ] ; then
		first_path="/data/AMFiles/hlds_l"
	fi

  if [ -x $first_path/$hlds_exec ] ; then
    TOPDIR="$first_path"
  elif [ -x /usr/local/games/hlds_l/$hlds_exec ] ; then
    TOPDIR="/usr/local/games/hlds_l"
  elif [ -x /usr/local/hlds_l/$hlds_exec ] ; then
	TOPDIR="/usr/local/hlds_l"
  elif [ -x /home/halflife/hlds_l/$hlds_exec ] ; then
    TOPDIR="/home/halflife/hlds_l"
  elif [ -x /home/hlds_l/$hlds_exec ] ; then
    TOPDIR="/home/hlds_l"
  else
	if [ ! -z "$locate" ] ; then
		hlds=`$locate -r 'hlds_l/hlds$' 2> /dev/null | head -1`
			if [ -x "$hlds" ] ; then
				TOPDIR=`$dirname $hlds`
			else
				TOPDIR=""
			fi
	else
		TOPDIR=""
	fi
  fi
else
  TOPDIR=$foundtopdir
fi

if [  ! -z "$premod_dir" -a  x"$premod_dir" != 'x.' ]  ; then
  TOPDIR=$premod_dir
fi

echo " done"

echo ""
echo " Please enter the directory where your HL server is installed,"
echo " or hit Return to accept the default."
echon " [$TOPDIR]: "
read topdir

if [ "$topdir" = "" ] ; then
	topdir=$TOPDIR
fi

if [ ! -d "$topdir"  ] ; then
	echo ""
	echo " I could not locate a HLDS installation under $topdir."
	echo " You'll have to tell me where you have the HLDS installed,"
	echo " or I won't be able to install Admin Mod for you."
	exit 2
fi


# If we have that server in the DB set it as active
if [ ! -z "$listed_server" ] ; then
	xdb_get_server_path $listed_server
	if [ "x$xdb_funcret" != "x$topdir" ] ; then
		# add this server to the servers list
		xdb_add_server $topdir
	fi
elif [ ! -z "$xdb_on" ] ; then
	xdb_add_server $topdir
fi

#
# STEP 4
# 
# Let's check which MODs he has installed.
#

clear

# if we have a mod given, we don't need to check for others

if [ -z "$mod_name" ] ; then
	# change into the hlds dir
	pushd $topdir > /dev/null 2>&1
	
	i=1
	for dir in `ls -1` ; do
		if [ -d "$dir" ] ; then
			if [ -f $dir/liblist.gam ] ; then
				if [ -n "$bashver1" ] ; then
					mods="$mods$dir "
				else
					mods[$i]=$dir
					desc[$i]=`$grep -i "^game " $dir/liblist.gam | $sed 's/game *"\(..*\)".*/\1/'`
					indices[$i]=$i
				fi
				i=$((i+1))
			fi
		fi
	done

	if [ $i -gt 1 ] ; then
		if [ -n "$bashver1" ] ; then
			echo ""
			echo " You seem to have the following MODs installed:"
			echo "   => $mods<="

			echo ""
			answer=""
			
			while [ x"$answer" = x ] ; do
				echo " Please enter the MOD you want to" 
				echon " install Admin Mod for (q to quit):  "
				read answer
				if [ x"$answer" != 'x' -a x"$answer" != 'xq' ] ; then
					ansvalid=`echo "$mods" | $grep "$answer "`
					if [ -z "$ansvalid" ] ; then
						answer=""
					fi
				fi
			done

			if [ x"$answer" = xq ] ; then
				exit
			fi

			mod_name="$answer"
			mod_descr="$answer"

		else
			echo ""
			echo " You seem to have the following MODs installed:"
			echoe "\tMOD\t\tDescription"
			echoe " ----------------------------------------------"
			for elem in ${indices[*]} ; do
				if [ ${#mods[$elem]} -lt $tw ] ; then
					echoe "  [$elem]:\t${mods[$elem]}\t\t${desc[$elem]}"
				else
					echoe "  [$elem]:\t${mods[$elem]}\t${desc[$elem]}"
				fi
			done
		
			echo ""
			answer=""
			while [ x"$answer" = x ] ; do
				echo " Please choose the number of the MOD you" 
				echon " want to install Admin Mod for (q to quit):  "
				read answer
				# if the user entered something and it is not 'q' check his answer
				if [ x"$answer" != 'x' -a x"$answer" != 'xq' ] ; then
					# check if he entered the name of the mod
					for elem in ${indices[*]} ; do
						if [ "${mods[$elem]}" = "$answer" ] ; then
							answer="$elem"
							break
						fi
					done
					# check if answer is a number
					ansvalid=`echo "$answer" | $sed -e 's/^[0-9]*$/yes/'`
					if [ x"$ansvalid" = 'xyes' ] ; then
						# make sure the number is in the range of available MODs
						if [ $answer -ge $i ] ; then
							answer=""
						fi
					else
						answer=""
					fi
				fi
			done

			if [ x"$answer" = xq ] ; then
				exit
			fi

			mod_name=${mods[$answer]}
			mod_descr="${desc[$answer]}"
		fi

	else
		echo " I couldn't find any MODs installed. Could you please try to run"
		echo " this script with the directory of the mod as an parameter?"
		echo " (See $0 -h)"
		exit
	fi

	popd > /dev/null 2>&1
else
	mod_descr="$mod_name"
fi

echo ""
MODDIR=${topdir}/${mod_name}


mod_dlls="${MODDIR}/dlls";
mod_liblist="${MODDIR}/liblist.gam";


if [ ! -f $mod_liblist ]; then
  echo ""
  echo " Couldn't find mod's liblist.gam (${mod_liblist}).";
  echo " Please follow the manual install instructions found";
  echo " in the documentation. Exiting";
  exit;
fi


echo ""
echo " Looks like you have $mod_descr installed in"
echo " $MODDIR."
echon " I can now install the Admin Mod files. Proceed? (y/n) [y] "
read answer
if [ "$answer" = "n" ] ; then
 echo "";
 exit;
fi





#
# STEP 5
#
# Installation / Update of Metamod.
#

# check if we already have a metamod installed.
gamedll=`$grep '^ *gamedll_linux' $mod_liblist | $sed -e 's/.* "\(..*\)".*/\1/'`

if ( echo $gamedll | $grep "metamod" ) >/dev/null 2>&1 ; then
	oldMMdso=`$basename $gamedll`
	oldMMdir=`$dirname $gamedll`
	gamedll=$MODDIR/$gamedll
	oldMMactive="yes"
elif [ -f $MODDIR/$mmBaseDir/$mmDllDir/$mmDso ] ; then  # maybe it is installed, but not activated
	oldMMdso=$mmDso
	oldMMdir=$mmBaseDir/$mmDllDir
	gamedll=$MODDIR/$mmBaseDir/$mmDllDir/$mmDso
fi

echo ""
echo ""
install="inst:inst"
# check if the installed version is older than ours.
if [ ! -z "$oldMMdso" ] ; then
	if [ ! -z "$ident" ] ; then  # use ident
		oldMMversion=`$ident $gamedll 2>/dev/null | $grep Pg: | $sed 's/.*-- \([0-9][0-9]*\.[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\) |.*/\1/'`
	elif [ ! -z "$strings" ] ; then # use strings
		oldMMversion=`$strings $gamedll | $grep '\$Pg: Metamod.* \\$' |  $sed 's/.*-- \([0-9][0-9]*\.[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\) .*/\1/'`
	else
		oldMMversion=`$grep -a "\$Pg: Metamod --" $gamedll |  $sed 's/.*Metamod -- \([0-9][0-9]*\.[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\) \|.*/\1/g'`
	fi


	if [ -z "$oldMMversion" ] ; then 
		echo " I am unable to find out which version of Metamod you have currently installed."
		echo " I will assume that it is older that the one that comes with this package."
		echon " Shall I continue or quit (c/q)? [c]: "
		read answer
		if [ x"$answer" = xq ] ; then
			exit
		fi
		echo " "
		oldMMversion=""
	fi

	if is_older_than "$MM_VERSION" "$oldMMversion" ; then # the present version is newer
		if [ ! -z "$oldMMactive" ] ; then # if mm is active
			# do nothing
			echo " You have a newer or same version ($oldMMversion) of Metamod installed than"
			echo " the one that comes with this package ($MM_VERSION). I'm leaving that untouched."
			install=""
		else
			# just activate it
			echo " You have a newer or same version ($oldMMversion) of Metamod installed than"
			echo " the one that comes with this package ($MM_VERSION), but it is not activated."
 			echo " I will activate the version you have installed."
			install=":act"
		fi
	else  # our version is newer.
		if [ -z "$oldMMactive" ] ; then  # mm is present but not active
			echon " You have an old version "
			if [ ! -z "$oldMMversion" ] ; then echon "($oldMMversion) " ; fi
			echo " of Metamod installed but it is not active."
			echo " I will install a newer version ($MM_VERSION) and activate it."
			echon " Shall I continue (y/n/q)? [y] "
			read answer
			if [ x"$answer" = xn ] || [ x"$answer" = xq ] ; then 
				exit
			fi
			install="inst:act"
		else
			echon " You have an older version "
			if [ ! -z "$oldMMversion" ] ; then echon "($oldMMversion) " ; fi
			echo " of Metamod installed than the one"
			echo " that comes with this package ($MM_VERSION)."
			echon " Should I update your Metamod with the new version (y/n/q)? [y] "
			read answer

			if [ x"$answer" = xn ] ; then 
				install=""
			elif [ x"$answer" = xq ] ; then
				exit
			else
				if (echo $gamedll | $grep "addons/metamod") >/dev/null 2>&1 ; then 
					# the old version is in the standard path
					# only install new dso, no changes to liblist.gam needed.
					install="inst:"
				else
					# the old version is in a different path
					if [ x"$gamedll" = "xdlls/metamod${modarch}.so" -a -z "$oldMMversion" ] ; then
						# probably a pre-addons-standard old version.
						echo ""
						echo ""
						echo " Your version of Metamod dates back from before the introduction of the new"
						echo " addons directory standard. I would suggest to remove it and install the new"
						echo " version of Metamod into the new standard directory."
						echon " Should I use the new standard directory (y/n/q)? [y] "
						read answer
					else
						# maybe he wants his Metamod to be in a different directory than the standard?
						echo ""
						echo ""
						echo " The installation of your old Metamod version does not adhere to the new"
						echo " addons directory standard. You may keep your Metamod version installed"
						echo " where it is now or switch to the new standard location."
						echon " Should I upgrade your Metamod to use the new standard directory (y/n/q)? [y] "
						read answer
					fi

					if [ x"$answer" = xq ] ; then
						exit
					elif [ x"$answer" = xn ] ; then
						install="repl:"
					else
						install="updt:updt"
					fi

				fi
			fi

		fi
	fi
fi


if [ ! -z "$doit" ] ; then
	echo "install is $install"
fi

#
# Checking is over, let's get to work and install stuff for Metamod
#

if [ ! -z "$xdb_on" ] ; then
	xdb_print_xml_tree > $amDBdir/$amidb
fi

if (echo $install | $grep "inst:") >/dev/null 2>&1 ; then
	# install new mm into standard location
	echo ""
	echon "Installing Metamod binaries to $mod_name/$mmBaseDir..."
	$doit $mkdir -p $MODDIR/$mmBaseDir/$mmDllDir
	$doit $cp $MYDIR/${myMMdllDir}/$mmDso $MODDIR/$mmBaseDir/$mmDllDir
	mmPluginsFile="$MODDIR/$mmBaseDir/plugins.ini"
	echo " done."
elif (echo $install | $grep "updt:" ) >/dev/null 2>&1 ; then
	# install new mm into standard location and remove old one
	echo ""
	echon "Installing Metamod files to $mod_name/$mmBaseDir..."
	$doit $mkdir -p $MODDIR/$mmBaseDir/$mmDllDir
	$doit $cp $MYDIR/${myMMdllDir}/$mmDso $MODDIR/$mmBaseDir/$mmDllDir
	$doit $rm $gamedll
	$doit $cp $MODDIR/metamod.ini $MODDIR/$mmBaseDir/plugins.ini
	$doit $cp $MODDIR/metaexec.cfg $MODDIR/$mmBaseDir/exec.cfg > /dev/null 2>&1
	$doit $rm $MODDIR/metamod.ini
	$doit $rm $MODDIR/metaexec.cfg
	mmPluginsFile="$MODDIR/$mmBaseDir/plugins.ini"
	echo " done."
elif (echo $install | $grep "repl:" ) >/dev/null 2>&1 ; then
	# replace mm in old location with new DSO
	echo ""
	echon "Installing Metamod binaries..."
	$doit $cp $MYDIR/${myMMdllDir}/$mmDso $gamedll
	mmPluginsFile="$MODDIR/metamod.ini"
	echo " done."
fi 
	# otherwise we don't install any mm DSO


if (echo $install | $grep ":inst\|:updt" ) >/dev/null 2>&1 ; then
	echo ""
	echon "Updating your liblist.game file..."
	if [ ! -f ${mod_liblist}.am.orig ] ; then
		$doit $cp $mod_liblist ${mod_liblist}.am.orig
	fi
	
	if file_ends_properly $mod_liblist ; then
		$doit eval "$cat $mod_liblist | $sed -f ${MYDIR}/${amDBdir}/${fdot}instscr.sed > $TEMPFILE"
	else
		$doit eval "$cat $mod_liblist | $sed -f ${MYDIR}/${amDBdir}/${fdot}instscrDOS.sed > $TEMPFILE"
	fi

	$doit $cp $TEMPFILE $mod_liblist
	$doit $rm $TEMPFILE

	echo " done."
elif (echo $install | $grep ":act" ) >/dev/null 2>&1 ; then
	echo ""
	echon "Updating your liblist.game file..."
	if [ ! -f ${mod_liblist}.am.orig ] ; then
		$doit $cp $mod_liblist ${mod_liblist}.am.orig
	fi
	
	if file_ends_properly $mod_liblist ; then
		if ( $cat $mod_liblist | $grep "$mmDso" ) >/dev/null 2>&1 ; then
			$doit eval "$cat $mod_liblist | $sed -e 's/..*\(gamedll_linux..*metamod\)/\1/' > $TEMPFILE"
		else
			$doit eval "$cat $mod_liblist | $sed -f ${MYDIR}/${amDBdir}/${fdot}instscr.sed > $TEMPFILE"
		fi
	else
		if ( $cat $mod_liblist | $grep "$mmDso" ) >/dev/null 2>&1 ; then
			$doit eval "$cat $mod_liblist | $sed -e 's/..*\(gamedll_linux..*metamod\)/\1/' >$TEMPFILE"
			$doit eval "echo '' >> $TEMPFILE"
		else
			$doit eval "$cat $mod_liblist | $sed -f ${MYDIR}/${amDBdir}/${fdot}instscrDOS.sed > $TEMPFILE"
		fi
	fi

	$doit $cp $TEMPFILE $mod_liblist
	$doit $rm $TEMPFILE

	echo " done."
fi






#
# STEP 6
#
# Installation/Update of Admin Mod
#


# check if we already have an adminmod installed.
if [ -f $MODDIR/$mmBaseDir/plugins.ini ] ; then
# first check plugins.ini
	amdll=`$grep "^ *linux  *.*admin_MM${modarch}" $MODDIR/$mmBaseDir/plugins.ini | $sed -e 's/.* \([^ ][^ ]*\) *.\{0,1\}$/\1/'`
	if [ ! -z "$amdll" ] ; then
		oldAMdso=`$basename $amdll`
		oldAMdir=`$dirname $amdll`
		amdll=$MODDIR/$amdll
		oldAMactive="yes"
	fi
	# This is the one MM will be using, so we set the mmPluginsFile var here
	mmPluginsFile="$MODDIR/$mmBaseDir/plugins.ini"
elif [ -f $MODDIR/metamod.ini ] ; then
# second check for metamod.ini
	amdll=`$grep "^ *linux  *.*admin_MM${modarch}" $MODDIR/metamod.ini | $sed -e 's/.* \([^ ][^ ]*\) *.\{0,1\}$/\1/'`
	if [ ! -z "$amdll" ] ; then
		oldAMdso=`$basename $amdll`
		oldAMdir=`$dirname $amdll`
		amdll=$MODDIR/$amdll
		oldAMactive="yes"
	fi
	# This is the one MM will be using, so we set the mmPluginsFile var here
	mmPluginsFile="$MODDIR/$mmBaseDir/plugins.ini"
fi

if [ -z "$amdll" -a -e "$MODDIR/$amBaseDir/$amDllDir/$amDso" ] ; then
# maybe we have an installed but inactive dll
	oldAMdso=admin_MM${modarch}.so
	oldAMdir=$amBaseDir/$amDllDir
	amdll=$MODDIR/$amBaseDir/$amDllDir/$amDso
fi


echo ""
echo ""
installAM="inst:inst"
# check if the installed version is older than ours.
if [ ! -z "$amdll" ] ; then
	if [ ! -z "$ident" ] ; then  # use ident
		oldAMversion=`$ident $amdll 2>/dev/null | $grep Pg: | $sed 's/.*-- \(b\{0,1\}[0-9][0-9]*\.\{0,1\}[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\).*/\1/'`
	elif [ ! -z "$strings" ] ; then # use strings
		oldAMversion=`$strings $amdll | $grep '\$Pg: Admin Mod.* \\$' | $sed 's/.*-- \(b\{0,1\}[0-9][0-9]*\.\{0,1\}[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\).*/\1/'`
	else
		oldAMversion=`$grep -a "\$Pg: Admin Mod --" $amdll |  $sed 's/.*-- \(b\{0,1\}[0-9][0-9]*\.\{0,1\}[0-9][0-9]*\(\.\{0,1\}[0-9][0-9]*\)\).*/\1/'`
	fi

	if [ -z "$oldAMversion" ] ; then 
		echo " I am unable to find out which version of Admin Mod you have currently installed."
		echo " I will assume that it is older that the one that comes with this package."
		echon " Shall I continue or quit (c/q)? [c]: "
		read answer
		if [ x"$answer" = xq ] ; then
			exit
		fi
		echo " "
		oldAMversion=""
	fi


	if is_older_than "$AM_VERSION" "$oldAMversion" ; then # the installed version is newer
		if [ ! -z "$oldAMactive" ] ; then # if am is active
			# do nothing
			echo " You have a newer or same version ($oldAMversion) of Admin Mod installed than"
			echo " the one that comes with this package ($AM_VERSION). I'm leaving that untouched."
			installAM=""
		else
			# just activate it
			echo " You have a newer or same version ($oldAMversion) of Admin Mod installed than"
			echo " the one that comes with this package ($AM_VERSION), but it is not activated."
 			echo " I will activate the version you have installed."
			installAM=":act"
		fi
	else  # our version is newer.
		if [ -z "$oldAMactive" ] ; then  # am is present but not active
			echon " You have an old version "
			if [ ! -z "$oldAMversion" ] ; then echon "($oldAMversion) " ; fi
			echo " of Admin Mod installed but it is not active."
			echo " I will install a newer version ($AM_VERSION) and activate it."
			echon " Shall I continue (y/n/q)? [y] "
			read answer
			if [ x"$answer" = 'xn' -o x"$answer" = 'xq' ] ; then 
				exit
			fi
			installAM="inst:inst"
		else  # old am is present and active
			echon " You have an older version "
			if [ ! -z "$oldAMversion" ] ; then echon "($oldAMversion) " ; fi
			echo " of Admin Mod installed than the one"
			echo " that comes with this package ($AM_VERSION)."
			echon " Should I update your Admin Mod with the new version (y/n/q)? [y] "
			read answer

			if [ x"$answer" = xn ] ; then 
				installAM=""
			elif [ x"$answer" = xq ] ; then
				exit
			else
				if (echo $amdll | grep "$amBaseDir") >/dev/null 2>&1 ; then 
					# the old version is in the standard path
					# only install new dso, no changes to MM plugins.ini needed.
					installAM="inst:"
				else
					# the old version is in a different path
					if [ "$amdll" = "dlls/admin_MM${modarch}.so" -a -z "$oldAMversion"  ] ; then
						# probably a pre-addons-standard old version.
						echo ""
						echo ""
						echo " Your version of Admin Mod dates back from before the introduction of the new"
						echo " addons directory standard. I would suggest to remove it and install the new"
						echo " version of Admin Mod into the new standard directory."
						echon " Should I use the new standard directory (y/n/q)? [y] "
						read answer
					else
						# maybe he wants his Admin Mod to be in a different directory than the standard?
						echo ""
						echo ""
						echo " The installation of your old Admin Mod version does not adhere to the new"
						echo " addons directory standard. You may keep your Admin Mod version installed"
						echo " where it is now or switch to the new standard location."
						echon " Should I upgrade your Admin Mod to use the new standard directory (y/n/q)? [y] "
						read answer
					fi

					if [ x"$answer" = xq ] ; then
						exit
					elif [ x"$answer" = xn ] ; then
						installAM="repl:"
					else
						installAM="updt:updt"
					fi

				fi
			fi

		fi
	fi
fi

if [ ! -z "$doit" ] ; then
	echo "installAM is $installAM"
fi

#
# Ok, let's go install the AM files.

if (echo "$installAM" | grep "inst:") >/dev/null 2>&1 ; then
	# install new am into standard location
	echo ""
	echo ""
	echon "Installing Admin Mod files to $mod_name/$amBaseDir ..."
	$doit $mkdir -p $MODDIR/$amBaseDir/$amDllDir
	$doit $mkdir -p $MODDIR/$amBaseDir/$amConfDir
	$doit $mkdir -p $MODDIR/$amBaseDir/$amAmxDir

	# install dsos
	$doit $cp $MYDIR/${myAMdllDir}/$amDso $MODDIR/$amBaseDir/$amDllDir
	$doit $cp $MYDIR/${myAMXdllDir}/amx_admin.so $MODDIR/$amBaseDir/$amDllDir

	# install amx files. No time to make this nice, we just drop anything in there we got. Needs to be redone some day.
	$doit $cp $MYDIR/scripting/binaries/*.${amxext} $MODDIR/$amBaseDir/$amAmxDir/

	# install base config files
	$doit eval "$mv $MODDIR/$amBaseDir/$amConfDir/plugin.ini  $MODDIR/$amBaseDir/$amConfDir/plugin.ini.old >/dev/null 2>&1"
	$doit eval "$mv $MODDIR/$amBaseDir/$amConfDir/adminmod.cfg  $MODDIR/$amBaseDir/$amConfDir/adminmod.cfg.old >/dev/null 2>&1"

	$doit $cp $MYDIR/config/plugin.ini $MODDIR/$amBaseDir/$amConfDir/
	$doit $cp $MYDIR/config/adminmod.cfg $MODDIR/$amBaseDir/$amConfDir/
	
	if [ ! -f ${MODDIR}/$amBaseDir/$amConfDir/users.ini ] ; then
		$doit $cp ${MYDIR}/config/Samples/users.ini $MODDIR/$amBaseDir/$amConfDir
	fi

	echo " done."

elif (echo $installAM | grep "updt:" ) >/dev/null 2>&1 ; then
	# install new am into standard location and remove old one
	echo ""
	echo ""
	echon "Installing Admin Mod binaries to $mod_name/$amBaseDir/$amDllDir ..."
	$doit $mkdir -p $MODDIR/$amBaseDir/$amDllDir
	$doit $mkdir -p $MODDIR/$amBaseDir/$amConfDir
	$doit $mkdir -p $MODDIR/$amBaseDir/$amAmxDir

	# delete old dsos
	$doit $rm $amdll
	$doit $rm $MODDIR/$oldAMdir/amx_admin.so
	# install dsos
	$doit $cp $MYDIR/${myAMdllDir}/$amDso $MODDIR/$amBaseDir/$amDllDir
	$doit $cp $MYDIR/${myAMXdllDir}/amx_admin.so $MODDIR/$amBaseDir/$amDllDir
	echo " done."


	echo ""
	echo ""
	echon "Trying to locate your config files and moving them to $mod_name/$amBaseDir/$amConfDir ..."
	# do we have a users.ini file?
	cfile=`$grep "^ *users_file" $MODDIR/server.cfg | $sed 's/.* \"\{0,1\}\([^\"][^\"]*\)\"\{0,1\} *.\{0,1\}$/\1/'`
	if [ -z "$cfile" ] ; then
		[ -f $MODDIR/users.ini ] && cfile="users.ini"
	fi
	if [ ! -z "$cfile" ] ; then
		bcfile=`$basename $cfile`
		if [ -f "$MODDIR/$cfile" ] ; then
			echol "$MODDIR/$cfile "
			echo "    --> $MODDIR/$amBaseDir/$amConfDir/$bcfile"
			$doit $cp $MODDIR/$cfile $MODDIR/$amBaseDir/$amConfDir/$bcfile
			$doit $mv $MODDIR/$cfile $MODDIR/${cfile}.old
			worked="yes"
		fi
	fi

	# do we have a maps.ini file?
	cfile=`$grep "^ *maps_file" $MODDIR/server.cfg | $sed 's/.* \"\{0,1\}\([^\"][^\"]*\)\"\{0,1\} *.\{0,1\}$/\1/'`
	if [ -z "$cfile" ] ; then
		[ -f "$MODDIR/maps.ini" ] && cfile="maps.ini"
	fi
	if [ ! -z "$cfile" ] ; then
		bcfile=`$basename $cfile`
		if [ -f "$MODDIR/$cfile" ] ; then
			echol "$MODDIR/$cfile "
			echo "    --> $MODDIR/$amBaseDir/$amConfDir/$bcfile"
			$doit $cp $MODDIR/$cfile $MODDIR/$amBaseDir/$amConfDir/$bcfile
			$doit $mv $MODDIR/$cfile $MODDIR/${cfile}.old
			worked="yes"
		fi
	fi

	# do we have a ips.ini file?
	cfile=`$grep "^ *ips_file" $MODDIR/server.cfg | $sed 's/.* \"\{0,1\}\([^\"][^\"]*\)\"\{0,1\} *.\{0,1\}$/\1/'`
	if [ -z "$cfile" ] ; then
		[ -f "$MODDIR/ips.ini" ] && cfile="ips.ini"
	fi
	if [ ! -z "$cfile" ] ; then
		bcfile=`$basename $cfile`
		if [ -f "$MODDIR/$cfile" ] ; then
			echol "$MODDIR/$cfile "
			echo "    --> $MODDIR/$amBaseDir/$amConfDir/$bcfile"
			$doit $cp $MODDIR/$cfile $MODDIR/$amBaseDir/$amConfDir/$bcfile
			$doit $mv $MODDIR/$cfile $MODDIR/${cfile}.old
			worked="yes"
		fi
	fi

	# do we have a models.ini file?
	cfile=`$grep "^ *models_file" $MODDIR/server.cfg | $sed 's/.* \"\{0,1\}\([^\"][^\"]*\)\"\{0,1\} *.\{0,1\}$/\1/'`
	if [ -z "$cfile" ] ; then
		[ -f "$MODDIR/modles.ini" ] && cfile="modles.ini"
	fi
	if [ ! -z "$cfile" ] ; then
		bcfile=`$basename $cfile`
		if [ -f $MODDIR/$cfile ] ; then
			echol "$MODDIR/$cfile "
			echo "    --> $MODDIR/$amBaseDir/$amConfDir/$bcfile"
			$doit $cp $MODDIR/$cfile $MODDIR/$amBaseDir/$amConfDir/$bcfile
			$doit $mv $MODDIR/$cfile $MODDIR/${cfile}.old
			worked="yes"
		fi
	fi

	# do we have a vault.ini file?
	cfile=`$grep "^ *admin_vault_file" $MODDIR/server.cfg | $sed 's/.* \"\{0,1\}\([^\"][^\"]*\)\"\{0,1\} *.\{0,1\}$/\1/'`
	if [ -z "$cfile" ] ; then
		[ -f $MODDIR/vault.ini ] && cfile="vault.ini"
	fi
	if [ ! -z "$cfile" ] ; then
		bcfile=`$basename $cfile`
		if [ -f $MODDIR/$cfile ] ; then
			echol "$MODDIR/$cfile "
			echo "    --> $MODDIR/$amBaseDir/$amConfDir/$bcfile"
			$doit $cp $MODDIR/$cfile $MODDIR/$amBaseDir/$amConfDir/$bcfile
			$doit $mv $MODDIR/$cfile $MODDIR/${cfile}.old
			worked="yes"
		fi
	fi

	# do we have a plugin.ini file?
	cfile=`$grep "^ *admin_plugin_file" $MODDIR/server.cfg | $sed 's/.* \"\{0,1\}\([^\"][^\"]*\)\"\{0,1\} *.\{0,1\}$/\1/'`
	if [ -z "$cfile" ] ; then
		[ -f $MODDIR/plugin.ini ] && cfile="plugin.ini"
	fi
	if [ ! -z "$cfile" ] ; then
		bcfile=`$basename $cfile`
		if [ -f $MODDIR/$cfile ] ; then
			echol "$MODDIR/$cfile "
			echo "    --> $MODDIR/$amBaseDir/$amConfDir/$bcfile"
			$doit $cp $MODDIR/$cfile $MODDIR/$amBaseDir/$amConfDir/$bcfile
			worked="yes"
		fi
	fi

	echol " done."
	if [ ! -z "$worked" ] ; then
		echo " Backup copies of your config files have been saved with the extension '.old'."
		echo " Remove them by hand if you do not need them anymore."
	fi


	# Install script files.
	if [ -f $MODDIR/${cfile} ] ; then
		echo ""
		echo ""
		echon "Relocating your script files to $mod_name/$amBaseDir/$amAmxDir ..."
		( cat $MODDIR/$cfile | while read plugin ; do 
			if ( echo $plugin | grep "^;\{0,1\}#\{0,1\} *dlls/.*\.${amxext}" ) >/dev/null 2>&1 ; then
				# if this is a plugin installed in the default MOD/dlls path
				# we copy it over
				if [ "x${amxext}" = "xamx64" ] ; then
					pfile=`echo $plugin | $sed 's/^\(\;\{0,1\}#\{0,1\}\) *dlls\/\(..*\.amx64\).*/dlls\/\2/'`
				else
					pfile=`echo $plugin | $sed 's/^\(\;\{0,1\}#\{0,1\}\) *dlls\/\(..*\.amx\).*/dlls\/\2/'`
				fi
				bpfile=`$basename $pfile`
				$doit $mv $MODDIR/$pfile $MODDIR/$amBaseDir/$amAmxDir/
				echol "$pfile"
				echo "    --> $amBaseDir/$amAmxDir/$bpfile"
			else
				echo "Leaving \"$plugin\" in place. You can move it by hand if you need it."
			fi
		done )
		if [ "x${amxext}" = "xamx64" ] ; then
			$doit eval "$cat $MODDIR/${cfile} | $sed 's/^\(\;\{0,1\}#\{0,1\}\) *dlls\/\(..*\.amx64\)/\1addons\/adminmod\/scripts\/\2/' > $MODDIR/$amBaseDir/$amConfDir/$bcfile"
		else
			$doit eval "$cat $MODDIR/${cfile} | $sed 's/^\(\;\{0,1\}#\{0,1\}\) *dlls\/\(..*\.amx\)/\1addons\/adminmod\/scripts\/\2/' > $MODDIR/$amBaseDir/$amConfDir/$bcfile"
		fi
		$doit $mv $MODDIR/$cfile $MODDIR/${cfile}.old	
	else
		$doit $cp $MYDIR/config/plugin.ini $MODDIR/$amBaseDir/$amConfDir
	fi

	echo ""
	echon "Installing new versions of script files ..."
	# install amx files. No time to make this nice, we just drop anything in there we got. Needs to be redone some day.
	$doit $cp $MYDIR/scripting/binaries/*.${amxext} $MODDIR/$amBaseDir/$amAmxDir/
	echo " done."

	# install base config file
	$doit eval "$mv $MODDIR/$amBaseDir/$amConfDir/adminmod.cfg  $MODDIR/$amBaseDir/$amConfDir/adminmod.cfg.old >/dev/null 2>&1"
	$doit $cp $MYDIR/config/adminmod.cfg $MODDIR/$amBaseDir/$amConfDir/


elif (echo $installAM | grep "repl:" ) >/dev/null 2>&1 ; then
	# replace am in old location with new DSO
	echo ""
	echo ""
	echon "Installing Admin Mod binaries ..."
	$doit $cp $MYDIR/${myAMdllDir}/$amDso $amdll
	$doit $cp $MYDIR/${myAMXdllDir}/amx_admin.so $MODDIR/$oldAMdir/

	echo " done."
	echo " Please install the Small scripts from the Adminmod/scripting folder by hand."
fi 
	# otherwise we don't install any mm DSO




#
# Next, we need to make an entry in the MM plugins list.
#

if (echo "$installAM" | $grep ":inst\|:updt" ) >/dev/null 2>&1 ; then
	echo ""
	echon "Updating your Metamod plugins file ..."
	# if the file exists already we might have to update the AM entry
	if [ ! -z "$mmPluginsFile" -a -f "$mmPluginsFile" ] ; then
		# make a backup copy
		if [ ! -f ${mmPluginsFile}.am.orig ] ; then
			$doit $cp $mmPluginsFile ${mmPluginsFile}.am.orig
		fi
	
		if file_ends_properly $mmPluginsFile ; then
			$doit eval "$cat $mmPluginsFile | $sed -e '/$amDso/d' > $TEMPFILE"
			$doit eval "echo linux $amBaseDir/$amDllDir/$amDso >> $TEMPFILE"
		else
			$doit eval "$cat $mmPluginsFile | $sed -e '/$amDso/d' > $TEMPFILE"
			$doit eval "echo linux $amBaseDir/$amDllDir/$amDso >> $TEMPFILE"
			$doit eval "echo '' >> $TEMPFILE"
		fi

		$doit $cp $TEMPFILE $mmPluginsFile
		$doit $rm $TEMPFILE

	# else if we know the location of the file but it doesn't exist yet, create it
	elif [ ! -z "$mmPluginsFile" ] ; then
		$doit eval "echo linux $amBaseDir/$amDllDir/$amDso > $mmPluginsFile" 

	# else, uhm, this is bad. Let's simply assume the new ADS location
	else
		$doit eval "echo linux $amBaseDir/$amDllDir/$amDso > $MODDIR/$mmBaseDir/plugins.ini" 
	fi

	echo " done."

elif (echo $installAM | $grep ":act" ) >/dev/null 2>&1 ; then
	echo ""
	echon "Updating your Metamod plugins file ..."
	# if the file exists already we might have to update the AM entry
	if [ ! -z "$mmPluginsFile" -a -f "$mmPluginsFile" ] ; then
		# make a backup copy
		if [ ! -f ${mmPluginsFile}.am.orig ] ; then
			$doit $cp $mmPluginsFile ${mmPluginsFile}.am.orig
		fi
	
		if file_ends_properly $mmPluginsFile ; then
			# if it has the AM entry commented out, just uncomment the line
			if ( $cat $mmPluginsFile | $grep "$amDso" ) >/dev/null 2>&1 ; then
				$doit eval "$cat $mmPluginsFile | $sed -e 's/..*\(linux  *..*${amDso}.*\)/\1/' > $TEMPFILE"
			else # just add the line
				$doit eval "echo linux $amBaseDir/$amDllDir/$amDso >> $TEMPFILE"
			fi
		else
			if ( $cat $mmPluginsFile | $grep "$amDso" ) >/dev/null 2>&1 ; then
				$doit eval "$cat $mmPluginsFile | $sed -e 's/..*\(linux  *..*${amDso}.*\)/\1/' > $TEMPFILE"
			else # just add the line
				$doit eval "echo linux $amBaseDir/$amDllDir/$amDso >> $TEMPFILE"
				$doit eval "echo '' >> $TEMPFILE"
			fi
		fi

		$doit $cp $TEMPFILE $mmPluginsFile
		$doit $rm $TEMPFILE

	# else if we know the location of the file but it doesn't exist yet, create it
	elif [ ! -z "$mmPluginsFile" ] ; then
		$doit eval "echo linux $MODDIR/$amBaseDir/$amDllDir/$amDso > $mmPluginsFile" 

	# else, uhm, this is bad. Let's simply assume the new ADS location
	else
		$doit eval "echo linux $MODDIR/$amBaseDir/$amDllDir/$amDso > $MODDIR/$mmBaseDir/plugins.ini" 
	fi

	echo " done."

fi


#
# Try to set up the config file
#


reinstallS=""
reinstallA=""
if [ -f "${MODDIR}/server.cfg" ] ; then
	reinstallA=`$grep '^ *exec  *.*adminmod.cfg' $MODDIR/server.cfg | $tail -n 1 | $sed -e 's/^ *exec  *//'`
	reinstallS=`$grep '^ *admin_plugin_file' ${MODDIR}/server.cfg | $tail -n 1 | $sed -e 's/.*\(admin_plugin_file\).*/\1/'`
	if [ "$reinstallS" = "" ] ; then
		reinstallS=`$grep '^ *script_file' ${MODDIR}/server.cfg | $tail -n 1 | $sed -e 's/.*\(script_file\).*/\1/'`
	fi
fi


if [ -z "$reinstallA" -a -f "${MODDIR}/adminmod.cfg" ] ; then
  reinstallA="yesold"
elif [ -z "$reinstallA" -a -f "${MODDIR}/$amBaseDir/$amConfDir/adminmod.cfg" ] ; then
	reinstallA="yesnew"
fi

reinstall="${reinstallS}${reinstallA}"
answer="n"

if (echo "$installAM" | $grep ":act" ) >/dev/null 2>&1 ; then
# if we had only activated an installed version of AM, we don't need to do it again.
	$cat <<EOF



 The Admin Mod files have now been installed and your Admin Mod version has been
 activated. Please check your adminmod.cfg file and add the new cvars and remove
 obsolete ones. You will find the current set of cvars and their defaults in the
 file $MYDIR/config/adminmod.cfg.

 I will not automatically edit your adminmod.cfg file.

EOF


elif [ ! -z "$reinstallS" -a x"$reinstallA" = 'xyesnew' ] ; then
# upgrade of pre-ADS version, updating the server.cfg file
  $cat <<EOF



 The Admin Mod files have now been installed. It looks like you had installed a
 version of Admin Mod before which did not use the directories laid forth in the
 new Addons Directory Standard. You will have to edit your adminmod.cfg file in 
 the directory $MODDIR/$amBaseDir/$amConfDir
 to configure Admin Mod for your server. Then add this line to the bottom of 
 your ${MODDIR}/server.cfg file: 

 exec $amBaseDir/$amConfDir/adminmod.cfg.

 You should remove all other Admin Mod settings from the server.cfg file. Note
 that you will have to set up the Admin Mod configuration before you can use
 Admin Mod.

 I can try and remove the old Admin Mod settings from your server.cfg file or,
 alternatively, comment them out. 

EOF

  echon "Shall I Delete the old settings, Comment them out or Leave the file as it is (d/c/l) [l] "
  read answer
  if [ "$answer" = "" ] ; then
    answer="l"
  fi

	if [ "$answer" = 'd' ] ; then
		$doit $cp $MODDIR/server.cfg $MODDIR/server.cfg.ambak.$$
		$doit eval "$cat $MODDIR/server.cfg | sed -f ${MYDIR}/${amDBdir}/${fdot}delete.cfg.sed > $TEMPFILE"
		$doit $mv $TEMPFILE $MODDIR/server.cfg
		echo "A backup copy of your server.cfg file has been saved under server.cfg.ambak.$$"
	elif [ "$answer" = 'c' ] ; then
		$doit $cp $MODDIR/server.cfg $MODDIR/server.cfg.ambak.$$
		$doit eval "$cat $MODDIR/server.cfg | sed -f ${MYDIR}/${amDBdir}/${fdot}clean.cfg.sed > $TEMPFILE"
		$doit $mv $TEMPFILE $MODDIR/server.cfg
		echo "A backup copy of your server.cfg file has been saved under server.cfg.ambak.$$"
	fi

	$cat <<EOF

 I can now add the necessary line to set the Admin Mod cvars from the adminmod.cfg
 file to your server.cfg file.

EOF

	answer="n"
	echon "Shall I add the line to your server.cfg file? (y/n) [y] "
	read answer
	if [ "$answer" = "" ] ; then
		answer="y"
	fi


elif [ "$reinstall" = "" -o x"$reinstallA" = 'xyesnew' ] ; then
# first time installation
  $cat  <<EOF



 The Admin Mod files have now been installed. It looks like you install this
 version of Admin Mod for the first time. You will have to edit your
 adminmod.cfg file in the directory 
 $MODDIR/$amBaseDir/$amConfDir
 to configure Admin Mod for your server. Then add this line to the bottom of
 your ${MODDIR}/server.cfg file: 

 exec $amBaseDir/$amConfDir/adminmod.cfg. 

 I have copied an adminmod.cfg file with default settings to your 
 ${MODDIR}/$amBaseDir/$amConfDir 
 directory and can now add the necessary line to your server.cfg file. 
 Edit the adminmod.cfg file afterwards to configure it to your needs.

EOF
  echon "Shall I add the line to your server.cfg file? (y/n) [y] "
  read answer
  if [ "$answer" = "" ] ; then
    answer="y"
  fi

elif [ x"$reinstallA" = x"$amBaseDir/$amConfDir/adminmod.cfg" ] ; then
# upgrade, but adminmod/config/adminmod.cfg present
	$cat <<EOF



 The Admin Mod files have now been installed. It looks like you had installed a
 version of Admin Mod before. You should check your adminmod.cfg file in the 
 Admin Mod configuration directory 
 $MODDIR/$amBaseDir/$amConfDir 
 because some options may have changed since the last version. An updated 
 version of the adminmod.cfg file has been installed and your original file
 has been saved as adminmod.cfg.old.

EOF

else 
# upgrade of pre-ADS version
  $cat <<EOF



 The Admin Mod files have now been installed. It looks like you had installed a
 version of Admin Mod before which did not use the directories laid forth in the
 new Addons Directory Standard. Since I can't update your config file automati-
 cally you will have to set up your base configuration file by hand. We suggest
 to place all cvar settings in a file called adminmod.cfg and put it into the
 directory:
 $MODDIR/$amBaseDir/$amConfDir.

 A sample adminmod.cfg configuration file can be found in the directory
 $MYDIR/config

 You'll then have to execute this file from your server.cfg file. To do that
 place the following line at the *bottom* of your server.cfg file:

 exec $amBaseDir/$amConfDir/adminmod.cfg

 You should remove all other Admin Mod settings from the server.cfg file. Note
 that you will have to set up the Admin Mod configuration before you can use
 Admin Mod.

EOF
fi

if [ "$answer" = "y" ] ; then
	#$doit $cp ${MYDIR}/config/adminmod.cfg ${MODDIR}/
	$doit eval "echo '' >> ${MODDIR}/server.cfg"
	$doit eval "echo '' >> ${MODDIR}/server.cfg"
	$doit eval "echo '// Execute Admin Mod configuration file' >> ${MODDIR}/server.cfg"
	$doit eval "echo exec $amBaseDir/$amConfDir/adminmod.cfg >> ${MODDIR}/server.cfg"
fi 

$cat <<EOF



   Congratulations, Admin Mod is now installed.
   To configure it you should now edit your adminmod.cfg file and create 
   users.ini, maps.ini, ips.ini, models.ini and wordlist files as needed.
   Follow the instructions in the documentation and check for more infor-
   mation at the Admin Mod website http://www.adminmod.org.

   Enjoy!

EOF




#this_is_the_last_line
