#!/bin/bash +x

install_main(){
	system_init
	yum_setting

	# enable_proxy

	install_apache
	install_mysql
	install_php

	# disable_proxy

	permission_setting
	service_activate
}

system_init(){
	echo 'system inti...'
	
	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

	systemctl stop firewalld.service
	systemctl disable firewalld.service
	yum -y upgrade 
	yum install -y yum-plugin-fastestmirror 
	yum install -y gcc gcc-c++ wget lrzsz zip unzip vim
}

yum_setting(){
	echo 'yum setting...'
	rpm -ivh http://download.icloudstudio.com/rpm/epel-release-7-10.noarch.rpm
	rpm -Uvh http://download.icloudstudio.com/rpm/webtatic-release.rpm
	rpm -Uvh http://download.icloudstudio.com/rpm/remi-release-7.rpm

	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
}

install_apache(){
	echo 'install apache...'
	mkdir -p /data1/www/
	mkdir -p /data1/www/logs

	yum -y install httpd mod_ssl 
	mv /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf_backup
	wget -P /etc/httpd/conf/ http://download.icloudstudio.com/lamp70/config/apache/httpd.conf
}

install_mysql(){
	mkdir -p /data1/mysql/logs
	mkdir -p /data1/mysql/data

	rpm -Uvh http://download.icloudstudio.com/rpm/mysql-community-release-el7.rpm
	yum -y install mysql-community-server

	mv /etc/my.cnf /etc/my.cnf_backup
	wget -P /etc/ http://download.icloudstudio.com/lamp70/config/mysql/my.cnf
}

install_php(){
	echo 'install php-fpm...'

	rpm -Uvh http://download.icloudstudio.com/rpm/ius-release-1.0-14.ius.centos7.noarch.rpm

	mkdir -p /data1/php/session
	mkdir -p /data1/php/wsdlcache

	yum -y install php70u php70u-opcache php70u-gd php70u-mcrypt php70u-mysqlnd php70u-xml php70u-mbstring php70u-pdo php70u-json php70u-intl php70u-devel php70u-bcmath openssl

	mv /etc/php.ini /etc/php.ini_backup
	wget -P /etc/ http://download.icloudstudio.com/lamp70/config/php/php.ini

	mv /etc/php-zts.d/10-opcache.ini /etc/php-zts.d/10-opcache.ini_backup
	wget -P /etc/ http://download.icloudstudio.com/lamp70/config/php/php-zts.d/10-opcache.ini

	mv /etc/php.d/10-opcache.ini /etc/php.d/10-opcache.ini_backup
	wget -P /etc/ http://download.icloudstudio.com/lamp70/config/php/php.d/10-opcache.ini
}

permission_setting(){
	echo 'permission setting...'
	
	chown -R mysql:mysql /data1/mysql
	chown -R :apache /data1/www
}

service_activate(){
	echo 'service activate...'

	systemctl enable httpd
	systemctl restart httpd
	systemctl enable mysql
	systemctl restart mysql
	systemctl enable mysqld
	systemctl restart mysqld
}

enable_proxy(){
	cp /etc/profile /etc/profile_backup
	echo 'export http_proxy=http://ecs.icloudstudio.com:9394' >> /etc/profile
	echo 'export https_proxy=http://ecs.icloudstudio.com:9394' >> /etc/profile
	echo 'export ftp_proxy=http://ecs.icloudstudio.com:9394' >> /etc/profile
	source /etc/profile
}

disable_proxy(){
	rm -rf /etc/profile
	mv /etc/profile_backup /etc/profile
	source /etc/profile
}

install_main

echo 'complete...'

