stages:
  - build
  - pages
  - tarball
  - release

variables:
  TARBALL_PATH_PREFIX: ${CI_PROJECT_NAME}-${CI_COMMIT_TAG}
  TARBALL_NAME: ${TARBALL_PATH_PREFIX}.tar.xz

include:
  - component: gitlab.gnome.org/GNOME/citemplates/release-service@master
    inputs:
      job-stage: release
      dist-job-name: "tarball"
      tarball-artifact-path: "${TARBALL_NAME}"

fedora:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v1
  stage: build
  script:
    - export BASE_BRANCH=master
    - export SOURCE=$PWD
    - export PREFIX=$HOME/_prefix
    - export DEPS=$PWD/_deps
    - mkdir $PREFIX
    - mkdir $DEPS
    - pushd po/ >/dev/null && export TXT=`intltool-update -m 2>&1` && popd >/dev/null
    - if [ ! -z "$TXT" ]; then echo -e "\\033[1;31m" 1>&2; echo "$TXT" 1>&2; echo -e "\\033[0m" 1>&2; exit 1; fi
    - export HELP_ANY_MISSING=0 &&
      pushd help/C >/dev/null &&
      for fl in `ls *.page`; do
        if [ `cat ../CMakeLists.txt | grep -c $fl` = "0" ]; then
          echo -e "\\033[1;31mHelp page '$fl' not part of the help/CMakeLists.txt\\033[0m" 1>&2;
          export HELP_ANY_MISSING=1;
        fi;
      done &&
      popd >/dev/null &&
      if [ "$HELP_ANY_MISSING" != "0" ]; then exit 1; fi
    - export LD_LIBRARY_PATH="$PREFIX/lib:$PREFIX/lib64"
    - export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/lib64/pkgconfig:$PREFIX/share/pkgconfig"
    - export GSETTINGS_SCHEMA_DIR="$PREFIX/share/glib-2.0/schemas"
    - cd $DEPS
    - git clone --depth=1 --branch $BASE_BRANCH https://gitlab.gnome.org/GNOME/evolution-data-server.git
    - cd $DEPS/evolution-data-server
    - mkdir _build && cd _build
    - cmake -G "Ninja"
            -DCMAKE_BUILD_TYPE=Release
            -DCMAKE_INSTALL_PREFIX=$PREFIX
            -DENABLE_GOA=ON
            -DENABLE_EXAMPLES=OFF
            -DENABLE_TESTS=OFF
            -DENABLE_INTROSPECTION=OFF
            -DENABLE_VALA_BINDINGS=OFF
            -DENABLE_INSTALLED_TESTS=OFF
            -DENABLE_GTK_DOC=OFF
            -DWITH_PRIVATE_DOCS=OFF
            -DWITH_LIBDB=OFF
            -DWITH_PHONENUMBER=OFF
            ..
    - ninja
    - ninja install
    - cd $SOURCE
    - mkdir _build && cd _build
    - cmake -G "Ninja"
            -DCMAKE_BUILD_TYPE=Release
            -DCMAKE_INSTALL_PREFIX=$PREFIX
            -DENABLE_GTK_DOC=ON
            -DENABLE_CONTACT_MAPS=OFF
            -DENABLE_INSTALLED_TESTS=ON
            -DWITH_HELP=ON
            ..
    - ninja
    - ninja install
    - cd $SOURCE
    - mkdir _prefix
    - cp -r $PREFIX/share/help/ _prefix/help
    - xvfb-run -a -s "-screen 0 1024x768x24" bash ./.gitlab-ci/run-tests.sh
    - cp $DEPS/evolution-data-server/_build/evolution-data-server-config.h $DEPS/
  artifacts:
    name: "${CI_PROJECT_NAME}-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "_build/evolution-config.h"
      - "_deps/evolution-data-server-config.h"
      - "_prefix/help/"
      - "_build/docs/reference/evolution-mail-composer/html/"
      - "_build/docs/reference/evolution-mail-engine/html/"
      - "_build/docs/reference/evolution-mail-formatter/html/"
      - "_build/docs/reference/evolution-shell/html/"
      - "_build/docs/reference/evolution-util/html/"

