name: CI

on:
  push:
    branches:
      - 'byc'
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always
  
jobs:
  build:
    strategy:
      matrix:
        os: [windows-latest,ubuntu-latest] 
    
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-tags: true
    
    - name: Define Binary Name(Windows)
      if: matrix.os == 'windows-latest'
      run: echo "BINARY_NAME=rxqlited" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
    - name: Define Binary Name(Ubuntu)  
      if: matrix.os == 'ubuntu-latest'
      run: echo "BINARY_NAME=rxqlited" >> $GITHUB_ENV
      
    - run: git fetch --no-tags --prune --depth=1 origin +refs/tags/*:refs/tags/*
    - name: Get latest tag (Windows)
      if: matrix.os == 'windows-latest'
      id: get_tag_windows
      run: |
        $tag = git describe --tags $(git rev-list --tags --max-count=1)
        echo "TAG=$tag" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
      shell: pwsh
    - name: Latest tag (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: git describe --tags `git rev-list --tags --max-count=1`
      
    - name: Get latest tag (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      id: get_tag_ubuntu
      run: echo "TAG=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
    - name: Print latest tag
      run: echo "Latest tag is ${{ env.TAG }}"
    
    
    
    
      
    - name: Install dependencies (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: sudo apt-get update && sudo apt-get install -y build-essential
    
    - name: Install MSVC(Windows)
      if: matrix.os == 'windows-latest'
      run: choco install visualstudio2022buildtools

    - name: Install Windows SDK(Windows)
      if: matrix.os == 'windows-latest'
      run: choco install windows-sdk-10.1 -y
      
    - name: Set INCLUDE environment variable(Windows)
      if: matrix.os == 'windows-latest'
      run: echo "INCLUDE=C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt;$INCLUDE" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
  
    - name: Install Clang(Windows)
      if: matrix.os == 'windows-latest'
      run: choco install llvm
    
    - name: Add libclang to PATH(Windows)
      if: matrix.os == 'windows-latest'
      run: echo "C:/Program Files/LLVM/bin" >> $GITHUB_PATH
    
    - name: Install Clang(Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: sudo apt-get install -y clang
      
    
      
    - name: Set target triple (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: echo "TARGET_TRIPLE=x86_64-unknown-linux-gnu" >> $GITHUB_ENV
    - name: Set target triple (Windows)
      if: matrix.os == 'windows-latest'
      run: echo "TARGET_TRIPLE=x86_64-pc-windows-msvc" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
    
    - name: Update local toolchain
      run: |
        rustup update
          
    - name: Display Rust Version
      run:  |
        cargo -vV
        rustc -vV
    
    
        
    - name: Build(Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: cargo build --release --target ${{ env.TARGET_TRIPLE }}
      env:
        CARGO_TERM_COLOR: always
        TMPDIR: ${{ runner.temp }}
        RXQLITED_DIR: ${{ github.workspace }}/target/${{ env.TARGET_TRIPLE }}/release
        
    - name: Build(Windows)
      if: matrix.os == 'windows-latest'
      run: cargo build --release --target ${{ env.TARGET_TRIPLE }}
      env:
        CARGO_TERM_COLOR: always
        TEMP: ${{ runner.temp }}
        RXQLITED_DIR: ${{ github.workspace }}/target/${{ env.TARGET_TRIPLE }}/release
    
    
    - name: Test(Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: cargo test --release --target ${{ env.TARGET_TRIPLE }} -- --nocapture
      env:
        CARGO_TERM_COLOR: always
        TMPDIR: ${{ runner.temp }}
        RXQLITED_DIR: ${{ github.workspace }}/target/${{ env.TARGET_TRIPLE }}/release
    - name: Test(Windows)
      if: matrix.os == 'windows-latest'
      run: cargo test --release --target ${{ env.TARGET_TRIPLE }} -- --nocapture
      env:
        CARGO_TERM_COLOR: always
        TEMP: ${{ runner.temp }}
        RXQLITED_DIR: ${{ github.workspace }}/target/${{ env.TARGET_TRIPLE }}/release
    
    - name: Strip(Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: strip -s target/${{ env.TARGET_TRIPLE }}/release/${{ env.BINARY_NAME }}
    
    - name: Install zip (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: sudo apt-get install zip
    - name: Install 7zip (Windows)
      if: matrix.os == 'windows-latest'
      run: choco install 7zip      
  
    
    - name: Name The Artifcat Archive(Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: echo "ARTIFACT_ARCHIVE=./${{ env.BINARY_NAME }}-ubuntu.zip" >> $GITHUB_ENV
      
    - name: Name The Artifcat Archive(Windows)
      if: matrix.os == 'windows-latest'
      run: echo "ARTIFACT_ARCHIVE=./${{ env.BINARY_NAME }}-win32.zip" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
    
    - name: Zip the executable(Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: cd ./target/${{ env.TARGET_TRIPLE }}/release && zip ./../../../${{ env.ARTIFACT_ARCHIVE }} ${{ env.BINARY_NAME }} && cd -
      
    
      
    - name: Zip the executable(Windows)
      if: matrix.os == 'windows-latest'
      run: 7z a ${{ env.ARTIFACT_ARCHIVE }} ./target/${{ env.TARGET_TRIPLE }}/release/${{ env.BINARY_NAME }}.exe
      
    - name: Upload artifact
      uses: actions/upload-artifact@v3
      with:
        name: ${{ runner.os }}-artifact
        path: ${{ env.ARTIFACT_ARCHIVE }}
      
      
  deploy:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-tags: true 

    - name: Define Binary Name(Windows)
      if: runner.os == 'Windows'
      run: echo "BINARY_NAME=rxqlited" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
    - name: Define Binary Name(Ubuntu)  
      if: runner.os == 'Linux'
      run: echo "BINARY_NAME=rxqlited" >> $GITHUB_ENV

    - run: git fetch --no-tags --prune --depth=1 origin +refs/tags/*:refs/tags/*
      
    - name: Get latest tag
      id: get_tag_ubuntu
      run: echo "TAG=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
    - name: Print latest tag
      run: echo "Latest tag is ${{ env.TAG }}"
      
    - name: Download Windows Artifact
      uses: actions/download-artifact@v3
      with:
        path: "."
        name: Windows-artifact
    
    - name: Download Ubuntu Artifact
      uses: actions/download-artifact@v3
      with:
        path: "."
        name: Linux-artifact
    
    - name: List Files
      run: ls -R  

    - name: Delete release if exists
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        tag: ${{ github.ref_name }}
      run: |
        gh release delete ${{ env.TAG }} -y
      continue-on-error: true
        
    - name: Create release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        tag: ${{ github.ref_name }}
      run: |
        gh release create ${{ env.TAG }} ./${{ env.BINARY_NAME }}-win32.zip ./${{ env.BINARY_NAME }}-ubuntu.zip
