#!/usr/bin/env bash
# @author juntao
# @2023.03.06

git_sha=`git rev-parse --short HEAD`
versionName=$(awk -v FS="versionName = " 'NF>1{print $2}' dependencies.gradle | sed "s/\"//g")
versionCode=$(awk -v FS="versionCode = " 'NF>1{print $2}' dependencies.gradle | sed "s/\"//g")
build_time=$(TZ=Asia/Shanghai date +'%Y-%m%d-%H%M')

cwd=$(cd "$(dirname "$0")"; pwd)
apk_release_path=""

# 重置 app build.gradle
git checkout app/build.gradle
# 重置 module_common build.gradle
git checkout module_common/build.gradle
# 重置 module_sensors_data build.gradle
git checkout module_sensors_data/build.gradle

# 开启 mapping 上传
if [[ "$OSTYPE" == "darwin"* ]]; then
  sed -i '' '1 a plugins { id "io.sentry.android.gradle" version "2.1.5" }' app/build.gradle
  sed -i '' 's/var isTestBuild = true/var isTestBuild = false/g' module_sensors_data/build.gradle
else
  sed -i '1 a plugins { id "io.sentry.android.gradle" version "2.1.5" }' app/build.gradle
  sed -i 's/var isTestBuild = true/var isTestBuild = false/g' module_sensors_data/build.gradle
fi

./gradlew --stop
./gradlew clean

mkdir -p release/

OPTIONS=$(getopt -o '' -l config_id:,sdk_platform:,sdk_version:,app_id:,app_name:,channel:,activate_reporting_ratio:,first_launch_jump:,output:,unix_timestamp:, -- "$@")

eval set -- "$OPTIONS"

while true; do
  case "$1" in
    --config_id) config_id="$2"; shift 2;;
    --sdk_platform) sdk_platform="$2"; shift 2;;
    --sdk_version) sdk_version="$2"; shift 2;;
    --channel) channel="$2"; shift 2;;
    --activate_reporting_ratio) activate_reporting_ratio="$2"; shift 2;;
    --first_launch_jump) first_launch_jump="$2"; shift 2;;
    --output) output="$2"; shift 2;;
    --unix_timestamp) unix_timestamp="$2"; shift 2;;
    --app_id) app_id="$2"; shift 2;;
    --app_name) app_name="$2"; shift 2;;
    --) shift; break;;
    *) echo "Invalid option: $1" >&2; exit 1;;
  esac
done

function updateChannelIfNeeded {
    if [ "${channel}" != "" ]; then
        java -jar ${cwd}/ApkChannelPackage.jar put -c $channel $1 release
        rm $1
        find . -type f -name "*.apk" -exec mv {} "${apk_release_path}" \;
    fi
}

# 保存 output 文件名
if [ "${output}" != "" ]; then
    apk_release_path="$output"
fi

# 保存 config_id
if [ "${config_id}" != "" ]; then
    sed -i "s/String CONFIG_ID = \"\"/String CONFIG_ID = \"${config_id}\"/g" app/build.gradle
fi

# 保存 first_launch_jump
if [ "${first_launch_jump}" != "" ]; then
    sed -i "s/String FIRST_LAUNCH = \"\"/String FIRST_LAUNCH = \"${first_launch_jump}\"/g" app/build.gradle
fi

# 保存快手 app_id
if [ "${app_id}" != "" ]; then
    sed -i "s/String KUAI_SHOU_APP_ID = \"\"/String KUAI_SHOU_APP_ID = \"${app_id}\"/g" app/build.gradle
fi

# 保存快手 app_name
if [ "${app_name}" != "" ]; then
    sed -i "s/String KUAI_SHOU_APP_NAME = \"\"/String KUAI_SHOU_APP_NAME = \"${app_name}\"/g" app/build.gradle
fi

# 是否选择了 sdk 类型
if [ "${sdk_platform}" != "" ]; then
    # 头条包
    if [ "${sdk_platform}" == "toutiao" ]; then
        if [ "${activate_reporting_ratio}" == "" ]; then
            activate_reporting_ratio="100"
        fi
        # 调整上报比例
        sed -i "s/int ACTIVATE_REPORTING_RATIO = 0/int ACTIVATE_REPORTING_RATIO = ${activate_reporting_ratio}/g" app/build.gradle

        if [ "${sdk_version}" == "5.3.0" ]; then
            sed -i "s/bytedanceApplog = \"6.14.3\"/bytedanceApplog = \"${sdk_version}\"/g" dependencies.gradle
            rm app/src/tea/java/com/gh/gamecenter/TeaHelper.kt
            mv app/src/tea/java/com/gh/gamecenter/TeaHelper app/src/tea/java/com/gh/gamecenter/TeaHelper.kt
        else
            sed -i "s/bytedanceApplog = \"6.14.3\"/bytedanceApplog = \"${sdk_version}\"/g" dependencies.gradle
        fi

        ./gradlew aTR -I init.gradle

        echo "${sdk_version}_${activate_reporting_ratio}_光环助手_${versionName}_${versionCode}_头条推广包_${git_sha}_${build_time}"

        cp -R app/build/outputs/apk/tea/release/app-tea-release.apk "${apk_release_path}"
        fi
    # 广点通包
    if [ "${sdk_platform}" == "guangdiantong" ]; then

        # 广点通 SDK 执行 ASM 后处理后会有问题
        ./gradlew aGR

        echo "光环助手_${versionName}_${versionCode}_广点通推广包_${git_sha}_${build_time}"
        cp -R app/build/outputs/apk/gdt/release/app-gdt-release.apk "${apk_release_path}"
    fi
    # 快手包
    if [ "${sdk_platform}" == "kuaishou" ]; then
        ./gradlew aKR -I init.gradle

        echo "光环助手_${versionName}_${versionCode}_快手推广包_${git_sha}_${build_time}"
        cp -R app/build/outputs/apk/kuaishou/release/app-kuaishou-release.apk "${apk_release_path}"
    fi

    updateChannelIfNeeded ${apk_release_path}
fi

# 重置 app build.gradle
git checkout app/build.gradle
# 重置 module_common build.gradle
git checkout module_common/build.gradle
# 重置 module_sensors_data build.gradle
git checkout module_sensors_data/build.gradle