# .gitlab-ci.yml -- Pamhyr gitlab-ci
# Copyright (C) 2023  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

stages:
  - downloads
  - build
  - test
  - package
  - release

#############
# DOWNLOADS #
#############

dl-mage-linux:
  stage: downloads
  tags:
    - linux
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  script:
    - curl -L -o mage.tgz https://gitlab.irstea.fr/jean-baptiste.faure/mage/-/releases/Test6/downloads/packages/mage_linux.tgz
    - mkdir -p mage-linux
    - cd mage-linux
    - tar xvf ../mage.tgz
  artifacts:
    paths:
      - mage-linux/mage
      - mage-linux/mage_extraire
      - mage-linux/mailleurPF

dl-mage-windows:
  stage: downloads
  tags:
    - linux
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  script:
    - curl -L -o mage.tgz https://gitlab.irstea.fr/jean-baptiste.faure/mage/-/releases/Test6/downloads/packages/mage_windows.tgz
    - mkdir -p mage-windows
    - cd mage-windows
    - tar xvf ../mage.tgz
  artifacts:
    paths:
      - mage-windows/mage.exe
      - mage-windows/mage_extraire.exe
      - mage-windows/mailleurPF.exe

#########
# BUILD #
#########

set-version:
  stage: build
  tags:
    - linux
  script:
    - cd packages
    - ./version.sh "$CI_COMMIT_BRANCH" "$CI_COMMIT_TAG" "$CI_COMMIT_SHORT_SHA"
  artifacts:
    paths:
      - VERSION

