#!/bin/sh

functions()
	{
		SELFTMP=$0
		ORIGPWD=`pwd`
		REALPWD=`dirname $0`
		cd "$REALPWD"
		while [ -L "$SELFTMP" ]
			do
				SELFTMP=`readlink "$SELFTMP"`
				cd `dirname "$SELFTMP"`
				REALPWD=`pwd -P`
		done

		cd "$ORIGPWD"

		if [ "$INITIALIZED" != "1" ]
			then
				source $REALPWD/inc/functions.sh
		fi
	}

functions

fixlibraries()
	{
		if [ ! -f /lib/libssl.so ] && [ ! -f /usr/lib/libssl.so ]
			then 
				if [ -f /lib/libssl.so.4 ]
					then
						ln -sf libssl.so.4 /lib/libssl.so
					else
						if [ -f /usr/lib/libssl.so.4 ]
							then
								ln -sf libssl.so.4 /usr/lib/libssl.so
							else
								if [ -f /lib/libssl.so.5 ]
									then
										ln -sf libssl.so.5 /lib/libssl.so
									else
										if [ -f /usr/lib/libssl.so.5 ]
											then
												ln -sf libssl.so.5 /usr/lib/libssl.so
											else
												bad "libssl.so library couldn't be found, some parts may not work"
										fi
								fi
						fi
				fi
		fi

		
		if [ ! -f /lib/libcrypto.so ] && [ ! -f /usr/lib/libcrypto.so ]
			then 
				if [ -f /lib/libcrypto.so.4 ]
					then
						ln -sf libcrypto.so.4 /lib/libcrypto.so
					else
						if [ -f /usr/lib/libcrypto.so.4 ]
							then
								ln -sf libcrypto.so.4 /usr/lib/libcrypto.so
							else
								if [ -f /lib/libcrypto.so.5 ]
									then
										ln -sf libcrypto.so.5 /lib/libcrypto.so
									else
										if [ -f /usr/lib/libcrypto.so.5 ]
											then
												ln -sf libcrypto.so.5 /usr/lib/libcrypto.so
											else
												bad "libcrypto.so library couldn't be found, some parts may not work"
										fi
								fi
						fi
				fi
		fi

	}

fixwebservercfg()
	{
		if [ -f ${MERAKDIR}/config/webserver.cfg} ]
			then
				grep -q "var/phpsocket|scripts/phpd" "${MERAKDIR}/config/webserver.cfg" && return 0
				cp "${MERAKDIR}/config/webserver.cfg" "${MERAKDIR}/config/webserver.cfg.saved"
				sed -e "s/var\/phpsocket/var\/phpsocket\|scripts\/phpd/g" "${MERAKDIR}/config/webserver.cfg.saved" >"${MERAKDIR}/config/webserver.cfg"
		fi
	}

installmissingpkgs()
	{
		[[ ${PKG2INSTALL} != "" ]] && getconfirm "Install missing packages?" && up2date -i ${PKG2INSTALL}
	}
		


upgradetask()
	{
		warn "We are going to perform the data upgrade to the latest version"
		warn "To do this, all services have to be started"
		warn "You can do this by hand by running"
		warn "./tool upgrade old_version_here in $MERAKDIR"
		getconfirm "Start services and perform the upgrade NOW?" && upgradedo
	}

upgradedo()
	{
		$SCRIPTDIR/restartd
		sleep 1
		tool upgrade "$OLDVERSION" && good "Upgrade completed successfully" || bad "Upgrade problem!"
	}

fixlibraries
installmissingpkgs
fixwebservercfg
getconfirm "Do you want Merak Mail Server to start automaticaly on reboot?" && $SCRIPTDIR/initinstall

[[ $OLDVERSION != "" ]] && upgradetask

if [ "$UPGRADE" != "1" ]
	then
		getconfirm "Do you want to run Merak Mail Server Wizard now?" && $MERAKDIR/wizard
		good "You can rerun wizard at later time by running $MERAKDIR/wizard script"
fi