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

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')

# 重置 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 '' "1s|^|plugins { id \"io.sentry.android.gradle\" version \"3.7.0\" }\n|" app/build.gradle
else
  sed -i '1 a plugins { id "io.sentry.android.gradle" version "3.7.0" }' app/build.gradle
fi

./gradlew --stop
./gradlew clean

BUILD_VARIANT_ENV=publish
BUILD_VARIANT_REGION=cn
BUILD_APK_CHANNEL_NAME=标准正式包
BUILD_PUSH_TYPE=jg
BUILD_WITH_INIT_GRADLE=true

# 添加 -t 指定编译环境，可选值：t: 头条 k: 快手 g: 广点通
# 添加 -r 指定区域，可选值: cn: 中国大陆 gat: 港澳台
# 添加 -p 指定推送类型 可选值: jg: 极光推送 ac: 阿里云推送
while getopts "t:r:p:" arg
do
	case ${arg} in
		t)
            case $OPTARG in
                t)
                    BUILD_VARIANT_ENV=tea
                    BUILD_APK_CHANNEL_NAME=头条推广正式包
                    ;;
                k)
                    BUILD_VARIANT_ENV=kuaishou
                    BUILD_APK_CHANNEL_NAME=快手推广正式包
                    ;;
                g)
                    sed -i '/android:name="android.permission.READ_PHONE_STATE"/{n;s/tools:node="remove" \/>/\/>/;}' app/src/main/AndroidManifest.xml
                    BUILD_WITH_INIT_GRADLE=false
                    BUILD_VARIANT_ENV=gdt
                    BUILD_APK_CHANNEL_NAME=广点通推广正式包
                    ;;
            esac
		;;
		r) 
            case $OPTARG in
                gat) BUILD_VARIANT_REGION=gat
                  ;;
                *) BUILD_VARIANT_REGION=cn
                  ;;
            esac
		;;
        p)
            case $OPTARG in
                jg) BUILD_PUSH_TYPE=jg
                  ;;
                *) BUILD_PUSH_TYPE=ac
                  ;;
            esac
        ;;
	esac
done

# 不存在指令时打普通包
if [ $BUILD_WITH_INIT_GRADLE == false ]; then
    ./gradlew assemble${BUILD_VARIANT_ENV}${BUILD_VARIANT_REGION^}Release -PBUILD_PUSH_TYPE=${BUILD_PUSH_TYPE}
else
    ./gradlew assemble${BUILD_VARIANT_ENV}${BUILD_VARIANT_REGION^}Release -I init.gradle -PBUILD_PUSH_TYPE=${BUILD_PUSH_TYPE} --no-daemon
fi

mkdir -p release-app/${versionName}_${versionCode}
cp -R app/build/outputs/apk/${BUILD_VARIANT_ENV}${BUILD_VARIANT_REGION^}/release/app-${BUILD_VARIANT_ENV}-${BUILD_VARIANT_REGION}-release.apk release-app/${versionName}_${versionCode}/光环助手_${versionName}_${versionCode}_${BUILD_APK_CHANNEL_NAME}_${git_sha}_${build_time}.apk

# 重置 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