#!/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`

# Get release arch.
if [ "$ARCH" = "i686" ]
then
	RELEASE_ARCH="i386"
        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  
else 
	RELEASE_ARCH="x86_64"
fi

REPASTIUM="ftp://yum.astium.nl/pub/dist/Astium/$V/CentOS6/neonova/$REL/$RELEASE_ARCH/os"

echo ""
echo "Installing Astium $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 Astium 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

# install yum-priorities
yum -y install yum-priorities 

# Now, install the NeoNova repository files, and the Astium kernel:
wget $REPASTIUM/CentOS/astium-release-$V-*.noarch.rpm
rpm -Uvh astium-release-$V-*.noarch.rpm
wget $REPASTIUM/Neonova/RPMS/astium-repo-$V-*.noarch.rpm
rpm -Uvh astium-repo-$V-*.noarch.rpm
sed -i -e s/gpgcheck=1/gpgcheck=0/g /etc/yum.repos.d/neonova-astium.repo

# clean yum cache
yum clean metadata

# Update CentOS.
yum -y update

# if postgresql /var/lib/pgsql/ does not exists, init postgreSQL.
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

# Remove installed ntp
rpm -e ntp --nodeps

# Now we're ready to install the Astium.
yum -y install astium

echo "NOTE: The currently supported countries in the Astium 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 ""
