#!/usr/bin/env bash
# @author juntao
# @2022.03.29
# 执行 dokit_build.sh
# 默认会把 library/LGLibrary 的代码更新到最新
# 打出来的包会放在 test-app 目录

git_sha=`git rev-parse --short HEAD`
version_name=$(awk -v FS="versionName = " 'NF>1{print $2}' dependencies.gradle | sed "s/\"//g")
version_code=$(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

./gradlew --stop
./gradlew clean

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

cd libraries/LGLibrary/
git checkout master
git pull origin master

cd ..
cd ..

if [[ "$OSTYPE" == "darwin"* ]]; then
  sed -i '' 's/buildConfigField "long", "BUILD_TIME", "0"/buildConfigField "long", "BUILD_TIME", '"\"${build_time_without_divider}\""'/g' module_common/build.gradle
  ### jenkins 包添加 dokit 供设计师查看颜色啥的
  sed -i '' '/debugImplementation "io.github.didi.dokit:dokitx:${dokit}"/c\    implementation "io.github.didi.dokit:dokitx:${dokit}"' app/build.gradle
  ### dokit 自带 okhttp 会破坏正式包的字节码修改
  sed -i '' '/project.apply plugin: "com.gh.gamecenter.plugin"/c\     ' init.gradle
else
  sed -i 's/buildConfigField "long", "BUILD_TIME", "0"/buildConfigField "long", "BUILD_TIME", '"\"${build_time_without_divider}\""'/g' module_common/build.gradle
  ### jenkins 包添加 dokit 供设计师查看颜色啥的
  sed -i '/debugImplementation "io.github.didi.dokit:dokitx:${dokit}"/c\    implementation "io.github.didi.dokit:dokitx:${dokit}"' app/build.gradle
  ### dokit 自带 okhttp 会破坏正式包的字节码修改
  sed -i '/project.apply plugin: "com.gh.gamecenter.plugin"/c\     ' init.gradle
fi

./gradlew aIR -I init.gradle
mkdir -p test-app/${version_name}_${version_code}
cp -R app/build/outputs/apk/internal/release/app-internal-release.apk test-app/${version_name}_${version_code}/光环助手_${version_name}_${version_code}_dokit调试包_${git_sha}_${build_time}.apk

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