#!/bin/sh
# IRCD INSTALL SCRIPT
# $Id: install_ircd,v 1.5 2001/02/02 20:40:01 jpinto Exp $
#
# Completely re-written by FlashMan <chuegen@quadrunner.com> 981231
# Wow.  The old version was *very* messy and had boatloads of redundancy.
# Fixed.
#

# Path to install-sh
INSTALL_SH="autoconf/install-sh"

#
# dir_concat looks to see if we have an absolute or relative path
# it echos $DPATH/<arg> if we have a relative path
#
dir_concat() {
  if [ ! -z "`echo $1 | grep '^/'`" ]; then
    echo $1;
  else
    echo ${DPATH}/$1;
  fi;
}

#
# dir_make attempts to make a directory tree using mkdir -p
# Used to eliminate redundancy
#
dir_make() {
  if [ \( ! -z "$1" \) -a \( ! -d "$1" \) ]; then
    echo $1 does not exist, creating...
    mkdir -p $1
    if [ $? -ne 0 ]; then
      echo Could not create directory path $1.
      echo Perhaps you are not allowed to create a directory in the path.
      echo Please fix and try again.
      exit -1;
    fi;
  fi
}
#
#
#
#
check_install() {
  if [ ! -f $1 ]; then
	echo "- installing $2 --> $1"
	$INSTALL_SH -c $2 $1;
#  else
#	echo "- $1 was found"
  fi
}

check_install_data() {
  if [ ! -f $1 ]; then
	echo "- installing $2 --> $1"
	$INSTALL_SH -m 660 -c $2 $1;
#  else
#	echo "* $1 was found"
  fi
}

check_install_data_old() {
  if [ ! -f $1 ]; then
	echo "- installing $2 --> $1"
  else
	echo "- saving $1.old"
	mv $1 $1.old
  fi

  $INSTALL_SH -m 660 -c $2 $1;
}

is_upgrade=0
SPATH=`grep '#define.SPATH' include/path.h|awk '{print $3}'|tr -d \"`;
DPATH=`grep '#define.DPATH' include/path.h|awk '{print $3}'|tr -d \"`;
CPATH=`grep '#define.CPATH' include/config.h|awk '{print $3}'|tr -d \"`;
KPATH=`grep '#define.KPATH' include/config.h|awk '{print $3}'|tr -d \"`;
MPATH=`grep '#define.MPATH' include/config.h|awk '{print $3}'|tr -d \"`;
LPATH=`grep '#define.LPATH' include/config.h|awk '{print $3}'|tr -d \"`;
PPATH=`grep '#define.PPATH' include/config.h|awk '{print $3}'|tr -d \"`;
HPATH=`grep '#define.HPATH' include/config.h|awk '{print $3}'|tr -d \"`;

SPATH="${SPATH}"
CPATH=`dir_concat ${CPATH}`;
KPATH=`dir_concat ${KPATH}`;
MPATH=`dir_concat ${MPATH}`;
LPATH=`dir_concat ${LPATH}`;
PPATH=`dir_concat ${PPATH}`;
HPATH=`dir_concat ${HPATH}`;

#   DPATH = directory,
#   SPATH = server executable,
#   CPATH = conf file,
#   MPATH = MOTD
#   KPATH = kline conf file
#   leave KPATH undefined if you want klines in main conf file
#   HPATH is the opers help file, seen by opers on /quote help



if [ -f src/ircd.exe ]; then
  EXESUFFIX=".exe"
else
  EXESUFFIX=""
fi
clear
echo "##########################################################################"
echo "# brasmirc.ircd - Installing ircd binary and example files                  #"
echo "##########################################################################"
# Let's make sure $DPATH exists
dir_make ${DPATH}
echo - chmod 700 ${DPATH}
chmod 700 ${DPATH}
if [ ! -f src/ircd${EXESUFFIX} ]; then
  echo "src/ircd${EXESUFFIX} was not found!"
  echo "Please run \"make all\" to build ircd."
  exit -1;
fi

dir_make `dirname ${CPATH}`

# try to install the example files if needed

check_install ${DPATH}/mkpasswd tools/mkpasswd${EXESUFFIX}
check_install_data ${CPATH} samples/example.conf
check_install_data_old ${DPATH}/opers.motd samples/opers.motd
check_install_data ${DPATH}/qlines.conf samples/qlines.conf
check_install_data ${DPATH}/hubs.conf samples/hubs.conf
check_install_data ${DPATH}/portas.conf samples/portas.conf
check_install_data ${DPATH}/ircd.motd samples/brasmirc.motd
check_install_data ${KPATH} samples/kline.conf
check_install_data ${DPATH}/save.dconf samples/save.dconf
check_install_data_old ${DPATH}/help.user samples/help.user
check_install_data_old ${DPATH}/help.oper samples/help.oper
check_install_data_old ${DPATH}/help.admin samples/help.admin

# if ircd.pid is found lets kill thr running ircd
# install ircd, save old one as ircd.old
dir_make `dirname ${SPATH}`
echo "- installing ircd at ${SPATH}${EXESUFFIX}"
if [ ! -f ${SPATH}${EXESUFFIX} ]; then
  $INSTALL_SH -c src/ircd${EXESUFFIX} ${SPATH}${EXESUFFIX};
else
  if [ -f ${DPATH}/ircd.pid ] ; then
    echo "- checking dconf settings"
    src/ircd${EXESUFFIX} -s -c
    if [ $? -eq 0 ] ; then
      echo "- killing running ircd"
      kill `cat ${DPATH}/ircd.pid`  > /dev/null 2>&1
      is_upgrade=1
    else
      echo "- aborting upgrade, fix your dconf settings first"
      exit
    fi
  fi
  echo "- saving existing ircd as ircd${EXESUFFIX}.old"
  mv ${SPATH}${EXESUFFIX} ${SPATH}${EXESUFFIX}.old
  $INSTALL_SH -c src/ircd${EXESUFFIX} ${SPATH}${EXESUFFIX};
fi

if  [ "${EXESUFFIX}" = "" ]; then
  VERSION=`grep '#define.PATCHLEVEL' include/patchlevel.h|awk '{print $3}'|tr -d \"`;
  echo "${VERSION}" > ${DPATH}/version.info
  check_install ${DPATH}/sendbug tools/sendbug
fi
# finished.
if [ $is_upgrade -eq 1 ]; then
  echo "- running ircd"
  ${SPATH}${EXESUFFIX}
  echo "Upgrade completed!"
else
  echo "Install completed!"
  echo "Please edit the configuration files ${DPATH}/*.conf,*.dconf"
  echo "Then execute ircd with ${SPATH}${EXESUFFIX}"
fi

