image: debian:bullseye

variables:
  GIT_STRATEGY: clone
  GIT_SUBMODULE_STRATEGY: none

stages:
  # no point in waiting for the tests to end before generating the data files
  # or the development documentation
  - tests
  - data
  - deploy

check:
  stage: tests
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - linux
    - volatile
  script:
    - apt-get update
    - apt-get install -y flake8
    - apt-get install -y make
    - apt-get install -y pycodestyle
    - apt-get install -y python3-flake8
    - make check


test:
  stage: tests
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - linux
    - volatile
  script:
    - apt-get update
    - apt-get install -y build-essential
    - apt-get install -y gcc
    - apt-get install -y gettext
    - apt-get install -y git
    - apt-get install -y gobject-introspection
    - apt-get install -y gtk-doc-tools
    - apt-get install -y libcunit1-ncurses-dev
    - apt-get install -y libgirepository1.0-dev
    - apt-get install -y libjpeg-dev
    - apt-get install -y libsane-dev
    - apt-get install -y make
    - apt-get install -y meson
    - apt-get install -y python3-dev
    - apt-get install -y python3-gi
    - apt-get install -y python3-pil
    - apt-get install -y python3-scipy
    - apt-get install -y tox
    - apt-get install -y valac
    - apt-get install -y wget
    - apt-get install -y xvfb
    - apt-get install -y zlib1g-dev
    - source ./activate_test_env.sh && make install
    - source ./activate_test_env.sh && paperwork-gtk chkdeps -y
    - source ./activate_test_env.sh && paperwork-cli chkdeps -y
    - source ./activate_test_env.sh && make test


test_chkdeps:
  stage: tests
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - linux
    - volatile
  script:
    - apt-get update
    # bare minimum to install Paperwork from sources
    - apt-get install -y build-essential
    - apt-get install -y make
    - apt-get install -y python3
    - apt-get install -y python3-dev
    - apt-get install -y python3-pip
    - apt-get install -y python3-setuptools
    - apt-get install -y wget
    - apt-get install -y gettext
    - make install
    - paperwork-json chkdeps


generate_data:
  stage: data
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - linux
    - volatile
  script:
    - echo deb http://deb.debian.org/debian buster-backports main > /etc/apt/sources.list.d/backports.list
    - apt-get update
    - apt-get install -y build-essential
    - apt-get install -y doxygen
    - apt-get install -y gcc
    - apt-get install -y gettext
    - apt-get install -y git
    - apt-get install -y gobject-introspection
    - apt-get install -y graphviz
    - apt-get install -y gtk-doc-tools
    - apt-get install -y imagemagick
    - apt-get install -y libcunit1-ncurses-dev
    - apt-get install -y libgirepository1.0-dev
    - apt-get install -y libjpeg-dev
    - apt-get install -y libsane-dev
    - apt-get install -y make
    - apt-get install -y meson
    - apt-get install -y po4a
    - apt-get install -y python3-dev
    - apt-get install -y python3-gi
    - apt-get install -y python3-pil
    - apt-get install -y python3-virtualenv
    - apt-get install -y rclone
    - apt-get install -y texlive
    - apt-get install -y texlive-lang-english
    - apt-get install -y texlive-lang-french
    - apt-get install -y texlive-lang-german
    - apt-get install -y texlive-latex-extra
    - apt-get install -y texlive-latex-recommended
    - apt-get install -y valac
    - apt-get install -y valgrind
    - apt-get install -y virtualenv
    - apt-get install -y wget
    - apt-get install -y xvfb
    - apt-get install -y zlib1g-dev
    - source ./activate_test_env.sh && make install
    - source ./activate_test_env.sh && xvfb-run paperwork-gtk chkdeps -y
    - source ./activate_test_env.sh && xvfb-run paperwork-cli chkdeps -y
    - source ./activate_test_env.sh && make data upload_data


doc_devel:
  stage: data
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - linux
    - volatile
  script:
    - apt-get update
    - apt-get install -y build-essential
    - apt-get install -y doxygen
    - apt-get install -y gettext
    - apt-get install -y git
    - apt-get install -y gobject-introspection
    - apt-get install -y graphviz
    - apt-get install -y gtk-doc-tools
    - apt-get install -y libcunit1-ncurses-dev
    - apt-get install -y libgirepository1.0-dev
    - apt-get install -y libsane-dev
    - apt-get install -y make
    - apt-get install -y meson
    - apt-get install -y openjdk-11-jre
    - apt-get install -y plantuml
    - apt-get install -y plantuml
    - apt-get install -y python3-dev
    - apt-get install -y python3-gi
    - apt-get install -y python3-pil
    - apt-get install -y python3-recommonmark
    - apt-get install -y python3-sphinx
    - apt-get install -y python3-sphinxcontrib.plantuml
    - apt-get install -y python3-virtualenv
    - apt-get install -y rclone
    - apt-get install -y valac
    - apt-get install -y valgrind
    - apt-get install -y virtualenv
    - apt-get install -y wget
    - apt-get install -y zlib1g-dev
    - source ./activate_test_env.sh && make doc
    - source ./activate_test_env.sh && make upload_doc


