#  MIT License
#
#  Copyright (c) 2022-2023 Luke Parker
#  Copyright (c) Cuprate developers
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in all
#  copies or substantial portions of the Software.
#
#  Initially taken from Serai Dex: https://github.com/serai-dex/serai/blob/b823413c9b7ae6747b9af99e18379cfc49f4271a/.github/actions/monero/action.yml.

name: monerod-download
description: Downloads the core Monero daemon

inputs:
  version:
    description: "Version to download"
    required: false
    default: v0.18.3.3

runs:
  using: "composite"
  steps:
    - name: Monero Daemon Cache
      id: cache-monerod
      uses: actions/cache@v3
      with:
        path: |
          monerod
          monerod.exe
        key: monerod-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}

    - name: Download the Monero Daemon
      if: steps.cache-monerod.outputs.cache-hit != 'true'
      shell: bash
      run: |
        OS=${{ runner.os }}
        ARCH=${{ runner.arch }}

        case "$OS $ARCH" in
          "Windows X64") FILE=monero-win-x64-${{ inputs.version }}.zip ;;
          "Windows X86") FILE=monero-win-x86-${{ inputs.version }}.zip ;;
          "Linux X64") FILE=monero-linux-x64-${{ inputs.version }}.tar.bz2 ;;
          "Linux X86") FILE=monero-linux-x86-${{ inputs.version }}.tar.bz2 ;;
          "macOS X64") FILE=monero-mac-x64-${{ inputs.version }}.tar.bz2 ;;
          "macOS ARM64") FILE=monero-mac-armv8-${{ inputs.version }}.tar.bz2 ;;
          *) exit 1 ;;
        esac
        curl -O -L https://downloads.getmonero.org/cli/$FILE
        if [[ ${{ runner.os }} == Windows ]]; then
          unzip $FILE
          mv */monerod.exe monerod.exe
        else
          tar -xvf $FILE
          mv */monerod monerod
        fi