#!/usr/bin/env bash
#
# Auto generated! Please DO NOT EDIT!

# Please set the environment variable ANDROID_NDK
# before running this script

# Inside the $ANDROID_NDK directory, you can find a binary ndk-build
# and some other files like the file "build/cmake/android.toolchain.cmake"

set -ex

log() {
  # This function is from espnet
  local fname=${BASH_SOURCE[1]##*/}
  echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
}

SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

log "Building streaming ASR two-pass APK for sherpa-onnx v${SHERPA_ONNX_VERSION}"

export SHERPA_ONNX_ENABLE_TTS=OFF

log "====================arm64-v8a================="
./build-android-arm64-v8a.sh
log "====================armv7-eabi================"
./build-android-armv7-eabi.sh
log "====================x86-64===================="
./build-android-x86-64.sh
log "====================x86===================="
./build-android-x86.sh

mkdir -p apks

{% for first, second in model_list %}
pushd ./android/SherpaOnnx2Pass/app/src/main/assets/

model_name1={{ first.model_name }}
model_name=$model_name1
type1={{ first.idx }}
lang1={{ first.lang }}
short_name1={{ first.short_name }}

curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/${model_name1}.tar.bz2
tar xvf ${model_name1}.tar.bz2

{{ first.cmd }}

rm -rf  *.tar.bz2
ls -lh $model_name1

model_name2={{ second.model_name }}
model_name=$model_name2
type2={{ second.idx }}
lang2={{ second.lang }}
short_name2={{ second.short_name }}

curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/${model_name2}.tar.bz2
tar xvf ${model_name2}.tar.bz2

{{ second.cmd }}

rm -rf  *.tar.bz2
ls -lh $model_name2

popd
# Now we are at the project root directory

git checkout .
pushd android/SherpaOnnx2Pass/app/src/main/java/com/k2fsa/sherpa/onnx
sed -i.bak s/"firstType = 9/firstType = $type1/" ./MainActivity.kt
sed -i.bak s/"secondType = 0/secondType = $type2/" ./MainActivity.kt
git diff
popd

for arch in arm64-v8a armeabi-v7a x86_64 x86; do
  log "------------------------------------------------------------"
  log "build ASR apk for $arch"
  log "------------------------------------------------------------"
  src_arch=$arch
  if [ $arch == "armeabi-v7a" ]; then
    src_arch=armv7-eabi
  elif [ $arch == "x86_64" ]; then
    src_arch=x86-64
  fi

  ls -lh ./build-android-$src_arch/install/lib/*.so

  cp -v ./build-android-$src_arch/install/lib/*.so ./android/SherpaOnnx2Pass/app/src/main/jniLibs/$arch/

  pushd ./android/SherpaOnnx2Pass
  sed -i.bak s/2048/9012/g ./gradle.properties
  git diff ./gradle.properties
  ./gradlew assembleRelease
  popd

  mv android/SherpaOnnx2Pass/app/build/outputs/apk/release/app-release-unsigned.apk ./apks/sherpa-onnx-${SHERPA_ONNX_VERSION}-$arch-asr_2pass-$lang1-${short_name1}_${short_name2}.apk
  ls -lh apks
  rm -v ./android/SherpaOnnx2Pass/app/src/main/jniLibs/$arch/*.so
done

rm -rf ./android/SherpaOnnx2Pass/app/src/main/assets/$model_name1
rm -rf ./android/SherpaOnnx2Pass/app/src/main/assets/$model_name2
{% endfor %}

git checkout .

ls -lh apks/
