#!/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

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

sed -i 's/buildConfigField "boolean", "IS_VGAME_ON", "true"/buildConfigField "boolean", "IS_VGAME_ON", "true"/g' module_common/build.gradle

./gradlew --stop
./gradlew clean

while getopts "tg" arg
do
	case ${arg} in
		t)
      ./gradlew aTR -I init.gradle
      mkdir -p release-app/${versionName}_${versionCode}
      cp -R app/build/outputs/apk/tea/release/app-tea-release.apk release-app/${versionName}_${versionCode}/光环助手_${versionName}_${versionCode}_头条推广正式包_${git_sha}_${build_time}.apk
      exit
		;;
		g)
      ./gradlew aGR -I init.gradle
      mkdir -p release-app/${versionName}_${versionCode}
      cp -R app/build/outputs/apk/gdt/release/app-gdt-release.apk release-app/${versionName}_${versionCode}/光环助手_${versionName}_${versionCode}_广点通推广正式包_${git_sha}_${build_time}.apk
      exit
		;;
	esac
done

# 不存在指令时打普通包
./gradlew aPR -I init.gradle
mkdir -p release-app/${versionName}_${versionCode}
cp -R app/build/outputs/apk/publish/release/app-publish-release.apk release-app/${versionName}_${versionCode}/光环助手_${versionName}_${versionCode}_标准正式包_${git_sha}_${build_time}.apk

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