#!/bin/sh

if [[ $EUID -ne 0 ]]; then
   echo -e "\e[1;31mThis script must be run as root or it will fail\e[0m" 1>&2
   exit 1
fi

echo -e "\e[1;32mBitrix Environment for Linux installation script\e[0m" 1>&2
echo ""
echo -e "\e[1;32mYes will be assumed to answers, and will be defaulted. 'n' or 'no' will result in a No answer, anything else will be a yes.\e[0m"
echo ""
echo -e "\e[1;31mThis script MUST be run as root or it will fail\e[0m"
echo "---"

# Check if the OS matches Fedora 14-16 or CentOS/RHEL 5.* or CentOS/RHEL 6.*
OS=`cat /etc/redhat-release | awk {'print $1}'`

if [ "$OS" = "Red" ] ;
then
	rel="6"
	VER=`cat /etc/redhat-release | awk {'print $7}'`
	OS="CentOS"
	if [[ "$VER" == "5."* ]] ; then rel="5" ; fi
else
	rel="6"
	VER=`cat /etc/redhat-release | awk {'print $3}'`
	if [ "$OS" = "CentOS" -a "$VER" = "release"  ]; then VER=`cat /etc/redhat-release | awk {'print $4}'` ; fi
	if [[ "$VER" == "5."* ]] ; then rel="5" ; fi
fi

if [ "$OS" = "CentOS" ] && [[ "$VER" == "5."* || "$VER" == "6."* ]] || [[ "$OS" -eq "Fedora" && "$VER" -ge "14" && "$VER" -le "16" ]] ; then
	echo -e "\e[1;32m$OS $VER - OS and version are correct.\e[0m" 1>&2
else
	echo -e "\e[1;31mSystem runs on something other than Fedora 10-15 or CentOS 5.* or CentOS 6.*. This may not work!\e[0m";
	echo -e "\e[1;31mContinue Anyway? (y/n)\e[0m"
	read cont
		if [ "$cont" = "n" ] || [ "$cont" = "no" ]; then
			echo "Exiting..."
			exit 1
		fi
fi

# munin user wiil be created, if not exists
pwconv
test `grep munin /etc/passwd | wc -l` -eq 0 && { useradd -p munin munin; echo 'user munin was created' ; }

test -f /etc/yum.repos.d/bitrix.repo && mv -f /etc/yum.repos.d/bitrix.repo /etc/yum.repos.d/bitrix.repo.ori
test `rpm -q yum | grep "yum-3" | wc -l` -ne 1 && { echo "yum package required but not installed, exiting" ; exit 0 ; }

if [ "$OS" = "CentOS" ]; then

if [[ "$VER" == "5."* ]]; then

echo "
[bitrix]
name=\$OS \$releasever - \$basearch
failovermethod=priority
baseurl=http://repos.1c-bitrix.ru/yum/el/$rel/\$basearch
enabled=1
gpgcheck=0
" > /etc/yum.repos.d/bitrix.repo ;

else

echo "
[bitrix]
name=\$OS \$releasever - \$basearch
failovermethod=priority
baseurl=http://repos.1c-bitrix.ru/yum/el/$rel/\$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-BitrixEnv
" > /etc/yum.repos.d/bitrix.repo ;

rpm --import http://repos.1c-bitrix.ru/yum/RPM-GPG-KEY-BitrixEnv ;

fi

yum clean all
yum -y install epel-release
yum install -y yum-fastestmirror
yum -y install bitrix-env4.noarch

echo -e "\e[1;32mBitrix Environment for Linux installation complete\e[0m"
exit 0

else

echo "
[bitrix]
name=\$OS \$releasever - \$basearch
failovermethod=priority
baseurl=http://repos.1c-bitrix.ru/yum/fedora/base/\$releasever/\$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-BitrixEnv
" > /etc/yum.repos.d/bitrix.repo

rpm --import http://repos.1c-bitrix.ru/yum/RPM-GPG-KEY-BitrixEnv

yum clean all
yum install -y yum-fastestmirror
yum -y install bitrix-env4.noarch

echo -e "\e[1;32mBitrix Environment for Linux installation complete\e[0m"
exit 0

fi
