image: debian:testing

stages:
  - prepare
  - test
  - build
  - www
  - distribution

variables:
  INSTALL_DIR: "_install"
  INSTALL_PREFIX: "${CI_PROJECT_DIR}/${INSTALL_DIR}"

build-image:
  stage: prepare
  variables:
    GIT_STRATEGY: none
  cache: {}
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - echo "FROM debian:testing" > Dockerfile
    - echo "RUN apt-get update" >> Dockerfile
    - echo "RUN apt-get install -y --no-install-recommends \\" >> Dockerfile
    - echo "automake build-essential docbook-website docbook-xml docbook-xsl fonts-noto-cjk fonts-noto-core gettext git intltool librsvg2-2 librsvg2-bin pkg-config pngcrush python3 python3-libxml2 python3-polib xsltproc libxml2-utils" >> Dockerfile

    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:build-debian-latest --cache=true --cache-ttl=120h

po-validate:
  stage: test
  image: $CI_REGISTRY_IMAGE:build-debian-latest
  script:
    - cd po
    - find . -type f -print0 | xargs -0 ../tools/validate_po.py $1 $2
  needs: ["build-image"]

check-broken-images:
  stage: test
  image: $CI_REGISTRY_IMAGE:build-debian-latest
  script:
    # We need a src/preface/authors.xml or validate_references will fail.
    # Since it doesn't contain images, its real contents are not important.
    - cat src/help-missing.xml > src/preface/authors.xml
    - if ./tools/validate_references.py -b | grep 'BROKEN:'; then echo "ERROR missing images found!" && false; exit 2; fi;
  needs: ["build-image"]

.build-debian-base:
  stage: build
  image: $CI_REGISTRY_IMAGE:build-debian-latest
  artifacts:
    expire_in: 1 week
    when: always
    paths:
    - "${HELP_DIR}"
    - "${PDF_DIR}"
    - _log
  script:
    - cd src
    # Mark all source files as updated. Needed to get up-to-date translations
    - find . -type f -exec touch {} +
    - cd ..
    - mkdir _build
    - cd _build
    - ../autogen.sh --prefix=${INSTALL_PREFIX} --without-gimp
    # Make po first to get validate to use up-to-date translations, and also to stop plain make from redoing the xml
    - make po
    - make validate-all
    # Test if any xml is present in logs, which means failed validation
    - if [ `ls -1 log/*.xml 2>/dev/null | wc -l ` -gt 0 ]; then mv log/ ../_log/; echo 'Validation failed!' && false; exit 2; else rm -f log/*.log; fi
    - make && make install
    - mv ${INSTALL_PREFIX}/share/gimp/3.0/help/ ../${HELP_DIR}
    - mv ../${HELP_DIR}/pdf/ ../${PDF_DIR}
  timeout: 2 hours 30 minutes
  needs: ["build-image"]

build-debian-1:
  extends: .build-debian-base
  variables:
    LINGUAS: "bg ca cs da de el"
    HELP_DIR: "_html1"
    PDF_DIR: "_pdf1"

build-debian-2:
  extends: .build-debian-base
  variables:
    LINGUAS: "en en_GB es fa fi fr"
    HELP_DIR: "_html2"
    PDF_DIR: "_pdf2"

build-debian-3:
  extends: .build-debian-base
  variables:
    LINGUAS: "hr hu it ja ko lt"
    HELP_DIR: "_html3"
    PDF_DIR: "_pdf3"

build-debian-4:
  extends: .build-debian-base
  variables:
    LINGUAS: "nl nn pl pt pt_BR ro"
    HELP_DIR: "_html4"
    PDF_DIR: "_pdf4"

build-debian-5:
  extends: .build-debian-base
  variables:
    LINGUAS: "ru sl sv tr uk zh_CN"
    HELP_DIR: "_html5"
    PDF_DIR: "_pdf5"

www-debian:
  stage: www
  variables:
    BUILD_DIR: "."
  image: $CI_REGISTRY_IMAGE:build-debian-latest
  dependencies:
    - build-debian-1
    - build-debian-2
    - build-debian-3
    - build-debian-4
    - build-debian-5
  artifacts:
    expire_in: 1 week
    when: always
    paths:
    - build/windows/installer/lang/
    - htdocs/
  script:
    # build the installer language files only.
    - mkdir _build
    - cd _build
    - ../autogen.sh --prefix=${INSTALL_PREFIX} --without-gimp --enable-windows-installer
    - cd build/windows/installer/lang
    - make
    - cd ../../../..
    - mv build/windows/installer/lang/help*isl ../build/windows/installer/lang/
    # Mark all source files as updated. Needed to get up-to-date translation percentages
    - cd ../src
    - find . -type f -exec touch {} +
    # update the po files or translation percentages will be incorrect
    - cd ../_build
    - make po
    # build the landing pages.
    - cd ../web
    - make
    - cd ..
    - mv web/html/ htdocs/
    # Move all PDF quickreferences
    - MAJOR_VERSION=`grep 'm4_define(\[help_major_version' configure.ac |sed 's/m4_define(\[help_major_version.*\[\([0-9]*\)\].*/\1/'`
    - MINOR_VERSION=`grep 'm4_define(\[help_minor_version' configure.ac |sed 's/m4_define(\[help_minor_version.*\[\([0-9]*\)\].*/\1/'`
    - mkdir -p htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/pdf
    - mv _pdf1/*.pdf htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/pdf/
    - mv _pdf2/*.pdf htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/pdf/
    - mv _pdf3/*.pdf htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/pdf/
    - mv _pdf4/*.pdf htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/pdf/
    - mv _pdf5/*.pdf htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/pdf/
    # Move all the help html files.
    - mv _html1/* htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/
    - mv _html2/* htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/
    - mv _html3/* htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/
    - mv _html4/* htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/
    - mv _html5/* htdocs/${MAJOR_VERSION}.${MINOR_VERSION}/

win-installer:
  variables:
    CHERE_INVOKING: "yes"
  tags:
    - win32-ps
  stage: distribution
  dependencies:
    - www-debian
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_REF_SLUG}"
    when: always
    expire_in: 1 week
    paths:
    - build/windows/installer/_Output
    - installer.log
  script:
    - C:\msys64\usr\bin\pacman --noconfirm -Syyuu
    - C:\msys64\usr\bin\bash -lc "bash -x ./build/windows/installer/installer-gimp-help-msys2.sh > installer.log 2>&1"

.distcheck-base:
  rules:
    # On releases.
    - if: '$CI_COMMIT_TAG != null'
    # Custom builds though web GUI, API or schedules.
    - if: '$GIMP_HELP_CI_MAKE_DIST != null'
    # Merge requests with appropriate label.
    - if: '$CI_MERGE_REQUEST_LABELS =~ /.*5. Source Distribution.*/'
  stage: test
  image: $CI_REGISTRY_IMAGE:build-debian-latest
  script:
    - cd src
    # Mark all source files as updated. Needed to get up-to-date translations
    - find . -type f -exec touch {} +
    - cd ..
    - mkdir _build
    - cd _build
    - ../autogen.sh --prefix=${INSTALL_PREFIX} --without-gimp
    - make distcheck

  needs: ["build-image"]

