#!/bin/bash

# This is a quick setup script to make the 3dmosmon installation easy.
# It will configure the mosstatd service which is required
# for the 3dmosmon.
# Matt Rechenburg / mosixview@t-online.de
#

# do we use mosstatd as 
# 0 standalone sever
# 1 by xinetd
# 2 by inetd
SERVER=0

CONFIGURESEVER=0

echo 
echo "3dmon installation"
echo
echo "You can run the mosstatd daemon (required for 3dmon)"
echo "in three diffrent ways:"
echo "(1) -> by xinetd"
echo "(2) -> by inetd"
echo "(3) -> as a standalone server"
echo
echo "Please decide which possibility you want to use"

read SERVER


case "$SERVER" in
	1)
	# xinetd
	# check for xinetd
	rpm -qa | grep xinetd > /dev/null
	if [[ $? == 0 ]]; then
	 echo "xinetd found!"
	 CONFIGURESERVER=1
	 else
 	 echo "xinetd not found!"
	 echo "Press return to continue"
	 echo "or CTRL-C for cancel"
	 read CANCEL
	fi
	;;
	2)
	# inetd
	# check for inetd
	rpm -qa | grep inetd > /dev/null
	if [[ $? == 0 ]]; then
	 echo "inetd found!"
         CONFIGURESERVER=2
	 else
         echo "inetd not found!"
         echo "Press return to continue"
         echo "or CTRL-C for cancel"
         read CANCEL
	fi
	;;


	3)
	# standalone servera
	echo "mosstatd will run as a standalone server"
	CONFIGURESERVER=3
	;;
	*)
	echo "Invalid value!"
	echo "Please try again"
	exit -1
	;;

esac

echo
echo "compiling mosstatd + 3dmon now"

# compiling section

cd 3dmosmon
make clean > /dev/null 2>&1
make
make install
cd ..

cd mosstatd
make clean > /dev/null 2>&1
make
make install
cd ..


# configure mosstatd service
cat /etc/services | grep mosstatd > /dev/null 2>&1
if [ $? == 1 ]
then
 echo "" >> /etc/services
 echo "mosstatd        10050/tcp       #openMosix/Mosix stat daemon" >> /etc/services
fi



# configure xinetd
if [ $CONFIGURESERVER == 1 ]; then
 echo "setting up the mosstatd xinetd-service"
 /bin/cp -f config/xinetd/mosstatd /etc/xinetd.d/mosstatd
 echo "restarting xinetd now"
 if [ -x /etc/init.d/xinetd ]; then
  /etc/init.d/xinetd restart
  echo 
  else
  echo "Could not find the xinetd-init script!"
  echo "Please be sure to start/restart xinetd now."
  echo "Press return to continue"
  read NOINIT
 fi
fi

# configure inetd
if [ $CONFIGURESERVER == 2 ]; then
 echo "setting up the mosstatd inetd-service"
 cat /etc/inetd.conf | grep mosstatd >/dev/null 2>&1
 # if there is no mosstatd severice already
 if [ $? == 1 ]; then
  echo "" >> /etc/inetd.conf
  echo "# mosstatd service for 3dmon" >> /etc/inetd.conf
  echo "mosstatd  stream  tcp     nowait  root	/usr/bin/mosstatd" >> /etc/inetd.conf
  echo "" >> /etc/inetd.conf
 fi
 echo "restarting inetd now"
 if [ -x /etc/init.d/inetd ]; then
  /etc/init.d/inetd restart
  echo
  else
  echo "Could not find the inetd-init script!"
  echo "Please be sure to start/restart xinetd now."
  echo "Press return to continue"
  read NOINIT
 fi
fi


# standalone
if [ $CONFIGURESERVER == 3 ]; then
 echo "setting up the mosstatd service as a standalone server"
 /bin/cp -f config/init/mosstatd /etc/init.d/
 chmod +x /etc/init.d/mosstatd
 echo 
 echo "NOTICE!! You have to start the mosstatd-daemon"
 echo "before you can use the 3dmon !!!"
 echo "You can do this by its init-script /etc/init.d/mosstatd"
 echo "e.g. /etc/init.d/mosstatd start"
 echo
 echo "Press return to start the mosstatd daemon now"
 echo "and finish the installtion"
 read CONFIRM
 /etc/init.d/mosstatd start
fi

echo
echo "Installation finished!"
echo "You can now execute :"
echo "3dmosmon [mosstatt_server_hostname]"
echo "e.g. 3dmosmon localhost"
echo 








