# Setup the pipeline for deploying the project homepage
# Copyright (c) UNICADO 2023
#
# 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/>.

# === Configure pipeline ===
stages:
  - build
  - deploy

# === Build the doxygen documentation ===
doxygen:
  image: alpine:latest
  stage: build
  tags:
    - documentation
  before_script:
    - apk update && apk --no-cache add git doxygen graphviz ttf-freefont texmf-dist texmf-dist-latexextra texlive texlive-dvi
  script:
    # clone repos
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@git.rwth-aachen.de/unicado/aircraft-design
    - cd aircraft-design
    - git clone --recurse-submodules https://gitlab-ci-token:${CI_JOB_TOKEN}@git.rwth-aachen.de/unicado/libraries libs/
    # create aircraft design documentation
    - cd propulsion_design/doc/ && ( cat Doxyfile ; echo "OUTPUT_DIRECTORY = $CI_PROJECT_DIR/docs/documentation/" ) | doxygen -
    - cd ../../wing_design/doc/ && ( cat Doxyfile ; echo "OUTPUT_DIRECTORY = $CI_PROJECT_DIR/docs/documentation/" ) | doxygen -
    - cd ../../empennage_design/doc/ && ( cat Doxyfile ; echo "OUTPUT_DIRECTORY = $CI_PROJECT_DIR/docs/documentation/" ) | doxygen -
    - cd ../../ecological_assessment/doc/ && ( cat Doxyfile ; echo "OUTPUT_DIRECTORY = $CI_PROJECT_DIR/docs/documentation/" ) | doxygen -
    # create libraries documentation
    - cd ../../libs/aircraftGeometry2/doc/ && ( cat Doxyfile ; echo "OUTPUT_DIRECTORY = $CI_PROJECT_DIR/docs/documentation/" ) | doxygen -
    - cd ../../engine/doc/ && ( cat Doxyfile ; echo "OUTPUT_DIRECTORY = $CI_PROJECT_DIR/docs/documentation/" ) | doxygen -
    - cd $CI_PROJECT_DIR

  artifacts:
    paths:
      - $CI_PROJECT_DIR/docs/documentation
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: on_success
    - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "trigger"'
      when: on_success

# === Build and deploy the website ===
pages:
  image: python:latest
  stage: deploy
  tags:
    - documentation
  before_script:
    - pip install pipenv
    - pipenv install
  script:cmks
    - pipenv run mkdocs build --site-dir $CI_PROJECT_DIR/public
  needs:
    - doxygen
  artifacts:
    paths:
      - $CI_PROJECT_DIR/public
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: on_success
    - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'
      when: manual
    - if: '$CI_PIPELINE_SOURCE == "trigger"'
      when: on_success