name: TVBox_test_T

on:
 
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - userName: zxyadc
            repoName: Box
            branchName: main
            appName: TBox
            osName: bb
   
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Check New Commit
        run: |
          upStream=https://github.com/${{ matrix.userName }}/${{ matrix.repoName }}
          echo "upStream=$upStream" >> $GITHUB_ENV
          commit=$(curl -sL $upStream/commits/${{ matrix.branchName }} |grep -o "/${{ matrix.userName }}/${{ matrix.repoName }}/commit/[a-z0-9]\+" |head -1 | cut -d\/ -f5)
          if ! grep -q "$commit" README.md || [ "${{ inputs.rebuild }}" == "true" ]; then
            echo "commit=$commit" >> $GITHUB_ENV
            echo "commitS=${commit:0:7}" >> $GITHUB_ENV
          fi
          
      - name: Checkout Source Code
        if: ${{ env.commit }}
        run: |
          git clone ${{ env.upStream }} ${{ matrix.repoName }}
          cd ${{ matrix.repoName }}
          git checkout ${{ env.commit }}
          echo "tag=$(git log --date=format:'%Y%m%d-%H%M' --pretty=format:%cd ${{ env.commitS }} -1)" >> $GITHUB_ENV
      
      - name: DIY PY
        if: ${{ env.commit }}
        working-directory: ${{ matrix.repoName }}
        run: |
          chmod +x ${{ github.workspace }}/DIY/py.sh
          bash ${{ github.workspace }}/DIY/py.sh
          
      - name: DIY
        if: ${{ env.commit }}
        working-directory: ${{ matrix.repoName }}
        run: |
          cp -rf $GITHUB_WORKSPACE/DIY/png/图标2.png app/src/main/res/drawable/app_icon.png
          cp -rf $GITHUB_WORKSPACE/DIY/png/等待图标.png app/src/main/res/drawable/icon_loading.png
          cp -rf $GITHUB_WORKSPACE/DIY/qbox/epg_data.json app/src/main/assets/epg_data.json
          sed -i 's/MM月dd日/yyyy年 MM月 dd日/g' app/src/main/res/values-zh/strings.xml
          sed -i 's%TVBox%TV盒子%g' app/src/main/res/values-zh/strings.xml
          sed -i 's%com.github.tvbox.osc.tk%com.github.tvbox.osc.${{ matrix.osName }}%g' app/build.gradle
          sed -i 's%HOME_REC, 2%HOME_REC, 1%g' app/src/main/java/com/github/tvbox/osc/base/App.java  
          sed -i 's%HomeActivity.getRes().getString(R.string.app_source));% "https://agit.ai/wwz09/ubuntu/raw/branch/master/main.json");%g' app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java
           
      - name: Extra Modify
        if: ${{ env.commit }}
        working-directory: ${{ matrix.repoName }}
        run: |
          echo '修改VersionName'
          sed -i "/versionName/s#[0-9a-zA-Z_\.\'\"-]\+\$#\'${{ env.tag }}\'#" app/build.gradle
          sed -i "/android:text=/s#=\"#=\"${{ env.tag }}\\\\n\\\\n#" app/src/main/res/layout/dialog_about.xml
          echo 'crosswalk源，防挂'
          if grep -q 'crosswalk' build.gradle; then
            sed -i "/crosswalk/a\        maven { url 'https://o0halflife0o.github.io/crosswalk/releases/crosswalk/android/maven2' }" build.gradle
          else
            sed -i "/jitpack.io/a\        maven { url 'https://o0halflife0o.github.io/crosswalk/releases/crosswalk/android/maven2' }" build.gradle
          fi
      - name: Option For Test
        if: ${{ inputs.testOption }}
        working-directory: ${{ matrix.repoName }}
        run: |
          #添加arm64支持，不支持ijk解码
          sed -i "/armeabi-v7a[\'\"]$/s#\$#, 'arm64-v8a'#" app/build.gradle
          sed -i "/armeabi-v7a[\'\"]$/s#\$#, 'arm64-v8a'#" player/build.gradle
      - name: Release Apk Sign
        if: ${{ env.commit }}
        working-directory: ${{ matrix.repoName }}
        run: |
          signingConfigs='ICAgIHNpZ25pbmdDb25maWdzIHtcCiAgICAgICAgaWYgKHByb2plY3QuaGFzUHJvcGVydHkoIlJFTEVBU0VfU1RPUkVfRklMRSIpKSB7XAogICAgICAgICAgICBteUNvbmZpZyB7XAogICAgICAgICAgICAgICAgc3RvcmVGaWxlIGZpbGUoUkVMRUFTRV9TVE9SRV9GSUxFKVwKICAgICAgICAgICAgICAgIHN0b3JlUGFzc3dvcmQgUkVMRUFTRV9TVE9SRV9QQVNTV09SRFwKICAgICAgICAgICAgICAgIGtleUFsaWFzIFJFTEVBU0VfS0VZX0FMSUFTXAogICAgICAgICAgICAgICAga2V5UGFzc3dvcmQgUkVMRUFTRV9LRVlfUEFTU1dPUkRcCiAgICAgICAgICAgICAgICB2MVNpZ25pbmdFbmFibGVkIHRydWVcCiAgICAgICAgICAgICAgICB2MlNpZ25pbmdFbmFibGVkIHRydWVcCiAgICAgICAgICAgICAgICBlbmFibGVWM1NpZ25pbmcgPSB0cnVlXAogICAgICAgICAgICAgICAgZW5hYmxlVjRTaWduaW5nID0gdHJ1ZVwKICAgICAgICAgICAgfVwKICAgICAgICB9XAogICAgfVwKXA=='
          signingConfig='ICAgICAgICAgICAgaWYgKHByb2plY3QuaGFzUHJvcGVydHkoIlJFTEVBU0VfU1RPUkVfRklMRSIpKSB7XAogICAgICAgICAgICAgICAgc2lnbmluZ0NvbmZpZyBzaWduaW5nQ29uZmlncy5teUNvbmZpZ1wKICAgICAgICAgICAgfVwK'
          signingConfigs="$(echo "$signingConfigs" |base64 -d )"
          signingConfig="$(echo "$signingConfig" |base64 -d )"
          sed -i -e "/defaultConfig {/i\\$signingConfigs " -e "/debug {/a\\$signingConfig " -e "/release {/a\\$signingConfig " app/build.gradle
          cp -f ${{ github.workspace }}/.github/workflows/TVBoxOSC.jks app/TVBoxOSC.jks
          sed -i '$a\RELEASE_STORE_FILE=./TVBoxOSC.jks'     ./gradle.properties
          sed -i '$a\RELEASE_KEY_ALIAS=TVBoxOSC'            ./gradle.properties
          sed -i '$a\RELEASE_STORE_PASSWORD=TVBoxOSC'       ./gradle.properties
          sed -i '$a\RELEASE_KEY_PASSWORD=TVBoxOSC'         ./gradle.properties
          
      - name: Setup Gradle
        if: ${{ env.commit }}
        uses: gradle/gradle-build-action@v2
        
      - name: Build With Gradle
        if: ${{ env.commit }}
        working-directory: ${{ matrix.repoName }}
        run: |
          chmod +x gradlew
          ./gradlew assemblerelease --build-cache --parallel --daemon --warning-mode all
      
      - name: Prepare App
        if: ${{ env.commit }}
        working-directory: ${{ matrix.repoName }}
        run: |
          rm -rf apk/
          mkdir -p apk/
          for file in `find ~ -name "*.apk" -print`; do
           mv "$file" apk/${{ matrix.userName }}_${{ matrix.appName }}_${{ env.tag }}.apk
          done
      
      - name: Upload App To Artifact
        uses: actions/upload-artifact@v3
        if: ${{ env.commit }}
        with:
          name: ${{ matrix.appName }}
          path: ${{ matrix.repoName }}/apk/*
       
      - name: Whether Or Not to Publish
        if: ${{ inputs.donotpublish && env.commit }}
        run: |
          echo "commit=" >> $GITHUB_ENV
          
      - name: Release and Upload Assets
        uses: ncipollo/release-action@v1
        if: ${{ env.commit }}
        with:
          name: ${{ env.tag }}
          tag: ${{ env.tag }}
          allowUpdates: true
          removeArtifacts: true
          artifacts: |
            ${{ matrix.repoName }}/apk/*.apk
            
      
      
