#!/bin/bash 
# 当前目录
CURRENT_DIR=$(
   cd "$(dirname "$0")"
   pwd
)

#安装jdk
installJdk(){
    if which java >/dev/null; then
		echo "检测到 jdk 已安装，跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/jdk-8u181-linux-x64.tar.gz" ]]; then
			echo "... 离线安装jdk"
			cd $CURRENT_DIR && tar -zxf jdk-8u181-linux-x64.tar.gz 
			cp /etc/profile $(pwd) && cd jdk1.8.0_181
			cat >> /etc/profile <<EOF
			export JAVA_HOME=$PWD 
			export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
			export PATH=\$PATH:\$JAVA_HOME/bin
EOF
			ln -s $(pwd)/bin/java /usr/bin/java
			source /etc/profile && java -version
			echo "... 离线安装jdk 完成"
		else
			echo "... 在线安装jdk"
			sudo yum install -y java-1.8.0-openjdk-devel
			java -version
			echo "... 在线安装jdk 完成"
		fi
	fi
}

#安装maven
installMaven(){
	if which mvn >/dev/null; then
		echo "检测到 maven 已安装，跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/apache-maven-3.6.3-bin.tar.gz" ]]; then
			echo "... 离线安装maven"
		else
			echo "... 在线安装 maven"
			wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
		fi
		cd $CURRENT_DIR && tar -zxf apache-maven-3.6.3-bin.tar.gz
		cp /etc/profile $(pwd) && cd apache-maven-3.6.3
		cat >> /etc/profile <<EOF 
		export MAVEN_HOME=$PWD 
		export PATH=\$MAVEN_HOME/bin:\$PATH
EOF
		source /etc/profile && mvn -v 
		echo "... maven安装完成"
	fi
}

# 安装node
installNode(){
    if which node >/dev/null; then
		echo "检测到 node 已安装，跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/node-v12.14.0-linux-x64.tar.xz" ]]; then
			echo "... 离线安装node"
		else
			echo "... 在线安装 node"
			cd $CURRENT_DIR && wget https://nodejs.org/dist/v12.14.0/node-v12.14.0-linux-x64.tar.xz
		fi
		cd $CURRENT_DIR && tar -xf node-v12.14.0-linux-x64.tar.xz
		cp /etc/profile $(pwd) && cd node-v12.14.0-linux-x64
		cat >> /etc/profile <<EOF 
		export NODE_HOME=$PWD 
		export PATH=\$PATH:\$NODE_HOME/bin 
EOF
		source /etc/profile && node -v && npm -v
		echo "... node安装完成"
	fi
}

#install cnpm、yarn
installCnpm(){
	echo " install cnpm、yarn 开始"
	npm install -g cnpm --registry=https://registry.npm.taobao.org
	cnpm install -g yarn
	yarn config set registry https://registry.npm.taobao.org
	echo " install cnpm、yarn 完成"
}

#jenkins 安装
installOffineJenkins(){
	# YUM安装 
	# 卸载 rpm -e jenkins-2.138.4-1.1.noarch 
	echo " jenkins安装 开始"
	cd $CURRENT_DIR && yum -y localinstall jenkins-2.235.1-1.1.noarch.rpm
	#/usr/lib/jenkins/jenkins.war WAR包 
	#/etc/sysconfig/jenkins 配置文件 
	#/var/lib/jenkins/ 默认的JENKINS_HOME目录 
	#/var/log/jenkins/jenkins.log Jenkins日志文件

	#只修改了端口和JENKINS_USER
	sed -i 's/JENKINS_USER="jenkins"/JENKINS_USER="root"/g' /etc/sysconfig/jenkins 

	sed -i 's/JENKINS_PORT="8080"/JENKINS_PORT="9999"/g' /etc/sysconfig/jenkins 


	#创建软连接，修改数据存储位置
	mkdir -p /home/jenkins && rm -rf /var/lib/jenkins && cd /var/lib/ && ln -s /home/jenkins jenkins && chmod 777 /home/jenkins && cd /home/jenkins


	# 开机自启
	sudo systemctl enable jenkins && sudo systemctl restart jenkins
	sleep 60
	# 修改jenkins插件下载源
	while(true)
	do
		if [ -f "/home/jenkins/hudson.model.UpdateCenter.xml" ]; then
			sed -i 's/https/http/g' /home/jenkins/hudson.model.UpdateCenter.xml
			echo "执行成功"
			break
		else
			sudo systemctl restart jenkins
			echo "重启jenkins" 
			sleep 90
		fi
	done
	# 修改jenkins插件下载网络检查链接
	while(true)
	do
		if [ -f "/home/jenkins/updates/default.json" ]; then
			sed -i 's/www.google.com/www.baidu.com/g' /home/jenkins/updates/default.json
			echo "执行成功"
			break
		else
			sudo systemctl restart jenkins
			echo "重启jenkins" 
			sleep 90
		fi
	done

	# 重启jenkins
	systemctl restart jenkins
	echo " jenkins安装 结束"
}

installJenkins(){
	systemctl status jenkins.service;
	if [ $? -eq  0 ]; then
		echo "检测到 jenkins 已安装，跳过安装步骤"
	else
		if [[ -f "$CURRENT_DIR/jenkins-2.235.1-1.1.noarch.rpm" ]]; then
			echo "... 离线安装jenkins"
		else
			echo "... 在线安装 jenkins"
			cd $CURRENT_DIR && wget https://pkg.jenkins.io/redhat-stable/jenkins-2.235.1-1.1.noarch.rpm ||exit
		fi
		installOffineJenkins
	fi
}


installJdk
installMaven
installNode
installCnpm
#安装jenkins
installJenkins