linux_flatpak:
  stage: deploy
  timeout: 48h
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - openpaper-flatpak
  script:
    # Running in from a gitlab-runner directly in a shell, as the user
    # 'gitlab-runner'
    # --> not running as root, so we cannot actually install anything
    # - apt-get update
    # - apt-get install -y -q rsync flatpak-builder make
    - ./ci/update_flatpak_repo.sh


.windows: &windows
  variables:
    MSYSTEM: "MINGW64"
    CHERE_INVOKING: "yes"
  before_script:
    # Libinsane build dependencies
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S make
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-cunit
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-doxygen
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-gcc
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-gobject-introspection
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-meson
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python3-gobject
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-vala
    # Paperwork build dependencies
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-gdk-pixbuf2
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-cairo
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python3-cairo
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-gtk3
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python3-pillow
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-ca-certificates
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python3-setuptools
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python3-pip
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-libhandy
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-libnotify
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-poppler
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python-psutil
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-gettext
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S git  # for 'make version'
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S wget  # for downloading data files
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python3-cx_Freeze
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S zip unzip
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-nsis
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-nsis-nsisunz
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-python-scikit-learn
    # Workaround for pycountry and Cx_freeze
    # See https://github.com/marcelotduarte/cx_Freeze/issues/930
    - c:\msys64\usr\bin\bash -lc "pip3 uninstall -y pycountry"
    - c:\msys64\usr\bin\bash -lc "pip3 install --no-cache --use-pep517 pycountry==20.7.3"
    - git submodule init
    - git submodule update --recursive --remote
    - c:\msys64\usr\bin\bash -lc "make clean"
    - c:\msys64\usr\bin\bash -lc "make uninstall"
    - c:\msys64\usr\bin\bash -lc "make -C sub/libinsane uninstall || true"
    - c:\msys64\usr\bin\bash -lc "make -C sub/libpillowfight uninstall || true"
    - c:\msys64\usr\bin\bash -lc "make -C sub/pyocr uninstall || true"
    # a 2nd time just to be really sure
    # (that's the problem when can't use containers ..)
    - c:\msys64\usr\bin\bash -lc "make uninstall"
    - c:\msys64\usr\bin\bash -lc "make -C sub/libinsane uninstall || true"
    - c:\msys64\usr\bin\bash -lc "make -C sub/libpillowfight uninstall || true"
    - c:\msys64\usr\bin\bash -lc "make -C sub/pyocr uninstall || true"


windows_tests:
  stage: tests
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - windows
    - msys2
  <<: *windows
  script:
    # Tesseract (required for unit tests)
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-libarchive  # missing tesseract dependency
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-tesseract-ocr
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-tesseract-data-eng
    - c:\msys64\usr\bin\pacman --needed --noconfirm -S mingw-w64-x86_64-tesseract-data-fra
    # Build
    - c:\msys64\usr\bin\bash -lc "export TESSDATA_PREFIX=/mingw64/share/tessdata && make install test"
    - c:\msys64\usr\bin\bash -lc "make uninstall"


windows_exe:
  stage: deploy
  only:
    - branches@World/OpenPaperwork/paperwork
    - tags@World/OpenPaperwork/paperwork
  tags:
    - windows
    - msys2
  <<: *windows
  script:
    # We need rclone to upload the files on OVH object storage
    - c:\msys64\usr\bin\rm -f rclone-v1.53.3-windows-386.zip
    - c:\msys64\usr\bin\rm -rf rclone-v1.53.3-windows-386
    - c:\msys64\usr\bin\wget -q https://github.com/rclone/rclone/releases/download/v1.53.3/rclone-v1.53.3-windows-386.zip
    - c:\msys64\usr\bin\unzip rclone-v1.53.3-windows-386.zip
    - c:\msys64\usr\bin\cp rclone-v1.53.3-windows-386/rclone.exe /usr/bin
    # Build
    - c:\msys64\usr\bin\bash -lc "make windows_exe"
    - c:\msys64\usr\bin\bash -lc "./ci/deliver.sh dist/paperwork.zip windows .zip"
    - c:\msys64\usr\bin\bash -lc "make uninstall"
