name: Android 13 Build
on:
  workflow_dispatch:
    inputs:
      arch:
        type: choice
        description: Architecture of WSA.
        required: true
        options: [x64, arm64]
        default: x64
      release-type:
        type: choice
        description: Release type of WSA. RP means Release Preview, WIS means Insider Slow, WIF means Insider Fas
        required: true
        options: ["retail","RP","WIS","WIF"]
        default: "WIF"
      magisk-ver:
        type: choice
        description: Magisk version.
        required: true
        options: ["delta"]
        default: "delta"
      gapps-brand:
        type: choice
        description: GApps brand. none for no integration of GApps
        required: true
        options: ["OpenGApps","MindTheGapps","none"]
        default: "OpenGApps"
      gapps-variant:
        type: choice
        description: GApps variant.
        required: true
        options: ["super","stock","full","mini","micro","nano","pico","tvstock","tvmini"]
        default: "pico"
      root-sol:
        type: choice
        description: Root solution. none means no root.
        required: true
        options: ["magisk","none"]
        default: "magisk"
#       remove-amazon:
#         description: Remove Amazon Appstore from the system
#         required: true
#         type: boolean 
#         default: true
      compress-format:
        type: choice
        description: Compress format.
        required: true
        options: ["zip","7z","xz"]
        default: "zip"

jobs:
  custbuild:
    name: Build WSA
    runs-on: ubuntu-latest

    steps:
      - name: Checkout ♻️
        uses: actions/checkout@v3

      - name: Install Dependencies 🧑‍🏭
        run: |
          sudo dpkg --add-architecture i386 
          sudo mkdir -pm755 /etc/apt/keyrings
          sudo wget -O /etc/apt/keyrings/winehq-archive.key https://dl.winehq.org/wine-builds/winehq.key
          sudo wget -NP /etc/apt/sources.list.d/ https://dl.winehq.org/wine-builds/ubuntu/dists/jammy/winehq-jammy.sources
          sudo apt-get update -y
          sudo apt-get install --install-recommends winehq-staging
          sudo apt-get install -y whiptail setools lzip patchelf e2fsprogs python3-pip aria2 p7zip-full attr xz-utils unzip cabextract
          sudo wget -P /usr/local/bin/ https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks
          sudo chmod +x /usr/local/bin/winetricks
          wget -P /home/runner/.cache/winetricks/msxml6/ https://raw.githubusercontent.com/${GITHUB_REPOSITORY}/${GITHUB_REF_NAME}/wine/.cache/winetricks/msxml6/msxml6-KB973686-enu-amd64.exe
          mkdir -p download >/dev/null 2>&1
          wget -P ./download/ https://download.microsoft.com/download/4/7/c/47c6134b-d61f-4024-83bd-b9c9ea951c25/appx/Microsoft.VCLibs.x64.14.00.Desktop.appx
          wget -P ./download/ https://cdn.glitch.global/5649a8d3-cad1-41a6-a507-798f8c4f8bb2/Microsoft.UI.Xaml_x64.appx
          pip list --disable-pip-version-check | grep -E "^requests " >/dev/null 2>&1 || python3 -m pip install requests
          winetricks list-installed | grep -E "^msxml6" >/dev/null 2>&1 || winetricks msxml6 || abort
      - name: Retrieve the user_code for WSA Preview Program, if exists
        env:
          USER_CODE: ${{ secrets.USER_CODE }}
        if: env.USER_CODE != null
        run: echo "user_code=$USER_CODE" > ./download/.ms_account

      - name: Manually download the latest build from WSA Preview Program
        env:
          USER_CODE: ${{ secrets.USER_CODE }}
        if: env.USER_CODE == null
        run: |
          wget -P ./download/ https://github.com/Howard20181/WSA-bundle/releases/download/v2211.40000.10.0/MicrosoftCorporationII.WindowsSubsystemForAndroid_2211.40000.10.0_neutral___8wekyb3d8bbwe.7z
          cd ./download/ 
          7z x MicrosoftCorporationII.WindowsSubsystemForAndroid_2211.40000.10.0_neutral___8wekyb3d8bbwe.7z
          mv MicrosoftCorporationII.WindowsSubsystemForAndroid_2211.40000.10.0_neutral___8wekyb3d8bbwe.Msixbundle wsa-WIF.zip
          rm MicrosoftCorporationII.WindowsSubsystemForAndroid_2211.40000.10.0_neutral___8wekyb3d8bbwe.7z
      - name: Grant exec permission to script 👑
        run: chmod +x ./scripts/build.sh

      - name: Build WSA 🏗️
        id: wsa
        run: ./scripts/build.sh --arch ${{ inputs.arch }} --release-type ${{ inputs.release-type }} --magisk-ver ${{ inputs.magisk-ver }} --gapps-brand ${{ inputs.gapps-brand }} --gapps-variant ${{ inputs.gapps-variant }} --root-sol ${{ inputs.root-sol }} --remove-amazon --skip-download-wsa --compress-format ${{ inputs.compress-format }}

      - name: Upload Artifact 🤌
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.wsa.outputs.artifact }}
          path: ./output/${{ steps.wsa.outputs.artifact }}.${{ inputs.compress-format }}
