#!/bin/bash
# This is flutter build

projectDir=`pwd`
rootFlutter=`which flutter`
rootDir=${rootFlutter%/*}

#${rootFlutter} build apk

#  ======old=====
#cd ${rootDir}/cache/artifacts/engine/android-arm-release
#rm -rf lib
#rm -rf io
#unzip flutter.jar
#mkdir ${projectDir}/android/app/src/main/assets
#rm -rf ${projectDir}/android/app/src/main/assets/io
rm -rf ${projectDir}/android/app/src/main/assets/lib

#mv ${pwd}io/* ${projectDir}/android/app/src/main/assets/io/
#mv ${pwd}lib/* ${projectDir}/android/app/src/main/assets/lib/
#cp ${pwd}darwin-x64/gen_snapshot ${projectDir}/android/app/src/main/assets/gen_snapshot
#cd ..
#cd darwin-x64/
#cp ${pwd}icudtl.dat ${projectDir}/android/app/src/main/assets/



cd ${projectDir}
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
${rootFlutter} packages get
${rootFlutter} clean

${rootFlutter} build aot --release --output-dir=build/flutteroutput/aot
${rootFlutter} build bundle --precompiled --asset-dir=build/flutteroutput/flutter_assets


rm -f -r android/packflutter/flutter/
echo 'Copy flutter jar'
mkdir -p android/packflutter/flutter/flutter/android-arm-release && cp ${rootDir}/cache/artifacts/engine/android-arm-release/flutter.jar "$_"
echo 'Copy flutter asset'
mkdir -p android/packflutter/flutter/assets/release && cp -r build/flutteroutput/aot/* "$_"
mkdir -p android/packflutter/flutter/assets/release/flutter_assets && cp -r build/flutteroutput/flutter_assets/* "$_"
echo 'Build and publish idlefish flutter to aar'
cd android
#if [ -n "$1" ]
#then
#    ./gradlew :packflutter:clean :packflutter:publish -PAAR_VERSION=$1
#else
#    ./gradlew :packflutter:clean :packflutter:publish
#fi
#cd ../

#cd android/
#./gradlew assembleRelease
#./gradlew assembleDebug

#cd ..
./gradlew uploadArchives
cd /Users/sylin/Downloads/flutter_aar
git add .
git commit -m "commit "
git push


#test
#cd ${PWD}/build/app/outputs/aar/
#open .

#cp ${PWD}/build/app/outputs/aar/app-release.aar /Users/sylin/lsy/PLVideo/app/libs/
#cp ${PWD}/build/app/outputs/aar/app-debug.aar /Users/sylin/lsy/PLVideo/app/libs/

exit