name: Download Toolchain

inputs:
  toolchain:
    description: 'Toolchain name'
    required: true
  toolchain_url:
    description: 'Toolchain URL'
    required: true

runs:
  using: "composite"
  steps:
    - name: Cache Toolchain
      if: ${{ !startsWith(inputs.toolchain_url, 'https://github.com') }}
      uses: actions/cache@v4
      id: cache-toolchain-download
      with:
        path: ~/cache/${{ inputs.toolchain }}
        key: ${{ runner.os }}-${{ inputs.toolchain }}-${{ inputs.toolchain_url }}

    - name: Install Toolchain
      if: steps.cache-toolchain-download.outputs.cache-hit != 'true'
      run: |
        mkdir -p ~/cache/${{ inputs.toolchain }}

        if [[ ${{ inputs.toolchain }} == rx-gcc ]]; then
          wget --progress=dot:giga ${{ inputs.toolchain_url }} -O toolchain.run
          chmod +x toolchain.run
          ./toolchain.run -p ~/cache/${{ inputs.toolchain }}/gnurx -y
        elif [[ ${{ inputs.toolchain }} == arm-iar ]]; then
          wget --progress=dot:giga ${{ inputs.toolchain_url }} -O ~/cache/${{ inputs.toolchain }}/cxarm.deb
        else
          wget --progress=dot:giga ${{ inputs.toolchain_url }} -O toolchain.tar.gz
          tar -C ~/cache/${{ inputs.toolchain }} -xaf toolchain.tar.gz
        fi
      shell: bash

    - name: Setup Toolchain
      run: |
        if [[ ${{ inputs.toolchain }} == arm-iar ]]; then
          sudo apt-get install -y ~/cache/${{ inputs.toolchain }}/cxarm.deb
          echo >> $GITHUB_PATH "/opt/iar/cxarm/arm/bin"
        else
          echo >> $GITHUB_PATH `echo ~/cache/${{ inputs.toolchain }}/*/bin`
        fi
      shell: bash
