#!/bin/bash
source /etc/profile
echo "xxl-job,端口48080按任意键继续"
read -n1
yum install wget
mkdir -p /data/software
cd /data/software
curl -O http://down.yadeaiot.net:8000/2.1.2.tar.gz
tar -xvf /data/software/2.1.2.tar.gz
cd /data/software/xxl-job-2.1.2
yum -y install mariadb
read -p "input mysql host ;e.g: 127.0.0.1(回车默认为127.0.0.1) : " mysql_host
read -p "input mysql port ;e.g: 3306(回车默认为3306) : " mysql_port
read -p "input mysql user ;eg: root(回车默认为root) : " mysql_user
read -p "input mysql password : " mysql_passwd
if [ ! $mysql_host ];then
mysql_host=127.0.0.1
fi
if [ ! $mysql_port ];then
mysql_port=3306
fi
if [ ! $mysql_user ];then
mysql_user=root
fi
if [ ! $mysql_passwd ];then
echo "数据库密码不能为空,mysql passwd is not null,请重新输入"
read -p "input mysql password : " mysql_passwd
        if [ ! $mysql_passwd ];then
                echo "数据库密码未输入，退出安装，NO PASSWD , Bye..."
                exit;
        fi
fi
echo $mysql_host
echo $mysql_port
echo $mysql_user
echo $mysql_passwd
echo "执行数据库"
mysql --host=$mysql_host --port=$mysql_port --user=$mysql_user --password=$mysql_passwd  </data/software/xxl-job-2.1.2/doc/db/tables_xxl_job.sql
echo "修改配置"
sed -i 's/8080/48080/g' /data/software/xxl-job-2.1.2/xxl-job-admin/src/main/resources/application.properties
sed -i "s/127.0.0.1:3306/$mysql_host:$mysql_port/g" /data/software/xxl-job-2.1.2/xxl-job-admin/src/main/resources/application.properties
#sed -i "s/root/$mysql_user/g" /data/software/xxl-job-2.1.2/xxl-job-admin/src/main/resources/application.properties
sed -i "s/root_pwd/$mysql_passwd/g" /data/software/xxl-job-2.1.2/xxl-job-admin/src/main/resources/application.properties
sed -i "s/root/$mysql_user/g" /data/software/xxl-job-2.1.2/xxl-job-admin/src/main/resources/application.properties
mkdir -p /data/applogs/xxl-job/
sed -i 's/8081/48081/g' /data/software/xxl-job-2.1.2/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties
ip2=`ip address | grep inet|grep -v "127.0.0.1" |grep -v "inet6" | awk -F "inet " '{print $2}' | awk -F "/" '{print $1}'`
read -p "input xxl-job-admin IP (admin的访问ip)(回车默认为$ip2) : " xxl_adminip
if [ ! $xxl_adminip ];then
xxl_adminip=$ip2
fi
sed -i "s/127.0.0.1:8080/$xxl_adminip:48080/g" /data/software/xxl-job-2.1.2/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties
mkdir -p /data/applogs/xxl-job/

#开始编译
echo "开始编译"
cd /data/software
curl -O http://down.yadeaiot.net:8000/apache-maven-3.6.3-bin.tar.gz
tar -zxvf /data/software/apache-maven-3.6.3-bin.tar.gz -C /usr/local/
echo '
export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=$MAVEN_HOME/bin:$PATH'>>/etc/profile
source /etc/profile
mvn -v
mvn1=`mvn -v|grep Apache`
if [ ! $mvn1 ];then
	echo "mvn安装出了问题，退出了，接下来的编译没执行，请手动安装mvn和执行"
	exit;
fi
cd /data/software/xxl-job-2.1.2
mvn package
if [ ! -f "/data/software/xxl-job-2.1.2/xxl-job-admin/target/xxl-job-admin-2.1.2.jar" ];then
	echo "xxl-job-admin-2.1.2.jar文件不存在，无法启动，请检查"
	exit
fi
echo "启动xxl-job服务，admin访问地址http://$xxl_adminip:48080/xxl-job-admin/  admin  123456"
echo "登录后，请配置可用的执行器，$xxl_adminip:9999"
nohup java -jar /data/software/xxl-job-2.1.2/xxl-job-admin/target/xxl-job-admin-2.1.2.jar &
nohup java -jar /data/software/xxl-job-2.1.2/xxl-job-executor-samples/xxl-job-executor-sample-springboot/target/xxl-job-executor-sample-springboot-2.1.2.jar &
chmod +x /etc/rc.local
echo "设置开机启动"
echo '
nohup java -jar /data/software/xxl-job-2.1.2/xxl-job-admin/target/xxl-job-admin-2.1.2.jar &
nohup java -jar /data/software/xxl-job-2.1.2/xxl-job-executor-samples/xxl-job-executor-sample-springboot/target/xxl-job-executor-sample-springboot-2.1.2.jar &' >>/etc/rc.local
