#!/bin/bash

echo "Stopping Apache"
launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist
/usr/sbin/httpd -k stop
killall httpd

# move out old l9 conf
if [ -f /etc/apache2/other/lasso9.apache2.conf ]; then
	mkdir /etc/apache2/other_disabled
	mv /etc/apache2/other/lasso9.apache2.conf /etc/apache2/other_disabled/
elif [ -f /etc/apache2/sites/lasso9.apache2.conf ]; then
	mkdir /etc/apache2/sites_disabled
	mv /etc/apache2/sites/lasso9.apache2.conf /etc/apache2/sites_disabled/
elif [ -f /etc/apache2/users/lasso9.apache2.conf ]; then
	mkdir /etc/apache2/users_disabled
	mv /etc/apache2/users/lasso9.apache2.conf /etc/apache2/users_disabled/
fi

# move out other old l9 conf
if [ -f /etc/apache2/other/lasso9.manager.conf ]; then
	mkdir /etc/apache2/other_disabled
	mv /etc/apache2/other/lasso9.manager.conf /etc/apache2/other_disabled/
elif [ -f /etc/apache2/sites/lasso9.manager.conf ]; then
	mkdir /etc/apache2/sites_disabled
	mv /etc/apache2/sites/lasso9.manager.conf /etc/apache2/sites_disabled/
elif [ -f /etc/apache2/users/lasso9.manager.conf ]; then
	mkdir /etc/apache2/users_disabled
	mv /etc/apache2/users/lasso9.manager.conf /etc/apache2/users_disabled/
fi

swvers=`sw_vers -productVersion`

if [[ $swvers != 10.5* ]]; then
	conf_path=/private/tmp/mod_lasso9.conf
else
	conf_path=/private/tmp/mod_lasso9.10.5.conf
fi

# move in new im conf
if [ -f $conf_path ]; then
	if [ -d /Library/Server/Web/Config/apache2/sites ]; then	# OS 10.8 Server
		echo "Placing .conf file in /Library/Server/Web/Config/apache2/sites/"
		mv $conf_path /Library/Server/Web/Config/apache2/sites/
	elif [ -d /etc/apache2/other ]; then	# OS X Client
		echo "Placing .conf file in /etc/apache2/other/"
		mv $conf_path /etc/apache2/other/
	elif [ -d /etc/apache2/sites ]; then	# OS X Server
		echo "Placing .conf file in /etc/apache2/sites/"
		mv $conf_path /etc/apache2/sites/
	elif [ -d /etc/apache2/users ]; then	# last resort
		echo "Placing .conf file in /etc/apache2/users/"
		mv $conf_path /etc/apache2/users/
	else
		echo "No place to put Apache .conf file!"
		exit 1
	fi
fi

rm -f /usr/local/bin/{lasso9,lassoc,lassoserver}

# migration from 9.1.[0-3]
# move the database over
# change old references to new master home
if ([ -f "/Library/Application Support/LassoSoft/Lasso Instance Manager/Lasso 9 Home/SQLiteDBs/lasso9_instance_manager" ]); then
	mkdir /var/lasso/home/SQLiteDBs 2>/dev/null
	mv /Library/Application\ Support/LassoSoft/Lasso\ Instance\ Manager/Lasso\ 9\ Home/SQLiteDBs/* /var/lasso/home/SQLiteDBs/
	rm /Library/Application\ Support/LassoSoft/Lasso\ Instance\ Manager/instances/*/LassoApps/Admin.zip || :
	sqlite3 /var/lasso/home/SQLiteDBs/lasso9_instance_manager "UPDATE prov_vars SET var=\"LASSO9_MASTER_HOME=/var/lasso/home/\" WHERE var LIKE \"LASSO9_MASTER_HOME=%\""
fi

echo "Starting Apache"
launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
launchctl load /Library/LaunchDaemons/com.lassosoft.lassoinstancemanager.plist

# strip old lines from /etc/profile
echo "Checking environment variables in /etc/profile"
if ([ -f /etc/profile ] && grep -qe "LASSO9_MASTER_HOME" /etc/profile); then
	cp -f /etc/profile /etc/profile.bak
	grep -v LASSO9_ /etc/profile > /private/tmp/profile.lasso
	mv -f /private/tmp/profile.lasso /etc/profile
fi
# add global env vars to permit cli tool for any user
if ([ ! -f /etc/profile ] || ! grep -qe LASSO9_MASTER_HOME /etc/profile); then
	echo "Setting environment variables in /etc/profile"
	echo "export LASSO9_MASTER_HOME=/var/lasso/home/" >> /etc/profile
	echo "export LASSO9_HOME=/var/lasso/global_cli_home/" >> /etc/profile
fi

open -g "http://www.lassosoft.com/LSI_9v2"
exit 0
