#!/bin/bash

if [ `whoami` != root ]; then
        echo "Must run as root or using sudo."
        exit 1
fi

if [ "$1" = "" ]; then
	#Download newest scrpit, run it, then then exit.
	echo "Downloading/running newest version of noc-install-upgrade.sh..."
	wget -nv -O /tmp/noc-install-upgrade.sh http://opennms.dougbakewell.ca/downloads/bin/noc-install-upgrade.sh
	chmod a+x /tmp/noc-install-upgrade.sh
	/tmp/noc-install-upgrade.sh -no-check
	exit $?
fi

echo ""
echo "NOC Install/Upgrade Script Version: `stat -c %y $0`"
echo "More Information: http://opennms.dougbakewell.ca/posts/insall-upgrade"
echo ""

echo -n "Continue [y] > "
read INPUT
if [ "$INPUT" != "" ] && [ "$INPUT" != "y" ]; then
	exit 1
fi
echo ""

install-upgrade-file () {
	file="$1"
	url="$2"
	bits="$3"

	echo -n "Installing/Upgrading $file..."

	rm "${file}.new"  > /dev/null 2>&1
	if [ -e $file ]; then
		wget -nv -a $NOCBASE/.install-upgrade.log -O "${file}.new" $2
	else
		wget -nv -a $NOCBASE/.install-upgrade.log -O $file $2
	fi
	
	if [ -e "${file}.new" ]; then
		if [ $file -nt $NOCBASE/.last-install-upgrade ]; then
			WARNINGS="${WARNINGS}\n${file} saved as ${file}.new"
			#make sure the modified files timestamp stays ahead of .last-install-upgrade
			CLEANUP="$CLEANUP touch $file;"
			chmod $bits "${file}.new"
			echo "saved with .new extention"
		else
			mv "${file}.new" $file > /dev/null 2>&1
			echo "mv ${file}.new $file" >> $NOCBASE/.install-upgrade.log
			chmod $bits $file
			echo "done"
		fi
	else
		chmod $bits $file
		echo "done"
	fi
}

###################### From Post "Setting Up Scripts" #############################
### set up or import /opt/noc.conf ###

#if there is a noc.conf then this is an upgrade. Import the noc.conf and continue
if [ -e /etc/noc.conf ]; then
	UPGRADE=1	
	source /etc/noc.conf
	echo "Found /etc/noc.conf. Upgrading..."
else #this is an initail install. Get info from user and set up noc.conf.
	UPGRADE=0	
	echo -n "Install location for noc scripts [/opt/noc] > "
	read NOCBASE
	if [ "$NOCBASE" = "" ]; then
		NOCBASE="/opt/noc"
	fi

	guess="?"	
	if [ -e /opt/opennms ]; then
		guess="/opt/opennms"
	else if [ -e /usr/share/opennms ]; then
		guess="/usr/share/opennms"
	fi
	fi
	
	echo -n "Location of OpenNMS install [$guess] > "
        read OPENNMSBASE
        if [ "$OPENNMSBASE" = "" ]; then
                OPENNMSBASE="$guess"
        fi
	if [ "$OPENNMSBASE" = "?" ]; then
		echo "Could not determine location of OpenNMS install. Exiting."
		exit 1
        fi
	echo -n "Creating /etc/noc.conf..."
	cat << EOF > /etc/noc.conf
NOCBASE=$NOCBASE

#OpenNMS is normally in /opt/opennms or /usr/share/opennms
OPENNMSBASE=$OPENNMSBASE

#The next loop adds the NOC script functions into the shell environment.
for f in \$NOCBASE/functions/* ; do
  source \$f
done

EOF
echo "done"
fi
echo ""

### Other stuff from setting up scripts ###
echo -n "Creating/Checking base directories..."
mkdir -p $NOCBASE
mkdir -p $NOCBASE/bin
mkdir -p $NOCBASE/functions
if [ $UPGRADE = 0 ]; then
	chmod -R a+rX $NOCBASE
fi
echo "done"

install-upgrade-file $NOCBASE/functions/base.func http://opennms.dougbakewell.ca/downloads/functions/base.func 644

##### From Post "Setting Up Custom Web Pages and Single Sign On" ##################
echo -n "Creating/Checking http and related directories..."
mkdir -p $NOCBASE/http
mkdir -p $NOCBASE/http/views
mkdir -p $NOCBASE/http/public
mkdir -p $NOCBASE/include
mkdir -p $NOCBASE/view-template
mkdir -p $NOCBASE/view-bin
if [ $UPGRADE = 0 ]; then
	chmod -R a+rX $NOCBASE/http $NOCBASE/include $NOCBASE/view-template $NOCBASE/view-bin
fi
echo "done"

install-upgrade-file $NOCBASE/http/index.shtml http://opennms.dougbakewell.ca/downloads/http/index.s 644
install-upgrade-file $NOCBASE/http/stats.shtml http://opennms.dougbakewell.ca/downloads/http/stats.s 644
install-upgrade-file $NOCBASE/http/public/index.shtml http://opennms.dougbakewell.ca/downloads/http/public/index.s 644

#============== Install files from other posts ==========

#$NOCBASE/view-bin
for f in commentsforced knownnames nodedetails ; do
	install-upgrade-file $NOCBASE/view-bin/${f}.cgi http://opennms.dougbakewell.ca/downloads/view-bin/$f 755
	ln -s $NOCBASE/view-bin/${f}.cgi $NOCBASE/http/${f}.cgi >/dev/null 2>&1
done

#$NOCBASE/bin
for f in clean-rrd-data count-rrd-data testsnmp set-cats-from-snmp set-critical-paths check-dns ; do
	install-upgrade-file $NOCBASE/bin/$f http://opennms.dougbakewell.ca/downloads/bin/$f 755
done

#$NOCBASE/http
for f in descsummary dupipaddr ; do 
        install-upgrade-file $NOCBASE/http/${f}.cgi http://opennms.dougbakewell.ca/downloads/http/$f 755
done

#$NOCBASE/include
install-upgrade-file $NOCBASE/include/stats http://opennms.dougbakewell.ca/downloads/include/stats 755

#$NOCBASE/
install-upgrade-file $NOCBASE/README http://opennms.dougbakewell.ca/downloads/README 644

#=== Done. Print README, Print WARNINGS, Record install/upgrade time, and Cleanup ===

echo ""
cat $NOCBASE/README
echo ""

if [ "$WARNINGS" != "" ]; then
	echo -e "=== WARNINGS ===\nThe following files may have been customized. New files saved with the .new extenstion. Manual upgrading may be required.$WARNINGS" | tee -a $NOCBASE/.install-upgrade.log
fi

#update timestamps
touch $NOCBASE/.last-install-upgrade
sleep 1
/bin/bash -c "$CLEANUP"
mv $0 $NOCBASE/bin/noc-install-upgrade.sh
echo "" | tee -a $NOCBASE/.install-upgrade.log
echo "Done."

