UpdateDependentLibraries:
  rules:
    - if: $CI_PIPELINE_SOURCE == "trigger" && ($CI_COMMIT_REF_NAME == "develop" || $CI_COMMIT_REF_NAME == "feature/autoupdate")
      when: on_success
      allow_failure: false
  tags:
    - macos
    - flutter
  stage: build
  script:
    - |
      echo "♻️ Update library for nut_player"
      echo $LIBRARY_TYPE
      echo $LIBRARY_PATH
      if [ -z "$LIBRARY_PATH" ]; then
        echo "⛔️ Path not passed."
        exit 1
      fi
      
      echo "💈 Download library"
      if [ "$LIBRARY_TYPE" == "ios" ]; then
      
        wget --header "PRIVATE-TOKEN: $K8S_TOKEN_LIBRARY_IOS" "$LIBRARY_PATH" -O artifact.zip
        
        echo "📦 Unzip library"
        unzip artifact.zip
        
        echo "📲 Copy library"
        cp -R ios/Result/NutPlayer/NutPlayer.xcframework nut_player_ios/ios/Vendors
        
        echo "🗑️ Remove temporary"
        rm artifact.zip
        rm -rf ios
        
        echo "⚙️ Add to Git new library"
        git add -A && git commit -m "Update iOS library [CI/CD]"
        
        git status
        
        project_url=$(echo $CI_PROJECT_URL | sed 's/https:\/\///')
        git remote set-url origin https://oauth2:$K8S_TOKEN_UPLOAD_LIBRARY@$project_url
        
        git push origin HEAD:$CI_COMMIT_REF_NAME
        
      fi
        
      if [ "$LIBRARY_TYPE" == "android" ]; then
      
        wget --header "PRIVATE-TOKEN: $K8S_TOKEN_LIBRARY_ANDROID" "$LIBRARY_PATH" -O artifact.zip
        
        echo "📦 Unzip library"
        unzip artifact.zip
        
        echo "🤖 Copy library"
        cp -R android/artifacts/*.aar nut_player_android/android/libs
        
        echo "🗑️ Remove temporary"
        rm artifact.zip
        rm -rf android
        
        echo "⚙️ Add to Git new library"
        git add -A && git commit -m "Update Android library [CI/CD]"
        
        git status
        
        project_url=$(echo $CI_PROJECT_URL | sed 's/https:\/\///')
        git remote set-url origin https://oauth2:$K8S_TOKEN_UPLOAD_LIBRARY@$project_url
      
        git push origin HEAD:$CI_COMMIT_REF_NAME
      
      fi
      