name: nextion2text

on:
  pull_request:
    paths:
      - HMI/nspanel.HMI
      - HMI/US/portrait/nspanel_US_P.HMI
      - HMI/US/portrait/diff-ignore/cardEntities.txt
      - HMI/US/landscape/nspanel_US_L.HMI
      - .github/workflows/nextion2text.yml
  push:
    branches:
      - main
    paths:
      - HMI/nspanel.HMI
      - HMI/US/portrait/nspanel_US_P.HMI
      - HMI/US/portrait/diff-ignore/cardEntities.txt
      - HMI/US/landscape/nspanel_US_L.HMI
      - .github/workflows/nextion2text.yml

jobs:
  gen-nextion-to-text:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ github.head_ref }}

    - name: Get last commit message
      id: last-commit-message
      run: |
        echo "::set-output name=msg::$(git log -1 --pretty=%s)"

    - name: Download n2t
      continue-on-error: true
      run: |
        wget https://raw.githubusercontent.com/joBr99/Nextion2Text/linux/Nextion2Text.py
  
    - name: Run n2t EU
      continue-on-error: true
      run: |
        python Nextion2Text.py -s -d -p -p visual -i HMI/nspanel.HMI -o HMI/n2t-out-visual
   
    - name: Run n2t US
      continue-on-error: true
      run: |
        python Nextion2Text.py -s -d -p -p visual -i HMI/US/portrait/nspanel_US_P.HMI -o HMI/US/portrait/n2t-out-visual
        python Nextion2Text.py -s -d -p -p visual -i HMI/US/landscape/nspanel_US_L.HMI -o HMI/US/landscape/n2t-out-visual
    
    - name: Run n2t US P DIFF
      continue-on-error: true
      run: |
        echo "YXR0cmlidXRlcyA9IHsKICAgICJpZCI6IHsKICAgICAgICAiaWdub3JlIjogVHJ1ZSwKICAgICAgICAibmFtZSI6ICJJRCIsCiAgICAgICAgInN0cnVjdCI6ICJpIiwKICAgIH0KfQ==" | base64 -d > ignore-id.py
        python Nextion2Text.py -c ignore-id.py -p font -d -i HMI/nspanel.HMI -o HMI/n2t-out
        
        echo portrait
        python Nextion2Text.py -c ignore-id.py -p font -d -i HMI/US/portrait/nspanel_US_P.HMI -o HMI/US/portrait/n2t-out
        diff -bur HMI/n2t-out HMI/US/portrait/n2t-out > HMI/US/portrait/diff-eu-version.txt

    - name: Run n2t US P DIFF DIFF
      continue-on-error: true
      run: |
        diff -u HMI/US/portrait/diff-eu-version-known-good.txt HMI/US/portrait/diff-eu-version.txt | grep -E "^\+" > HMI/US/portrait/diff-filtered.txt

    - name: Run n2t US L DIFF
      continue-on-error: true
      run: |      
        echo landscape
        python Nextion2Text.py -c ignore-id.py -p font -d -i HMI/US/landscape/nspanel_US_L.HMI -o HMI/US/landscape/n2t-out
        diff -bur HMI/n2t-out HMI/US/landscape/n2t-out > HMI/US/landscape/diff-eu-version.txt

        
    - name: Display txt files 
      continue-on-error: true
      run: |      
        find -name "**.txt"
        
    - uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: ${{ steps.last-commit-message.outputs.msg }} (add nextion2text)
        file_pattern: "**.txt"
