name: bitcoin-regtest
description: Spawns a regtest Bitcoin daemon

inputs:
  version:
    description: "Version to download and run"
    required: false
    default: "27.0"

runs:
  using: "composite"
  steps:
    - name: Bitcoin Daemon Cache
      id: cache-bitcoind
      uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2
      with:
        path: bitcoin.tar.gz
        key: bitcoind-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}

    - name: Download the Bitcoin Daemon
      if: steps.cache-bitcoind.outputs.cache-hit != 'true'
      shell: bash
      run: |
        RUNNER_OS=linux
        RUNNER_ARCH=x86_64
        FILE=bitcoin-${{ inputs.version }}-$RUNNER_ARCH-$RUNNER_OS-gnu.tar.gz

        wget https://bitcoincore.org/bin/bitcoin-core-${{ inputs.version }}/$FILE
        mv $FILE bitcoin.tar.gz

    - name: Extract the Bitcoin Daemon
      shell: bash
      run: |
        tar xzvf bitcoin.tar.gz
        cd bitcoin-${{ inputs.version }}
        sudo mv bin/* /bin && sudo mv lib/* /lib

    - name: Bitcoin Regtest Daemon
      shell: bash
      run: PATH=$PATH:/usr/bin ./orchestration/dev/networks/bitcoin/run.sh -daemon