#!/bin/sh

#
# Script to install admin mod v2.50
# Alfred Reynolds (alfred@mazuma.net.au) 
# Copyright (C) 2000, 2001
#

VERSION="2.50.11"

sed=`which sed`
cat=`which cat`
grep=`which grep`
rm=`which rm`" -f"
cp=`which cp`" -f"
basename=`which basename`

TMPDIR=${TMPDIR:-/tmp}

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

# 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"
   for dir in `echo $PATH | $sed -e 's/:/ /g'`; do
      [ -x "$dir/echo" ] && elist="$elist $dir/echo"
   done
 
   # 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
echon()
{
   $echoN "$* $echoEnd"
} 


#
# In case dirname is not on system
#
dirname () {
   expr ${1-.}'/' : '\(/\)[^/]*//*$' \
        \| ${1-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
        \| .
}
 
#
# In case dirname 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_run ]; then
      foundtopdir="no default"
   fi
}
 
#
# Start of Script
#
FindTopDir $0
 
FindEchoN

MYDIR=`dirname $0`

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

progname=`$basename $0`

if [ "$1" = "" ]; then
  echo "Usage:";
  echo "$progname <mod name>";
  echo "where <mod name> is the directory name of the mod (e.g. cstrike)";
  exit;
fi


mod_name=`$basename $1`
premod_dir=`dirname $1`


#
# Check for our files
#
echo ""

$cat <<EOF
               Adminmod Installation Script
                       Version $VERSION

   This script will install Adminmod 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 Adminmod 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

echo ""


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

if [ ! -f "${MYDIR}/scripting/binaries/plugin_base.amx" ]; then
  echo "Couldn't find the base plugin. You have to compile the plugins before ";
  echo "installing Adminmod. Go into the scripting/examples directory";
  echo "and execute ./compile_all.sh. Exiting ...";
  exit;
fi

#
# Check for the mod's directory
#

if [ "$foundtopdir" = "no default" ] ; then
  if [ -f /compat/linux/usr/games/hlds_l/hlds_run ] ; then
    TOPDIR="/compat/linux/usr/games/hlds_l"
  elif [ -f /usr/local/hlds_l/hlds_run ] ; then
	TOPDIR="/usr/local/hlds_l"
  elif [ -f /home/halflife/hlds_l/hlds_run ] ; then
    TOPDIR="/home/halflife/hlds_l"
  else
    TOPDIR=""
  fi
else
  TOPDIR=$foundtopdir
fi

if [ "$premod_dir" != "." ] ; then
  TOPDIR=$premod_dir
fi

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
echo ""
MODDIR=${topdir}/${mod_name}

mod_dlls="${MODDIR}/dlls";

if [ ! -d $mod_dlls ]; then
  echo "Couldn't find mod's dlls dir $mod_dlls.";
  echo "Please follow the manual install instructions found";
  echo "in the documentation. Exiting";
  exit;
fi



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

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



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


echo ""
echo "Installing binaries and config files ..."
if [ -f ${MYDIR}/config/admin_help.cfg ] ; then
  $cp ${MYDIR}/config/admin_help.cfg $MODDIR
fi
$cp ${MYDIR}/dlls/* $mod_dlls
$cp ${MYDIR}/scripting/binaries/*.amx $mod_dlls
$cp ${MYDIR}/config/metamod.ini $MODDIR
$cp ${MYDIR}/config/plugin.ini $MODDIR
#if [ ! -f ${MODDIR}/users.ini ] ; then
#	$cp ${MYDIR}/config/Samples/users.ini $MODDIR
#fi

echo ""
echo "Editing your liblist.gam file ..."
if [ ! -f ${mod_liblist}.orig ] ; then
  $cp $mod_liblist ${mod_liblist}.orig
fi
$cat $mod_liblist | $sed -f ${MYDIR}/instscr.sed > $TEMPFILE


$cp $TEMPFILE $mod_liblist
$rm $TEMPFILE

reinstall=""
if [ -f "${MODDIR}/server.cfg" ] ; then
  reinstall=`$grep script_file ${MODDIR}/server.cfg`
fi



if [ "$reinstall" = "" ] ; then
  $cat <<EOF

   The Adminmod files have now been installed. It looks like you 
   install this version of Adminmod for the first time. You will 
   have to edit your server.cfg file in the directory $MODDIR. 
   I can now add the necessary lines to your server.cfg file.
   Edit the file afterwards to configure it to your needs.

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

   The Adminmod files have now been installed. It looks like you 
   had installed a version of Adminmod before. You will still have 
   to edit your server.cfg file in the directory $MODDIR. 
   If you want to, I can now append the necessary lines to your 
   server.cfg file. But since you have a previous installation you 
   may want to do that by hand. Edit the file in any case because some 
   options may have changed since the last version.

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

if [ "$answer" = "y" ] ; then
  $cat ${MYDIR}/config/server.cfg >> ${MODDIR}/server.cfg
fi 

$cat <<EOF

   Conratulations, Adminmod is now installed.
   To configure it you should now edit your server.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 Adminmod website http://www.adminmod.org.

   Enjoy!

EOF