flatpak:
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  stage: build
  tags:
    - flatpak
  variables:
    BUNDLE: "evolution-nightly.flatpak"
    MANIFEST_PATH: "data/org.gnome.Evolution.Devel.json"
    FLATPAK_MODULE: "evolution"
    APP_ID: "org.gnome.Evolution.Devel"
    RUNTIME_REPO: "https://nightly.gnome.org/gnome-nightly.flatpakrepo"
  when: manual
  before_script:
    - export PATH="/usr/lib/ccache:$PATH"
    - export CCACHE_BASEDIR="$PWD"
    - export CCACHE_DIR="$PWD/ccache"
  script:
    - jq -M '(.modules[] | select(.name=="evolution")).sources[0].url = env.CI_REPOSITORY_URL' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}
    - jq -M 'del((.modules[] | select(.name=="evolution")).sources[0].branch)' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}
    - jq -M '(.modules[] | select(.name=="evolution")).sources[0].commit = env.CI_COMMIT_SHA' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}

    - flatpak-builder --keep-build-dirs --user --disable-rofiles-fuse flatpak_app --repo=repo --ccache ${BRANCH:+--default-branch=$BRANCH} ${MANIFEST_PATH}
    - flatpak build-bundle repo ${BUNDLE} --runtime-repo=${RUNTIME_REPO} ${APP_ID} ${BRANCH}
  cache:
    paths:
      - ccache
      - .flatpak-builder
  artifacts:
    name: "Flatpak artifacts"
    expose_as: "Get Flatpak bundle here"
    when: always
    paths:
      - "${BUNDLE}"
    expire_in: 14 days

pages:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v1
  stage: pages
  dependencies:
    - fedora
  interruptible: false
  variables:
    FLATPAK_MODULE: "evolution"
  script:
    - |
      export PUBDIR=$(pwd)/public/help
      export DOCSDIR=$(pwd)/public/docs
      LANGLINKS=" "
      mkdir -p $PUBDIR
      mkdir -p $DOCSDIR
      for d in "_prefix/help/"*
      do
        if [ -d "$d" ] && [ $(basename $d) != "CMakeFiles" ]; then
          lang=$(basename $d);
          cd $d/evolution/
          echo "Generating help for $lang"
          if [ $lang == "C" ]; then
            yelp-build html -o $PUBDIR .
          else
            mkdir $PUBDIR/$lang
            yelp-build html -o $PUBDIR/$lang . -p ../C
            LANGLINKS="${LANGLINKS}<a href='help/$lang/'>$lang</a> "
          fi
          cd $PUBDIR/../..
        fi
      done
      gcc `pkg-config --cflags --libs glib-2.0` .gitlab-ci/update-help.c -g -O0 -o update-help
      ./update-help $PUBDIR
      echo "Go to Evolution <a href='https://gitlab.gnome.org/GNOME/evolution/-/wikis'>Wiki pages</a> or the <a href='help/'>user documentation</a> (${LANGLINKS})<br><br>Developer documentation is <a href='docs/'>here</a>">$(pwd)/public/index.html
      mkdir $(pwd)/public/YahooOAuth2
      echo "Wait a bit, please...<br><br>The page URL contains an authorization code, which can be copied into the credentials prompt dialog.">$(pwd)/public/YahooOAuth2/index.html
      mv "_build/docs/reference/evolution-mail-composer/html" $DOCSDIR/evolution-mail-composer
      mv "_build/docs/reference/evolution-mail-engine/html" $DOCSDIR/evolution-mail-engine
      mv "_build/docs/reference/evolution-mail-formatter/html" $DOCSDIR/evolution-mail-formatter
      mv "_build/docs/reference/evolution-shell/html" $DOCSDIR/evolution-shell
      mv "_build/docs/reference/evolution-util/html" $DOCSDIR/evolution-util
      echo "<h4>Developer documentation for Evolution libraries</h4><a href='evolution-mail-composer'>evolution-mail-composer</a><br><a href='evolution-mail-engine'>evolution-mail-engine</a><br><a href='evolution-mail-formatter'>evolution-mail-formatter</a><br><a href='evolution-shell'>evolution-shell</a><br><a href='evolution-util'>evolution-util</a><br>" >$DOCSDIR/index.html
  artifacts:
    paths:
    - public
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

tarball:
  stage: tarball
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  dependencies:
    - fedora
  allow_failure: false
  script:
    - git config --global --add safe.directory `pwd`
    - git archive --prefix=${TARBALL_PATH_PREFIX}/ HEAD | xz -z > ${TARBALL_NAME}
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    expire_in: 14 days
    paths:
      - "${TARBALL_NAME}"
  rules:
    - if: $CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED
