#!/bin/sh

# This is for 2.2
V="2.2"
if [ -z "$1" ]
then
	REL="C"
else
	REL="$1"
fi

# Collect some information.
ARCH=`arch`
OSVERS=`rpm -qa \*-release|grep -Ei "^oracle|^redhat|^centos"| cut -d"-" -f3`

# Get release arch.
if [ "$ARCH" = "i686" ]
then
	RELEASE_ARCH="i386"

	if [ "$OSVERS" = "6" ]
	then
		echo "==============================================================="
		echo "WARNING!! YOU ARE INSTALLING THE ASTIUM ON A 32-BITS CENTOS. "
		echo "ASTIUM 2.2 IS ONLY SUPPORTED ON 64-BITS CENTOS 6 !!! "
		echo "==============================================================="
		sleep 5
		exit  
	fi
else 
	RELEASE_ARCH="x86_64"

	if [ "$OSVERS" = "5" ]
	then
		echo "==============================================================="
		echo "WARNING!! YOU ARE INSTALLING THE 64-BITS VERSION OF ASTIUM. "
		echo "THIS VERSION IS UNSUPPORTED AND WILL PROBABLY NOT WORK."
		echo ""
		echo "THIS INSTALL WILL CONTINUE AT YOUR OWN RISK. WE WILL NOT ANSWER"
		echo "QUESTIONS ABOUT PROBLEMS WITH THE 64-BIT VERSION"
	        echo "==============================================================="
		sleep 5
	fi
fi

REPASTIUM="ftp://yum.astium.nl/pub/dist/Supercell/$V/CentOS$OSVERS/intorrent/$REL/$RELEASE_ARCH/os"
REPNEONOVA="ftp://yum.neonova.nl/pub/dist/Neonova-CentOS/2.0/CentOS5/neonova/A/$RELEASE_ARCH/os"

echo ""
echo "Installing Supercell $V $REL $ARCH, please wait...."
echo ""

if selinuxenabled
then
	echo "Please set SELINUX=disabled in /etc/sysconfig/selinux."
	echo "Then REBOOT and try installing again."
	exit 1
fi

ifconfig eth0 > /dev/null 2>&1
if [ "$?" = "1" ]
then
	echo "Sorry, your system does not have an eth0 interface. This is required."
	echo "The Supercell wil NOT work without an eth0 interface. STOP."
	exit 1
fi

# If we run run on a 64 bit system, remove all i386 and i686 packages.
if [ $RELEASE_ARCH = "x86_64" ]
then
	yum remove \*.i\?86 -y
fi

# for 64-bit systems, install zlib, libstdc++ 32 bit versions, because aapt needs those
if [ $RELEASE_ARCH = "x86_64" ]
then
	yum -y install libzip.i686 libstdc++.i686	
fi

# On centos6 install yum-priorities
if [ "$OSVERS" = "6" ]
then
	yum -y install yum-priorities 
fi

# On centos5 install yum-priorities and tftp-server.
if [ "$OSVERS" = "5" ]
then
	# install yum-priorities
	yum -y install yum-priorities
	rpm -Uvh $REPNEONOVA/Neonova/RPMS/yum-priorities-*.noarch.rpm

	# temp because of bug in CentOS 5.7, they dropped tftp-server from their repos
	yum -y install xinetd
	rpm -Uvh ftp://yum.astium.nl/pub/tftp-server-0.49-2.el5.centos.$RELEASE_ARCH.rpm
fi

# Now, install the Supercell repository files:
rpm -Uvh $REPASTIUM/CentOS/supercell-release-$V-*.noarch.rpm
rpm -Uvh $REPASTIUM/intorrent/RPMS/supercell-repo-$V-*.noarch.rpm

# On centos5 also install the neonova repository.
if [ "$OSVERS" = "5" ]
then
	rpm -Uvh $REPNEONOVA/Neonova/RPMS/neonova-centos-release-2.0-*.noarch.rpm
fi

# clean yum cache
yum clean metadata

# Update CentOS.
yum -y update

# install fail2ban on centos5.
if [ "$OSVERS" = "5" ]
then
	wget http://download.fedoraproject.org/pub/epel/5/$RELEASE_ARCH/epel-release-5-4.noarch.rpm
	rpm -Uvh epel-release-5-4.noarch.rpm
	yum --enablerepo epel install -y fail2ban
fi

# if postgresql /var/lib/pgsql/ does not exists, init postgreSQL.
if [ "$OSVERS" = "6" ]
then
	if [ ! -d /var/lib/pgsql/data ]
	then
		if [ ! -d /var/lib/pgsql ]
		then
			yum -y install postgresql-server
		fi
		test=`/sbin/service postgresql 2>&1|grep initdb`
		if [ -n "$test" ]
		then
			/sbin/service postgresql initdb
		fi
	fi
fi

# Remove installed ntp
rpm -e ntp --nodeps

# Now we're ready to install the Supercell.
yum -y install supercell

echo "NOTE: The currently supported countries in the Supercell are:"
echo ""
echo "      Belgium"
echo "      Canada"
echo "      Germany"
echo "      Latvia"
echo "      Netherlands"
echo "      Netherlands Antilles"
echo "      Romania"
echo "      United Kingdom"
echo "      United States"
echo "      South Africa"
echo ""
echo "If you want your country to be supported, just send an email to: support@neonova.nl"
echo ""
echo "Are you running behind a firewall, ensure the proper ports are opened."
echo "See: http://www.neonova.nl/nl/content/download_astium/installation.php."
echo ""
echo "**************************************************************************"
echo "* Done. NOW REBOOT YOUR MACHINE."
echo "* Then point your browser to this machine and login using system/admin."
echo "**************************************************************************"
echo ""
