#!/bin/bash
#
# ==========================================================
# VI editor settings
# set shiftwidth=4
# set tabstop=4
# ==========================================================
# Title: Install Legato Networker 
# Author: Mike Gore
# Date: 28 April 2014
# Notes: 
# ==========================================================


TMP=/tmp/$$.install

mkdir -p $TMP
pushd $TMP
	echo update packages
	apt-get update -q

	# =================== NOTE ======================
	# These files names will change with new releases
	if [ `arch` = "x86_64" ]
	then
		apt-get install --reinstall libstdc++5 libstdc++5:i386 libxp6
	else
		apt-get install --reinstall libstdc++5  libxp6
	fi
	# ===============================================

	echo Downloading $LEGATO
	wget -nc http://ist.uwaterloo.ca/download/networker/current/nw_lgtoclnt_amd64.deb 
	echo Installing
	dpkg -i nw_lgtoclnt_amd64.deb

	echo stopping networker
	/etc/init.d/networker stop

	echo removing old startup links
	update-rc.d -f networker remove

	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 /etc/init.d/networker -e "s/\/usr\/sbin\/nsrexecd$/& -s backup.cs.uwaterloo.ca/" 
	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 start

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

if [ -n "$TMP" ]
then
	rm -rf "$TMP"
fi