#!/bin/bash

EXECLINE='/usr/local/sbin/sms-default 8000 > /dev/console'

echo "This will install sms-server into /usr/local/sbin/"
echo "and will add a line into your rc.local file"
echo "to run the server on boot up."
echo ""
echo "Is this okay? [y,n]"

read foo
if [ "$foo" != "y" ] ; then
	echo "Installation aborted"
	exit 1
fi

echo ""
echo "Installing files into /usr/local/sbin/ ..."
install -m 755 sms-server /usr/local/sbin/

echo ""
echo "Symlinking IP address version as default server..."
rm -f /usr/local/sbin/sms-default
ln -s /usr/local/sbin/sms-server /usr/local/sbin/sms-default 

echo ""
echo "Adding a line to your rc.local..."

if [ ! -f /etc/rc.d/rc.local ] ; then 
	echo "--- Sorry, can't find rc.local on your system."
	echo "--- You will need to add the following line into your rc scripts:"
	echo "--- $EXECLINE &"
	exit 2
fi

if (grep sms-default /etc/rc.d/rc.local > /dev/null) ; then
	echo "--- It seems you already have a line in rc.local... replacing line..."
	cp -f /etc/rc.d/rc.local /etc/rc.d/rc.local.backup
	grep -v sms-default /etc/rc.d/rc.local.backup > /etc/rc.d/rc.local
fi

echo "$EXECLINE &" >> /etc/rc.d/rc.local

echo ""
echo "Would you like to reboot your system now? [y,n]"
read foo
if [ "$foo" = "y" ] ; then
	shutdown -r now
	exit 1
fi

echo "Please reboot your computer to start the server."
echo "Installation complete."

