---
stages:
  - version
  - prepare
  - build
  - deploy

version:
  image: python:3.7-stretch
  stage: version
  script:
    - python3 scripts/update-version.py -k "$SSH_TAGGING_KEY" -o version -v
  artifacts:
    paths:
      - version
  only:
    - branches


prepare:
  stage: prepare
  image: python:3.7-stretch
  script:
    - bash scripts/build.sh --version "$(cat version)"
  artifacts:
    paths:
      - build/**
  only:
    - master
    - beta


.deploy:firefox:
  stage: deploy
  image: node:lts-buster
  cache:
    paths:
      - $CI_PROJECT_DIR/.cache/npm
  before_script:
    - mkdir -p "$CI_PROJECT_DIR/.cache/npm"
  script:
    - npm install --global web-ext --cache "$CI_PROJECT_DIR/.cache/npm" --prefer-offline --no-audit
    - cd build/firefox
    - web-ext -a . sign --channel=$CHANNEL
    - mv atp_safe_links_cleaner* ../../safelinks-cleaner-firefox.xpi
  artifacts:
    paths:
      - safelinks-cleaner-firefox.xpi
  when: manual


deploy:firefox:beta:
  extends: .deploy:firefox
  variables:
    CHANNEL: unlisted
  only:
    - beta


deploy:firefox:master:
  extends: .deploy:firefox
  variables:
    CHANNEL: listed
  only:
    - master


deploy:thunderbird:
  stage: deploy
  image: python:3.7-stretch
  script:
    - apt-get -y update
    - apt-get -y install zip
    - cd build/thunderbird
    - zip -r ../../safelinks-cleaner-thunderbird.xpi *
  artifacts:
    paths:
      - safelinks-cleaner-thunderbird.xpi
  when: manual


pages:
  stage: deploy
  needs:
    - project: $CI_PROKECT_PATH
      job: deploy:thunderbird
    - project: $CI_PROKECT_PATH
      job: deploy:firefox
  script:
    - cp -r site .public
    - sed -i -e "s/%BUILDDATE%/$(date +'%Y-%m-%d %H:%M')/g" .public/index.html
    - cp safelinks-cleaner-thunderbird.xpi .public
    - cp safelinks-cleaner-firefox.xpi .public
    - mv .public public
    - ls -lr public
  artifacts:
    paths:
      - public/
  only:
    - beta
  when: manual
