name: Installation script
on: 
  pull_request:
    paths:
      - .ci/install.sh
  push:
    branches:
      - master
    paths:
      - .ci/install.sh

jobs:
  test_against_latest_release_arm:
    strategy:
      matrix:
        docker_image:
          - arm64v8/ubuntu
          - arm32v7/ubuntu
    name: Test against latest release (ARM)
    runs-on: ubuntu-latest
    steps:
      - name: Set up QEMU
        id: qemu
        uses: docker/setup-qemu-action@v1
      - uses: actions/checkout@v2
      - name: Run installation script in Docker
        run: |
          docker run --rm -v $(pwd):$(pwd) -e "RUST_LOG=fnm=debug" --workdir $(pwd) ${{matrix.docker_image}} bash -c '
            set -e

            apt update && apt install -y unzip curl libatomic1

            echo "-------------------------------------"
            echo "Installing for CPU arch: $(uname -m)"

            bash ./.ci/install.sh

            echo "fnm --version"
            ~/.fnm/fnm --version

            echo "eval fnm env"
            eval "$(~/.fnm/fnm env)"

            echo "fnm install"
            ~/.fnm/fnm install 12

            echo "node -v"
            ~/.fnm/fnm exec --using=12 -- node -v
          '

  test_against_latest_release:
    name: Test against latest release
    strategy:
      matrix:
        shell: [fish, zsh, bash]
        setup: 
          - os: ubuntu
            script_arguments: ''
          - os: macos
            script_arguments: ''
          - os: macos
            script_arguments: '--force-no-brew'
    runs-on: ${{ matrix.setup.os }}-latest
    steps:
      - uses: actions/checkout@v2
      - run: "sudo apt-get install -y ${{ matrix.shell }}"
        name: Install ${{matrix.shell}} using apt-get
        if: matrix.setup.os == 'ubuntu'
      - run: "brew update && brew install ${{ matrix.shell }}"
        name: Update formulae and install ${{matrix.shell}} using Homebrew
        if: matrix.setup.os == 'macos'
      - run: |
          if [ -f ~/.bashrc ]; then
            cp ~/.bashrc ~/.bashrc.bak
            echo 'echo hello world' > ~/.bashrc
            echo '. ~/.bashrc.bak' >> ~/.bashrc
          fi

          if [ -f ~/.zshrc ]; then
            echo 'echo hello world' > ~/.zshrc
            echo '. ~/.zshrc.bak' >> ~/.zshrc
          fi
        name: reset shell profiles
      - run: "env SHELL=$(which ${{ matrix.shell }}) bash ./.ci/install.sh ${{ matrix.setup.script_arguments }}"
        name: Run the installation script
      - run: ./.ci/test_installation_script.sh ${{ matrix.shell }}
        name: 'Test installation script'