# One make distcheck would be ideal, but the reality is that on certain runners
# we don't finish within the 1 hour timeout. Let's split it into several jobs.

distcheck-1:
  extends: .distcheck-base
  variables:
    LINGUAS: "bg ca cs da de el"

distcheck-2:
  extends: .distcheck-base
  variables:
    LINGUAS: "en en_GB es fa fi fr"

distcheck-3:
  extends: .distcheck-base
  variables:
    LINGUAS: "hr hu it ja ko lt"

distcheck-4:
  extends: .distcheck-base
  variables:
    LINGUAS: "nl nn pl pt pt_BR ro"

distcheck-5:
  extends: .distcheck-base
  variables:
    LINGUAS: "ru sl sv tr uk zh_CN"

source-dist:
  rules:
    # On releases.
    - if: '$CI_COMMIT_TAG != null'
    # Custom builds though web GUI, API or schedules.
    - if: '$GIMP_HELP_CI_MAKE_DIST != null'
    # Merge requests with appropriate label.
    - if: '$CI_MERGE_REQUEST_LABELS =~ /.*5. Source Distribution.*/'
  stage: distribution
  image: $CI_REGISTRY_IMAGE:build-debian-latest
  artifacts:
    expire_in: 1 week
    when: always
    paths:
    #- _build
    - _dist
  script:
    - cd src
    # Mark all source files as updated. Needed to get up-to-date translations
    - find . -type f -exec touch {} +
    - cd ..
    - mkdir _dist
    - mkdir _build
    - cd _build
    - ../autogen.sh --prefix=${INSTALL_PREFIX} --without-gimp
    - make dist
    - FILENAME=`ls gimp-help-*.tar.bz2`                               &&
      sha256sum gimp-help-*.tar.bz2 > ../_dist/${FILENAME}.SHA256SUMS &&
      sha512sum gimp-help-*.tar.bz2 > ../_dist/${FILENAME}.SHA512SUMS
    - mv gimp-help-*.tar.bz2 ../_dist/

  needs: ["build-image"]
