#!/bin/bash

# 定义变量
MOUNT_DIR="/mnt/hgfs/workspace"
NEW_IP="192.168.1.10"

# 配置文件路径
CONFIG_FILES=(
    "/usr/local/cdn/config/config.xml"
    "/home/bsy/workplace/boshiyuan-api-viewwatch/config/application.properties"
    "/home/bsy/workplace/boshiyuan-api-viewwatch/config/application-prod.properties"
    "/usr/local/nginx/html/dist/static/config.js"
)

# 目录配置
declare -A DIRECTORIES=(
    ["CHNLDATA_DIR"]="$MOUNT_DIR/chnldata"
    ["FRAGS_DIR"]="$MOUNT_DIR/chnldata/frags"
    ["DOWNLOAD_DIR"]="$MOUNT_DIR/media/download"
    ["UPLOAD_DIR"]="$MOUNT_DIR/boshiyuan-api-viewwatch/upload"
)

# 创建目录函数
create_directories() {
    for dir in "${DIRECTORIES[@]}"; do
        mkdir -p "$dir"
        echo "创建或确认目录: $dir"
    done
}

# 更新配置文件函数
update_config_file() {
    local file=$1
    case $file in
        *.xml)
            sed -i "s|<dir>/home/chnldata</dir>|<dir>${DIRECTORIES[CHNLDATA_DIR]}</dir>|g" "$file"
            sed -i "s|<fragsdir>/home/chnldata/frags</fragsdir>|<fragsdir>${DIRECTORIES[FRAGS_DIR]}</fragsdir>|g" "$file"
            sed -i "s|<file_path>/home/media/download</file_path>|<file_path>${DIRECTORIES[DOWNLOAD_DIR]}</file_path>|g" "$file"
            ;;
        *application.properties)
            sed -i 's/spring\.jpa\.show-sql=true/spring.jpa.show-sql=false/' "$file"
            sed -i '/mybatis\.configuration\.map-underscore-to-camel-case=true/a mybatis.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl' "$file"
            sed -i '/^#log4j/d' "$file"
            sed -i '/^logging\.level\.com\.boshiyuan\.mapper = debug/d' "$file"
            echo -e "\n# logging\nlogging.level.root = info\nlogging.level.com.boshiyuan = info\nlogging.level.com.boshiyuan.mapper = warn\nlogging.level.org.springframework = warn\nlogging.level.org.hibernate = warn" >> "$file"
            ;;
        *application-prod.properties)
            sed -i "s/fileupload\.url=http:\/\/[0-9.]\+/fileupload.url=http:\/\/$NEW_IP/" "$file"
            sed -i "s|fileupload\.mediaRoot=/home/chnldata/root/|fileupload.mediaRoot=${DIRECTORIES[CHNLDATA_DIR]}/root/|" "$file"
            sed -i "s|fileupload\.uploadPath=.*|fileupload.uploadPath=${DIRECTORIES[UPLOAD_DIR]}/|" "$file"
            grep -q "fileupload.uploadPath" "$file" || echo "fileupload.uploadPath=${DIRECTORIES[UPLOAD_DIR]}/" >> "$file"
            ;;
        *config.js)
            sed -i "s/\"baseUrl\":\"http:\/\/[0-9.]\+:10432\//\"baseUrl\":\"http:\/\/$NEW_IP:10432\//" "$file"
            ;;
    esac
    echo "更新配置文件: $file"
}

# 主函数
main() {
    create_directories
    for file in "${CONFIG_FILES[@]}"; do
        update_config_file "$file"
    done
}

main
