#!/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 audio tagging APK for sherpa-onnx v${SHERPA_ONNX_VERSION}"

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

export SHERPA_ONNX_ENABLE_TTS=OFF

mkdir -p apks

{% for model in model_list %}
pushd ./android/SherpaOnnxAudioTagging/app/src/main/assets/
model_name={{ model.model_name }}
short_name={{ model.short_name }}
type={{ model.idx }}

curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/${model_name}.tar.bz2
tar xvf ${model_name}.tar.bz2
rm -rfv $model_name/model.onnx
rm -rfv $model_name/test_wavs
rm -rf  *.tar.bz2
ls -lh $model_name

popd
# Now we are at the project root directory

git checkout .
pushd android/SherpaOnnxAudioTagging/app/src/main/java/com/k2fsa/sherpa/onnx/audio/tagging/
sed -i.bak s/"type = 0/type = $type/" ./Tagger.kt
git diff
popd

for arch in arm64-v8a armeabi-v7a x86_64 x86; do
  log "------------------------------------------------------------"
  log "build audio tagging 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/SherpaOnnxAudioTagging/app/src/main/jniLibs/$arch/

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

  mv android/SherpaOnnxAudioTagging/app/build/outputs/apk/release/app-release-unsigned.apk ./apks/sherpa-onnx-${SHERPA_ONNX_VERSION}-$arch-audio-tagging-$short_name.apk
  ls -lh apks
  rm -v ./android/SherpaOnnxAudioTagging/app/src/main/jniLibs/$arch/*.so
done

rm -rf ./android/SherpaOnnxAudioTagging/app/src/main/assets/$model_name
{% endfor %}

git checkout .

ls -lh apks/
