stages:
  - test
  - pages

flatpak:
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  stage: test
  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"
  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}

    - >
      xvfb-run -a -s "-screen 0 1024x768x24"
      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}"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/evolution-config.h"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/CMakeFiles/CMakeError.log"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/CMakeFiles/CMakeOutput.log"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/help/"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-mail-composer/html/"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-mail-engine/html/"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-mail-formatter/html/"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-shell/html/"
      - ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-util/html/"
    expire_in: 14 days

pages:
  image: registry.fedoraproject.org/fedora:40
  stage: pages
  dependencies:
    - flatpak
  interruptible: false
  variables:
    FLATPAK_MODULE: "evolution"
  script:
    - |
      dnf install -qy yelp-tools gcc glib2-devel
      export PUBDIR=$(pwd)/public/help
      export DOCSDIR=$(pwd)/public/docs
      LANGLINKS=" "
      mkdir -p $PUBDIR
      mkdir -p $DOCSDIR
      for d in ".flatpak-builder/build/${FLATPAK_MODULE}/help/"*
      do
        if [ -d "$d" ] && [ $(basename $d) != "CMakeFiles" ]; then
          lang=$(basename $d);
          cd $d
          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 ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-mail-composer/html" $DOCSDIR/evolution-mail-composer
      mv ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-mail-engine/html" $DOCSDIR/evolution-mail-engine
      mv ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-mail-formatter/html" $DOCSDIR/evolution-mail-formatter
      mv ".flatpak-builder/build/${FLATPAK_MODULE}/docs/reference/evolution-shell/html" $DOCSDIR/evolution-shell
      mv ".flatpak-builder/build/${FLATPAK_MODULE}/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
