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

post_init_script=init.gradle

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

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

# 添加 -c 代表启用 compose setting 模块
while getopts "c" arg
do
    sed -i '260 a implementation(project(\x27:module_setting_compose\x27)) { exclude group: \x27androidx.swiperefreshlayout\x27 }' app/build.gradle
    post_init_script=init.compose.gradle
done

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

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