# UNICADO - UNIversity Conceptual Aircraft Design and Optimization
#
# Copyright (C) 2025 UNICADO consortium
#
# 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/>.
#
# Description:
# This file is part of UNICADO.

# === Configure pipeline ===
# This section defines the stages of the pipeline: build and deploy
stages:
  - build   # The 'build' stage is where documents are copied to the PROJECT_DIR
  - deploy  # The 'deploy' stage generates and deploys the generated documentation.

# === Clone the repositories / copy documentation to the  PROJECT_DIR ===
clone:
  image: alpine:latest
  stage: build  # This job is part of the build stage
  tags:
    - documentation  # Label for the job to be picked up by appropriate runners
  before_script:
  # Install necessary packages, including git, doxygen, and other dependencies
    - 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/
    # Change to the project directory (useful for multi-directory repositories)
      - cd $CI_PROJECT_DIR
      - ls -la $CI_PROJECT_DIR
  artifacts:
    # Save the generated documentation as artifacts so they can be accessed later in the pipeline
    paths:
      - $CI_PROJECT_DIR/aircraft-design
      - $CI_PROJECT_DIR/libraries
      - $CI_PROJECT_DIR/docs/documentation
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'  # Run when the commit is on the default branch
      when: on_success  # Only run if the previous jobs are successful
    - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'  # Allow manual triggers on non-default branches
      when: manual  # Run only when triggered manually
    - if: '$CI_PIPELINE_SOURCE == "trigger"'  # Triggered by another pipeline
      when: on_success  # Run if the source pipeline was successful

# === Build and deploy the website ===
pages:
  image: python:latest
  stage: deploy  # This job is part of the deploy stage
  tags:
    - documentation  # Label for the job to be picked up by appropriate runners
  before_script:
    # Install pipenv to manage Python dependencies
    - apk update && apk --no-cache add graphviz
    - pip install pipenv
    - pipenv install  # Install the dependencies from the Pipfile and Pipfile.lock
    - apt-get update
    - apt-get install -y doxygen
    - export DOXYGEN_BIN=/usr/bin/doxygen
    - pipenv install --dev  # Install all necessary dependencies
  script:
    # Build the MkDocs documentation site
    - pipenv run mkdocs build --verbose --site-dir $CI_PROJECT_DIR/public
  needs:
    - clone  # This job depends on the successful completion of the clone job
  artifacts:
    # Save the generated static website files as artifacts
    paths:
      - $CI_PROJECT_DIR/public
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'  # Run when the commit is on the default branch
      when: on_success  # Only run if the previous jobs are successful
    - if: '$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH'  # Allow manual triggers on non-default branches
      when: manual  # Run only when triggered manually