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

version=$(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')
build_time_without_divider=$(TZ=Asia/Shanghai date +'%Y%m%d%H%M')L

git checkout module_common/build.gradle
git checkout gradle.properties

sed -i '1 a android.enableResourceOptimizations = false' gradle.properties

./gradlew --stop
./gradlew clean

sed -i 's/buildConfigField "long", "BUILD_TIME", "0"/buildConfigField "long", "BUILD_TIME", '"\"${build_time_without_divider}\""'/g' module_common/build.gradle

# 添加 -n 代表全局禁用深色模式
while getopts "n" arg
do
	case ${arg} in
		n)
		  # 移除夜间颜色资源
      rm -r app/src/main/res/values-night
      rm -r app/src/main/res/drawable-night-xxxhdpi
      rm -r app/src/main/res/drawable-night
      rm -r app/src/main/res/drawable-night-nodpi
      rm -r module_core/src/main/res/drawable-night-xxxhdpi
      rm -r module_core/src/main/res/values-night
      rm -r module_common/src/main/res/values-night
      rm -r module_common/src/main/res/drawable-night-xxxhdpi
      rm -r module_common/src/main/res/drawable-night-nodpi
      rm -r module_common/src/main/res/drawable-night
      sed -i 's/buildConfigField "boolean", "IS_DARK_MODE_ON", "true"/buildConfigField "boolean", "IS_DARK_MODE_ON", "false"/g' module_common/build.gradle
		;;
	esac
done

# 生成git log文本
git log --pretty=format:'%s' --max-count=20 --no-merges > app/src/main/assets/gitlog.txt

./gradlew rIR -I init.gradle
mv app/build/outputs/apk/internal/release/app-internal-release.apk app/build/tmp/${version}-${versionCode}-test-${build_time}.apk

git checkout module_common/build.gradle
git checkout gradle.properties
rm app/src/main/assets/gitlog.txt