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

#====clean
function 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
}

#====apk android
function android_apk(){
    flutter build apk
    cp -r /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/app-${suff}.apk /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/app-${suff}.zip
    cd /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/
    unzip /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/app-${suff}.zip
    cp -r /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/lib/armeabi-v7a/libapp.so /Users/apple/lsy/gengmei_alpha/alpha/libs/armeabi-v7a
    cp -r /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/lib/armeabi-v7a/libflutter.so /Users/apple/lsy/gengmei_alpha/alpha/libs/armeabi-v7a
    rm -rf /Users/apple/lsy/gengmei_alpha/alpha/src/main/assets/flutter_assets
    cp -r /Users/apple/lsy/gmalpha_flutter/build/app/outputs/apk/${suff}/assets/flutter_assets /Users/apple/lsy/gengmei_alpha/alpha/src/main/assets/

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

}

#====build android
function 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 ..

    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
#    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/libapp.so /Users/apple/lsy/aar_update/androd${message}/
}

function android_publish(){
    cd /Users/apple/lsy/gengmei_alpha
    git checkout dev
    git stash
    git pull
    cp -r /Users/apple/lsy/aar/jni/armeabi-v7a/libapp.so /Users/apple/lsy/gengmei_alpha/alpha/libs/armeabi-v7a
    git status
    git add .
    git add -A
    git commit -m "change app.so"
    git status
    git push
    git checkout test
    git stash
    git pull
    cp -r /Users/apple/lsy/aar/jni/armeabi-v7a/libapp.so /Users/apple/lsy/gengmei_alpha/alpha/libs/armeabi-v7a
    git status
    git add .
    git add -A
    git commit -m "change app.so"
    git status
    git push
}

#====upload
function upload_android_aar(){
    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!!!
function ios(){
    cd ${projectDir}
    flutter build ios --release --no-codesign
    lipo -remove x86_64 /Users/apple/lsy/gmalpha_flutter/ios/Flutter/Flutter.framework/Flutter -o /Users/apple/lsy/gmalpha_flutter/ios/Flutter/Flutter.framework/Flutter
    cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/App.framework /Users/apple/lsy/aar_update/ios${message}/
    rm -rf /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project/App.framework
    rm -rf /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project/Flutter.framework
    rm -rf /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project/gengmei_flutter_plugin.framework
    cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/App.framework /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project
    cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/Flutter.framework /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project
    cp -r /Users/apple/lsy/gmalpha_flutter/build/ios/Release-iphoneos/gengmei_flutter_plugin/gengmei_flutter_plugin.framework /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project
}

function ios_publish(){
    cd /Users/apple/lsy/GMAlpha
    git checkout test
    git stash
    git pull
    rm -rf /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project/App.framework
    cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/App.framework /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project
    git status
    git add .
    git add -A
    git commit -m " change App.framwork"
    git status
    git push

    git checkout dev
    git stash
    git pull
    rm -rf /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project/App.framework
    cp -r /Users/apple/lsy/gmalpha_flutter/ios/Flutter/App.framework /Users/apple/lsy/GMAlpha/GMAlpha/Classes/APP/flutter_project
    git status
    git add .
    git add -A
    git commit -m " change App.framwork"
    git status
    git push
}

function notifyDingDing(){
    python /Users/apple/lsy/update.py
}
#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

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}/

clean
android_apk
#android
#android_publish
#upload_android_aar
#ios
#ios_publish
#notifyDingDing


exit