#!/bin/bash

# 安装JDK
echo "正在安装JDK..."
yum install -y java-1.8.0-openjdk-devel.x86_64

echo "检查JDK安装情况..."
JDK_NAME=$(ls /usr/lib/jvm | grep java-1.8.0-openjdk-)
if [ -z "$JDK_NAME" ]; then
    echo "JDK安装失败，退出..."
    exit 1
else
    echo "已安装JDK: $JDK_NAME"
fi

echo "配置JDK环境变量..."
cat <<EOL >> /etc/profile
export JAVA_HOME=/usr/lib/jvm/$JDK_NAME/
export PATH=\$PATH:\$JAVA_HOME/bin
EOL

echo "重新加载配置文件..."
source /etc/profile

echo "检查JDK版本..."
java -version

# 安装Maven
echo "正在安装Maven..."
yum -y install maven-3.0.5-17.el7.noarch

echo "检查Maven安装情况..."
if [ ! -d "/usr/share/maven" ]; then
    echo "Maven安装失败，退出..."
    exit 1
else
    echo "已安装Maven在 /usr/share/maven"
fi

echo "创建Maven仓库目录..."
mkdir -p /usr/share/maven/repository

echo "配置Maven设置..."
MAVEN_SETTINGS_FILE="/usr/share/maven/conf/settings.xml"

# 提取第53行内容并修改
modified_line="  <localRepository>/usr/share/maven/repository</localRepository>"

# 使用awk将修改后的内容插入到第55行并增加一个空行，同时删除第53行
awk -v mod_line="$modified_line" '{
    if (NR == 53) next; # 跳过第53行
    if (NR == 55) {
        print mod_line;
        print "";
    }
    print $0;
}' $MAVEN_SETTINGS_FILE > temp_file && mv temp_file $MAVEN_SETTINGS_FILE

# 定义要插入的内容
insert_content='    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>'

# 使用awk在第159行之后插入内容
awk -v insert="$insert_content" 'NR == 159 {print; print insert; next} {print}' $MAVEN_SETTINGS_FILE > temp_file && mv temp_file $MAVEN_SETTINGS_FILE

echo "配置Maven环境变量..."
cat <<EOL >> /etc/profile
export MAVEN_HOME=/usr/share/maven/
export PATH=\$MAVEN_HOME/bin:\$PATH
EOL

echo "重新加载配置文件..."
source /etc/profile

echo "检查Maven版本..."
mvn -version

# 安装Node.js
echo "正在安装Node.js..."
NODE_VERSION="v14.2.0"
NODE_DIST="node-${NODE_VERSION}-linux-x64"
wget "https://nodejs.org/dist/${NODE_VERSION}/${NODE_DIST}.tar.gz"

echo "创建Node.js安装目录..."
mkdir -p /usr/local/node

echo "解压Node.js..."
tar -zxvf "${NODE_DIST}.tar.gz" -C /usr/local/node

echo "检查Node.js解压情况..."
if [ ! -d "/usr/local/node/${NODE_DIST}" ]; then
    echo "Node.js解压失败，退出..."
    exit 1
else
    echo "Node.js已解压到 /usr/local/node/${NODE_DIST}"
fi

echo "为Node.js和npm创建符号链接..."
ln -s /usr/local/node/${NODE_DIST}/bin/node /usr/local/bin/node
ln -s /usr/local/node/${NODE_DIST}/bin/npm /usr/local/bin/npm

echo "配置Node.js环境变量..."
cat <<EOL >> /etc/profile
export NODE_HOME=/usr/local/node/${NODE_DIST}/bin/
export PATH=\$PATH:\$NODE_HOME:/usr/local/bin/
EOL

echo "重新加载配置文件..."
source /etc/profile

echo "检查Node.js和npm版本..."
node -v
npm -v

echo "安装和配置完成。"

