
export buildArg=$1


./build_version.sh

. build_version.properties
. build_id.properties

echo "buildArg:$1"
# buildId="21090778"
ext=""
branch=`git branch --show-current`
if [[ $branch == *main* || $branch == *master* || "$buildArg" == "prod" ]]
then
    ext=""
    echo "ext: prod"
elif [[ $branch == *release* ]]
then
    ext=".rc"
    echo "ext: rc"
else
    ext=".dev"
    echo "ext: dev"
fi

buildName="$androidVersionName-$buildId$ext"


echo "start build android app..."
echo "build name id $buildName"
echo "build id is $buildId"
echo "track is $track"

# git tag -a "Android-v$buildName" -m "Build Android Prod v$buildName"
# oldPath="$PATH"
# currentFlutterPath=`which flutter`
# flutterBin=`dirname $currentFlutterPath`
# flutterDir=`dirname $flutterBin`
# flutterWorkDir=`dirname $flutterDir`
# flutterPath="$flutterWorkDir/flutter-2.10.5/bin"
# PATH="$flutterPath:$PATH"
currentProjectPath=`pwd`
echo $PATH
flutter clean
flutter pub get
# export http_proxy=http://127.0.0.1:4780;
# export https_proxy=http://127.0.0.1:4780;
# flutter build apk --build-name $buildName --build-number $buildId
flutter build appbundle --build-name $buildName --build-number $buildId
# export http_proxy=;export https_proxy=;
./extract_apk_and_upload.sh

result=$?
echo "build $buildArg result:$result path:$currentProjectPath"
# PATH="$oldPath"

if [ $result == 0 ]
then
    echo "build success!"
    # if [ "$buildArg" == "prod" ]
    # then
    #     tagName="Android-v$buildName"
    #     echo "EXEC=> git tag $tagName -m \"Build Android Prod v$buildName\""
    #     git tag -a $tagName -m "Build Android Prod v$buildName"
    #     if [ $? == 0 ]
    #     then
    #         git push origin $tagName
    #         # ./send_wechat_msg.sh M2Blocks Android $androidVersionName $buildId
    #     fi
    # fi
else
    echo "build failed"
fi

# open build/app/outputs/bundle/release/

# cd android
# #fastlane play_store_internal
# #
# if [ "$track" == "beta" ]
# then
#   echo "fastlane track=$track"
#   fastlane play_store_beta
# else
#  echo "fastlane track=$track"
#  fastlane play_store_internal
# fi


# fastlane supply --aab ../build/app/outputs/bundle/release/app-release.aab --track $track --skip_upload_apk $skip_upload_apk --skip_upload_changelogs $skip_upload_changelogs --skip_upload_images $skip_upload_images --skip_upload_screenshots $skip_upload_screenshots
