#!/bin/bash +x

disable_selinux() {
	if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
		sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
		setenforce 0
	fi
}

system_init(){
	echo 'system inti...'
	disable_selinux
	mkdir -p /data1/www
	systemctl stop firewalld.service
	systemctl disable firewalld.service
	yum upgrade -y
	yum -y install yum-plugin-fastestmirror 
	yum -y install gcc gcc-c++ wget lrzsz zip unzip vim
	yum -y install erlang xmlto
}

download_rpm(){
	echo 'download rpm...'
	rpm -Uvh http://download.icloudstudio.com/rpm/epel-release-latest-7.noarch.rpm
	rpm -Uvh http://download.icloudstudio.com/rpm/remi-release-7.rpm
	rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
	rpm -ivh http://download.icloudstudio.com/rpm/mysql-community-release-el7.rpm
	wget -P /data1/software/ http://download.icloudstudio.com/software/jexus-5.8.2.tar.gz
	wget -P /data1/software/ http://download.icloudstudio.com/rpm/rabbitmq-server-3.6.6-1.el7.noarch.rpm
	rpm --import http://download.icloudstudio.com/rpm/rabbitmq-signing-key-public.asc
}

yum_setting(){
	echo 'yum setting...'
	mv /etc/yum.repos.d/remi.repo /etc/yum.repos.d/remi.repo_backup
	wget -P /etc/yum.repos.d/ http://download.icloudstudio.com/rpm/remi.repo
	#yum clean all
	#yum -y upgrade
	#yum makecache
}

permission_setting(){
	echo 'permission setting...'
	groupadd www
	useradd -G www -s /sbin/nologin nginx
	useradd -G www web
	
	chown -R mysql:mysql /data1/mysql
	chown -R nginx:www /data1/jexus
	chown -R nginx:www /data1/www
}

install_jexus(){
	echo 'install jexus...'
	mkdir -p /data1/jexus/logs
	mkdir -p /data1/jexus/siteconf
	cd /data1/software/
	tar -zxvf /data1/software/jexus-5.8.2.tar.gz
	sudo /data1/software/jexus-5.8.2/install
	mv /usr/jexus/jws.conf /usr/jexus/jws.conf_backup
	wget -P /usr/jexus/ http://download.icloudstudio.com/jmm/config/jexus/jws.conf
}


install_mono(){
	echo 'install mono...'

	yum -y install yum-utils
	
	yum-config-manager --add-repo http://download.mono-project.com/repo/centos/

	yum -y install mono-complete.x86_64
}

install_memcached(){
	echo 'install memcached...'
	yum -y install memcached
}

install_rabbitmq(){
	echo 'install rabbitmq...'
	yum -y install /data1/software/rabbitmq-server-3.6.6-1.el7.noarch.rpm

	#默认安装目录：/usr/lib/rabbitmq/lib/rabbitmq-server-3.6.6.1
	
	rabbitmq-plugins enable rabbitmq_management
}

install_mysql(){
	mkdir -p /data1/mysql/logs
	mkdir -p /data1/mysql/data
	yum -y install mysql-community-server
	mv /etc/my.cnf /etc/my.cnf_backup
	wget -P /etc/ http://download.icloudstudio.com/lnmp/config/mysql/my.cnf
}

service_activate(){
	systemctl enable jexus
	systemctl restart jexus
	systemctl enable mysql
	systemctl restart mysql
	systemctl enable memcached
	systemctl restart memcached
	systemctl enable rabbitmq-server
	systemctl restart rabbitmq-server
}

install_main(){
	system_init
	download_rpm
	yum_setting

	install_mono
	install_jexus
	install_mysql
	install_memcached
	install_rabbitmq

	permission_setting
	service_activate
}

install_main

echo 'complete...'

