image: registry.gitlab.com/iniparser/docker-cmake:latest

variables:
    GIT_SUBMODULE_STRATEGY: recursive
    LD_LIBRARY_PATH: ${CI_BUILDS_DIR}/iniparser/iniparser/install/lib/


stages:
    - build
    - doc
    - test
    - win
    - prepare
    - release

build:
    stage: build
    script:
        - mkdir build
        - cd build
        - cmake -DCMAKE_INSTALL_PREFIX=../install -DBUILD_TESTING=ON -DBUILD_EXAMPLES=ON -DBUILD_DOCS=ON ..
        - make install
        - tree -a .
    artifacts:
        paths:
            - install/
            - build/

pages:
    stage: doc
    dependencies:
        - build
    script:
        - mv build/html/ public/
    artifacts:
        paths:
            - public
    rules:
        - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

test:
    stage: test
    script:
        - pwd
        - ls -l
        - cd build/
        - ctest --verbose
    dependencies:
        - build

memcheck:
    stage: test
    script:
        - pwd
        - ls -l
        - cd build/
        - valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=255 ctest --verbose
    dependencies:
        - build

iniexample-memcheck:
    stage: test
    script:
        - pwd
        - tree
        - cd install/share/doc/iniparser/examples/
        - valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=255 ./iniexample
    dependencies:
        - build

iniwrite-memcheck:
    stage: test
    script:
        - pwd
        - tree
        - cd install/share/doc/iniparser/examples/
        - valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=255 ./iniwrite
    dependencies:
        - build

parse-memcheck:
    stage: test
    script:
        - pwd
        - tree
        - cd install/share/doc/iniparser/examples/
        - valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=255 ./parse
    dependencies:
        - build

.shared_windows_runners:
    tags:
        - saas-windows-medium-amd64

win-build:
    extends:
        - .shared_windows_runners
    stage: win
    script:
        - git clone https://github.com/tronkko/dirent.git
        - mkdir build
        - cd build
        - cmake -DCMAKE_INCLUDE_PATH="../dirent/include" -DBUILD_TESTING=ON -DBUILD_EXAMPLES=ON ..
        - cmake --build .
        - Get-Location
        - Get-ChildItem –Recurse
        # due to the long provisioning time of the Windows runner, tests are
        # executed in the same stage as the build
        - ctest -C Debug --verbose
    artifacts:
        paths:
            - build/
    dependencies: []

prepare_job:
    stage: prepare
    image: alpine:latest
    rules:
    - if: '$CI_COMMIT_TAG =~ /^v?\d+\.\d+\.\d+$/'
    script:
        - apk add curl jq
        - 'curl -H "PRIVATE-TOKEN: $CI_API_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/repository/changelog?version=$CI_COMMIT_TAG" | jq -r .notes > release_notes.md'
        - 'curl -H "PRIVATE-TOKEN: $CI_API_TOKEN" -X POST "$CI_API_V4_URL/projects/$CI_PROJECT_ID/repository/changelog?version=$CI_COMMIT_TAG"'
    artifacts:
        paths:
        - release_notes.md

release_job:
    stage: release
    image: registry.gitlab.com/gitlab-org/release-cli:latest
    needs:
        - job: prepare_job
          artifacts: true
    rules:
    - if: '$CI_COMMIT_TAG =~ /^v?\d+\.\d+\.\d+$/'
    script:
        - echo "Creating release"
    release:
        name: 'Release $CI_COMMIT_TAG'
        description: release_notes.md
        tag_name: '$CI_COMMIT_TAG'
        ref: '$CI_COMMIT_SHA'
        #assets:
        #    links:
        #        - name: 'Container Image $CI_COMMIT_TAG'
        #          url: "https://$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA"