build-lang:
  stage: build
  tags:
    - linux
  script:
    - cd ./src/lang/
    - ./create_ts.sh
  artifacts:
    paths:
      - src/lang/*.qm

build-users-doc:
  stage: build
  tags:
    - linux
  script:
    - cd ./doc/users/
    - ./build.sh
  artifacts:
    paths:
      - doc/users/documentation.pdf

build-developers-doc:
  stage: build
  tags:
    - linux
  script:
    - cd ./doc/dev/
    - ./build.sh
  artifacts:
    paths:
      - doc/dev/documentation.pdf

build-linux:
  stage: build
  tags:
    - linux
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  needs:
    - job: dl-mage-linux
      artifacts: true
    - job: set-version
      artifacts: true
    - job: build-lang
      artifacts: true
    - job: build-developers-doc
      artifacts: true
    - job: build-users-doc
      artifacts: true
  script:
    - mkdir -p linux
    - cd linux
    # Setup virtual env
    - python3 -m venv linux-venv
    - . linux-venv/bin/activate
    - pip3 install -r ../requirements.txt
    # Run Pyinstaller
    - pyinstaller -y --paths linux-venv/lib/python3.8/site-packages ../src/pamhyr.py
    # Create directory
    - mkdir -p pamhyr
    - mkdir -p pamhyr/pamhyr
    # Copy Pamhyr info
    - cp -v ../VERSION pamhyr/pamhyr/
    - cp -v ../AUTHORS pamhyr/pamhyr/
    - cp -v ../LICENSE pamhyr/pamhyr/
    # Copy mage
    - mkdir -p pamhyr/mage
    - cp -v ../mage-linux/* pamhyr/mage/
    # Copy Pamhyr
    - cp -r dist/pamhyr pamhyr/
    # Copy Pamhyr resources
    - mkdir -p pamhyr/pamhyr/View
    - mkdir -p pamhyr/pamhyr/View/ui
    - mkdir -p pamhyr/pamhyr/View/ui/Widgets
    - cp -r ../src/View/ui/*.ui pamhyr/pamhyr/View/ui/
    - cp -r ../src/View/ui/ressources/ pamhyr/pamhyr/View/ui
    - cp -r ../src/View/ui/Widgets/*.ui pamhyr/pamhyr/View/ui/Widgets
    # Copy lang
    - mkdir -p pamhyr/pamhyr/lang
    - cp -r ../src/lang/*.qm pamhyr/pamhyr/lang
    # Copy tests cases
    - mkdir -p pamhyr/tests_cases/
    - mkdir -p pamhyr/tests_cases/Saar
    - cp ../tests_cases/Saar/Saar.pamhyr pamhyr/tests_cases/Saar/
    # Copy documentations
    - mkdir -p pamhyr/doc/
    - cp ../doc/users/documentation.pdf pamhyr/doc/Pamhyr2-users.pdf
    - cp ../doc/dev/documentation.pdf pamhyr/doc/Pamhyr2-dev.pdf
    # Create running script
    - echo "#!/bin/sh" > pamhyr/Pamhyr2
    - echo "./pamhyr/pamhyr $@" >> pamhyr/Pamhyr2
    - chmod u+x pamhyr/Pamhyr2
  artifacts:
    paths:
      - linux/pamhyr

build-windows:
  stage: build
  tags:
    - wine
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  needs:
    - job: dl-mage-windows
      artifacts: true
    - job: set-version
      artifacts: true
    - job: build-lang
      artifacts: true
    - job: build-developers-doc
      artifacts: true
    - job: build-users-doc
      artifacts: true
  script:
    - mkdir windows
    - cd windows
    # Setup env
    - python -m pip install -r ..\requirements.txt
    # Run Pyinstaller
    - pyinstaller -y ..\src\pamhyr.py
    # Create directory
    - mkdir pamhyr
    - dir
    - copy ..\packages\make-windows-dir.bat .\make-windows-dir.bat
    - .\make-windows-dir.bat
  artifacts:
    paths:
      - windows/pamhyr

#########
# TESTS #
#########

test:
  stage: test
  tags:
    - linux
  needs:
    - job: set-version
      artifacts: true
  script:
    - echo "TODO PAMHYR tests"

############
# PACKAGES #
############

linux-package-tar:
  stage: package
  tags:
    - release
    - linux
  needs:
    - job: build-linux
      artifacts: true
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  artifacts:
    paths:
      - linux/pamhyr-gnulinux.tar.xz
  script:
    - cd linux
    - tar --xz -cf pamhyr-gnulinux.tar.xz ./pamhyr --checkpoint=.100

windows-package-zip:
  stage: package
  tags:
    - release
    - linux
  needs:
    - job: build-windows
      artifacts: true
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  artifacts:
    paths:
      - windows/pamhyr-windows.zip
  script:
    - cd windows
    - zip -r pamhyr-windows.zip pamhyr

windows-package-exe:
  stage: package
  tags:
    - release
    - wine
  needs:
    - job: build-windows
      artifacts: true
  rules:
    - if: $CI_COMMIT_BRANCH == 'ci-test' || $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_TAG
  artifacts:
    paths:
      - windows/pamhyr-windows.exe
  script:
    - cd windows
    - copy ..\packages\make-windows-exe.bat make-windows-exe.bat
    - copy ..\packages\pamhyr.nsi pamhyr.nsi
    - .\make-windows-exe.bat

###########
# RELEASE #
###########

tag-release:
  stage: release
  tags:
    - release
    - linux
  needs:
    - job: linux-package-tar
      artifacts: true
    - job: windows-package-zip
      artifacts: true
    - job: windows-package-exe
      artifacts: true
  rules:
    - if: $CI_COMMIT_TAG
  artifacts:
    paths:
      - linux/pamhyr-gnulinux.tar.xz
      - packages/pamhyr-windows.zip
      - packages/pamhyr-windows.exe
  script:
    - cd packages
  release:
    name: '$CI_COMMIT_TAG'
    description: 'Automatic release from tag $CI_COMMIT_TAG'
    tag_name: '$CI_COMMIT_TAG'
    ref: '$CI_COMMIT_SHA'
    assets:
      links:
        - name: 'GNU/Linux (tar.xz)'
          url: '${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/packages/pamhyr-gnulinux.tar.xz'
          filepath: '/linux/pamhyr-gnulinux.tar.xz'
          link_type: 'package'
        - name: 'Windows (exe)'
          url: '${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/packages/pamhyr-windows.exe'
          filepath: '/windows/pamhyr-windows.exe'
          link_type: 'package'
        - name: 'Windows (zip)'
          url: '${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/packages/pamhyr-windows.zip'
          filepath: '/windows/pamhyr-windows.zip'
          link_type: 'package'
        - name: 'User documentations (pdf)'
          url: '${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/doc/users/documentation.pdf'
          filepath: '/doc/users/documentation.pdf'
          link_type: 'other'
        - name: 'Dev documentations (pdf)'
          url: '${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/doc/dev/documentation.pdf'
          filepath: '/doc/dev/documentation.pdf'
          link_type: 'other'