before_script:
  - echo "executing before script"
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

  ##
  ## Run ssh-agent (inside the build environment)
  ##
  - eval $(ssh-agent -s)

  ##
  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  ## We're using tr to fix line endings which makes ed25519 keys work
  ## without extra base64 encoding.
  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
  ##
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null

  ##
  ## Create the SSH directory and give it the right permissions
  ##
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

  - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts

  - echo "$SSH_HOST" > ~/.ssh/config
  - chmod 644 ~/.ssh/config
  - git submodule sync --recursive
  - git submodule update --init


  ## Move pymap from the docker image to the tools folder
  - mkdir -p ../tools
  - rm -rf ../tools/v_tools
  - cp -r /sots-tools/v_tools ../tools
  - rm -f base/bpre0.gba
  - cp /bpre0.gba ./base/bpre0.gba
  - chmod 644 ./base/bpre0.gba

stages:
  - build

build-sots:
  stage: build
  only:
    - tags
    - web
    - triggers

  script:
    - echo "Building Pokémon SotS"
    - make all

  cache:
    key: ${CI_COMMIT_REF_SLUG}
