#!/bin/bash
# This is flutter build

projectDir=`pwd`
rootFlutter=`which flutter`
rootDir=${rootFlutter%/*}
# 0 true 1 false
buildDebug=1
upload=0
suff=debug


dif=debug
#dif=release
message=埋点字段写反问题

if [ $buildDebug -eq  0 ]; then
    suff=debug
else
    suff=release
fi



#=====更新版本号 TODO
#cd /Users/apple/lsy/MergeTest
#v=`grep ext.PUBLISH_VERSION build.gradle|cut -d'=' -f2`
#v0=`echo $v | awk '{split($0,a," ");print a[2]}'`
#echo 旧版本号$v0
#
#v1=`echo $v0 | awk '{split($0,a,".");print a[1]}'`
#v2=`echo $v0 | awk '{split($0,a,".");print a[2]}'`
#v3=`echo $v0 | awk '{split($0,a,".");print a[3]}'`
#y=`expr $v3 + 1`
#vv=$v1"."$v2"."$y
#echo 新版本号$vv
#sed -i '.bak' 's/'$v0'/'$vv'/g' build.gradle
#if [ $? -eq 0 ]; then
#    echo ''
#else
#    echo '更新版本号失败...'
#    exit
#fi

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

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


#====build android
${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 [ $buildDebug -eq  0 ]; then
    ./gradlew assembleDebug
else
    ./gradlew assembleRelease
fi
cd ..

rm -rf /Users/apple/lsy/aar/
mkdir /Users/apple/lsy/aar/
rm -rf /Users/apple/lsy/aar_update/
mkdir /Users/apple/lsy/aar_update/
mkdir /Users/apple/lsy/aar_update/androd${message}/
mkdir /Users/apple/lsy/aar_update/ios${message}/
cp -r /Users/apple/lsy/gmalpha_flutter/build/app/outputs/aar/app-${suff}.aar /Users/apple/lsy/MergeTest/app/libs/app.aar
cp -r /Users/apple/lsy/gmalpha_flutter/build/flutter_boost/outputs/aar/flutter_boost-${suff}.aar /Users/apple/lsy/MergeTest/app/libs/flutter_boost.aar
cp -r /Users/apple/lsy/gmalpha_flutter/build/gengmei_flutter_plugin/outputs/aar/gengmei_flutter_plugin-${suff}.aar /Users/apple/lsy/MergeTest/app/libs/gengmei_flutter_plugin.aar
cp -r /Users/apple/lsy/gmalpha_flutter/build/path_provider/outputs/aar/path_provider-${suff}.aar /Users/apple/lsy/MergeTest/app/libs/path_provider.aar
cp -r /Users/apple/lsy/gmalpha_flutter/build/sqflite/outputs/aar/sqflite-${suff}.aar /Users/apple/lsy/MergeTest/app/libs/sqflite.aar
cp -r /Users/apple/lsy/gmalpha_flutter/build/xservice_kit/outputs/aar/xservice_kit-${suff}.aar /Users/apple/lsy/MergeTest/app/libs/xservice_kit.aar

cp -r /Users/apple/lsy/gmalpha_flutter/build/app/outputs/aar/app-${suff}.aar /Users/apple/lsy/aar/app-${suff}.zip
#mv /Users/apple/lsy/gmalpha_flutter/build/app/outputs/aar/app-debug.aar /Users/apple/lsy/aar/app-debug.zip
cd /Users/apple/lsy/aar
unzip /Users/apple/lsy/aar/app-${suff}.zip
cp -r /Users/apple/lsy/aar/jni/armeabi-v7a/* /Users/apple/lsy/MergeTest/app/libs/jniLibs/armeabi-v7a/
cp -r /Users/apple/lsy/aar/jni/armeabi-v7a/* /Users/apple/lsy/Test/app/libs/armeabi-v7a/
cp -r /Users/apple/lsy/aar/jni/armeabi-v7a/* /Users/apple/lsy/gmalpha_flutter/build/
cp -r /Users/apple/lsy/aar/jni/armeabi-v7a/* /Users/apple/lsy/aar_update/androd${message}/

#====upload
#cd /Users/apple/lsy/MergeTest
#./gradlew clean
#./gradlew assembleRelease
#./gradlew uploadArchives
#cp -r /Users/apple/lsy/MergeTest/app/build/outputs/aar/app.aar /Users/apple/lsy/Test/app/libs/
#cp -r /Users/apple/lsy/MergeTest/app/build/outputs/aar/app.aar /Users/apple/lsy/gmalpha_flutter/build/

#cd /Users/apple/lsy/flutter_aar
#git add .
#git commit -m "commit "
#git push



# ios!!!
cd ${projectDir}
flutter build ios --release --no-codesign
cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/App.framework /Users/apple/lsy/aar_update/ios${message}/
cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/Flutter.framework /Users/apple/lsy/aar_update/ios${message}/
cd /Users/apple/lsy/aar_update/
open .


#cd ios/Pods
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target flutter_boost BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target FMDB BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target gengmei_flutter_plugin BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target package_info BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target path_provider BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target shared_preferences BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target sqflite BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target xservide_kit BUILD_DIR=../../build/ios -sdk iphoneos -quiet

#/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target Flutter BUILD_DIR=../../build/ios -sdk iphoneos -quiet


#echo "IOS build "
#
#${rootFlutter} build ios --release
#
#echo "===生成各个plugin的二进制库文件==="
#cd ios/Pods
##/usr/bin/env xcrun xcodebuild clean
##/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' BUILD_AOT_ONLY=YES VERBOSE_SCRIPT_LOGGING=YES -workspace Runner.xcworkspace -scheme Runner BUILD_DIR=../build/ios -sdk iphoneos
#for plugin_name in ${plugin_arr}
#do
#    echo "生成lib${plugin_name}.a..."
#    /usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target ${plugin_name} BUILD_DIR=../../build/ios -sdk iphoneos -quiet
#    /usr/bin/env xcrun xcodebuild build -configuration Debug ARCHS='x86_64' -target ${plugin_name} BUILD_DIR=../../build/ios -sdk iphonesimulator -quiet
#    echo "合并lib${plugin_name}.a..."
#    lipo -create "../../build/ios/Debug-iphonesimulator/${plugin_name}/lib${plugin_name}.a" "../../build/ios/Release-iphoneos/${plugin_name}/lib${plugin_name}.a" -o "../../build/ios/Release-iphoneos/${plugin_name}/lib${plugin_name}.a"
#done
#
#echo "===生成注册入口的二进制库文件==="
#for reg_enter_name in "flutter_plugin_entrance" "flutter_service_register"
#do
#    echo "生成lib${reg_enter_name}.a..."
#    /usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target ${reg_enter_name} BUILD_DIR=../../build/ios -sdk iphoneos
#    /usr/bin/env xcrun xcodebuild build -configuration Debug ARCHS='x86_64' -target ${reg_enter_name} BUILD_DIR=../../build/ios -sdk iphonesimulator
#    echo "合并lib${reg_enter_name}.a..."
#    lipo -create "../../build/ios/Debug-iphonesimulator/${reg_enter_name}/lib${reg_enter_name}.a" "../../build/ios/Release-iphoneos/${reg_enter_name}/lib${reg_enter_name}.a" -o "../../build/ios/Release-iphoneos/${reg_enter_name}/lib${reg_enter_name}.a"
#done


#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