# -*- Mode: sh -*-

distro_text() {
	echo
	echo "If you require installation on another system, this is only"
	echo "possible via manual installation. Please follow installation"
	echo "instructions in the installation documentation."
	echo
}

warning() {
	echo
	echo "*** WARNING ***"
	echo
}

continue_anyway() {
	echo "If you still wish to continue with this installation script, please make"
	echo "sure the Zarafa packages are already installed on the system."
	echo "Press <enter> to continue, or <ctrl-c> to stop."
	read dummy
}

sles1011_only() {
	warning
	echo "This installation script only runs with SuSE Enterprise Linux 10 and 11."
	echo "Your distribution was found to be:"
	cat /etc/SuSE-release
	distro_text
	continue_anyway
}

redhat56_only() {
	warning
	echo "This installation script only runs with RedHat Enterprise Linux 5 and 6."
	echo "Your distribution was found to be:"
	cat /etc/redhat-release
	distro_text
	continue_anyway
}

ubuntu_lts_only() {
	warning
	echo "This installation script only runs with Ubuntu 10.04 LTS (lucid),"
	echo "and 12.04 LTS (precise)."
	echo "Your distribution was found to be:"
	echo ${DISTRIB_DESCRIPTION}
	distro_text
	continue_anyway
}

debian567_only() {
	warning
	echo "This installation script only runs with 5 (Lenny), 6 (Squeeze) and 7 (Wheezy)."
	echo "Your distribution was found to be:"
	echo -en "Debian "
	cat /etc/debian_version
	distro_text
	continue_anyway
}

unsupported_distro() {
	warning
	echo "Unfortunately, this installation script could not determine"
	echo "your Linux Distribution. Please follow installation"
	echo "instructions in the installation documentation for a manual installation."
	echo
	continue_anyway
	ARCH="Unknown"
	DISTRO="Unknown"
	DVERSION="Unknown"
}

unsupported_architecture() {
	echo "Unfortunately, you use an unsupported architecture for this distribution."
	echo
	exit 1
}

mysql_version() {
	if [ ! -z $MYSQLVER ]; then
		echo "You have MySQL version $MYSQLVER installed."
	fi
	echo "Zarafa requires MySQL 4.1 or higher. Please upgrade your MySQL"
	echo "database and restart the installer."
	echo
	exit 1
}

mysql_reconnect_bug() {
	echo "Your installed version of MySQL contains a bug where it cannot reconnect"
	echo "to the server after is was disconnected due to a timeout."
	echo
	echo "Please upgrade your MySQL installation, especially the MySQL client library."
	echo
	echo "Press <enter> to continue the installation, or <ctrl-c> to stop."
	read dummy
}

distro_check() {
	DISTRO=""
#
# SLES check
#
	if [ -f /etc/SuSE-release ]; then
		grep -q Enterprise /etc/SuSE-release
		if [ $? -eq 0 ]; then
			DISTRO=SLES
		else
			DISTRO=SuSE
		fi
		DVERSION=`grep VERSION /etc/SuSE-release | sed -e "s/VERSION = //"`
		SUSEMAJOR=`echo $DVERSION | sed -e 's/\..*$//'`
		SUSEMINOR=`echo $DVERSION | grep '\.' | sed -e 's/^.*\.//'`
		if [ -z "$SUSEMINOR" ]; then SUSEMINOR=`grep PATCHLEVEL /etc/SuSE-release | sed -e "s/PATCHLEVEL = //"`; fi
		ARCH=`rpm -q rpm --queryformat '%{ARCH}'`

		if [ $SUSEMAJOR -ne 10 -a $SUSEMAJOR -ne 11 ]; then
			sles1011_only
		fi
		if [ $SUSEMAJOR -eq 10 -a $SUSEMINOR -lt 2 ]; then
			echo
			echo "You need atleast SP2 for SLES 10."
			echo
			exit 1
		fi
		if [ $SUSEMAJOR -eq 11 ]; then
			echo
			echo "Warning: Please make sure you have the latest libxml2 and php5 versions installed."
			echo "The version of php5 must atleast be 5.2.6-50.24 or a later 5.2 release."
			echo
		fi
		if [ $ARCH != "i586" -a $ARCH != "x86_64" ]; then
			unsupported_architecture
		fi
		return
	fi

#
# Redhat (RHEL) check
#
	if [ -f /etc/redhat-release ]; then
		DISTRO=RedHat
		DVERSION=`sed -e 's/.*release \([[:digit:]]\).*/\1/' /etc/redhat-release`
		ARCH=`rpm -q rpm --queryformat '%{ARCH}\n' | head -1`

		if [ $DVERSION -ne 5 -a $DVERSION -ne 6 ]; then
			redhat56_only
		fi
		if [ $ARCH != "i386" -a $ARCH != "i686" -a $ARCH != "x86_64" ]; then
			unsupported_architecture
		fi
		return
	fi

#
# Ubuntu check
#
	if [ -f /etc/lsb-release ] && grep -q Ubuntu /etc/lsb-release; then
		. /etc/lsb-release
		DISTRO=${DISTRIB_ID}
		DVERSION=${DISTRIB_RELEASE}
		ARCH=`dpkg --print-architecture`

		if [ $DVERSION != "12.04" -a $DVERSION != "10.04" ]; then
			ubuntu_lts_only
		fi
		if [ $ARCH != "i386" -a $ARCH != "amd64" ]; then
			unsupported_architecture
		fi
		return
	fi

#
# Debian check
#
	if [ -f /etc/debian_version ]; then
		DISTRO=Debian
		DVERSION=`cat /etc/debian_version`
		ARCH=`dpkg --print-architecture`

		# detect supported versions
		if [ ${DVERSION:0:3} != "5.0" -a ${DVERSION:0:3} != "6.0" -a ${DVERSION:0:1} != "7" ]; then
			debian567_only
		fi
		if [ $ARCH != "i386" -a $ARCH != "amd64" ]; then
			unsupported_architecture
		fi
		return
	fi

	unsupported_distro
}

replace() {
	file=$1
	option=$2
	value=$3

	# escape &-signs, and % for sed
	value=`echo $value | sed -e 's/\&/\\\&/g' -e 's/\%/\\\%/g'`
	sed -i -e "s%\([[:space:]]*$option[[:space:]]*=[[:space:]]*\).*%\1$value%" $file
}

uncomment() {
	file=$1
	option=$2

	sed -i -e "s%[[:space:]]*#\(.*$option.*\)%\1%" $file
}

import() {
	file=$1
	option=$2
	eval "$3=`grep -w $option $file | grep -v \# | cut -d= -f2 | sed -e 's/[[:blank:]\r]//g'`"
}

doscopy() {
	sed -e 's/\r$//g' <$1 >$2
}
