#!/bin/bash
#
# ======================================================
# VI editor settings
# set shiftwidth=4
# set tabstop=4
# ======================================================
# Title: 
# Author: Mike Gore
# Date: 7 Oct 2016
# Notes:
#   
# ======================================================

echo update packages
apt-get update -q >/dev/null 2>&1
echo Installing gdebi package installer
apt-get install -y -q gdebi


echo Install libstdc++5 for backwards compatibility
apt-get install -y -q libstdc++5

if [ -f /etc/init.d/networker ]
then
	echo stopping networker 
	/etc/init.d/networker stop
	echo removing old startup links
	rm -f /etc/rc?.d/*networker*
fi

echo Downloading Networker DEBIAN package
wget -nc http://ist.uwaterloo.ca/download/networker/current/nw_lgtoclnt_amd64.deb
echo "y" | gdebi --q nw_lgtoclnt_amd64.deb

if [ ! -f /etc/init.d/networker ]
then
	echo /etc/init.d/networker is missing
	exit 1
fi

echo Modify netwoker startup scripts
if ! grep backup.cs.uwaterloo.ca /etc/init.d/networker >/dev/null 2>&1
then
	sed -i -e "s/\/usr\/sbin\/nsrexecd$/& -s backup.cs.uwaterloo.ca/"  /etc/init.d/networker
	grep /usr/sbin/nsrexecd /etc/init.d/networker
fi

echo creating new startup links
rm -f /etc/rc?.d/*networker*
update-rc.d networker defaults 80

echo starting networker
/etc/init.d/networker stop
sleep 2
/etc/init.d/networker start

echo You should see the task nsrexecd running
ps ax | grep nsrexecd