stages:          # List of stages for jobs, and their order of execution
  - build
  - pretest
  - unittest
  - packaging
  - secret_detection

variables:
  EXTLIB: "/usr/local/perl5/"
  PERL5LIB: "$EXTLIB/lib/perl5/:lib"
  # Make sure to use only codenames that are supported by SLUB's Debian repo server!
  DEBIAN_DEFAULT_RELEASE: "bookworm"
  DEBIAN_RELEASE: "${DEBIAN_DEFAULT_RELEASE}"
  DEBIAN_RELEASES: "${DEBIAN_DEFAULT_RELEASE}"
  IMAGE_TARGET: "${HARBOR_HOST}/${HARBOR_PROJECT}/${DEBIAN_DEFAULT_RELEASE}_${HARBOR_PROJECT}"
  FF_USE_FASTZIP: "true"
  # These can be specified per job or per pipeline
  ARTIFACT_COMPRESSION_LEVEL: "fast"
  CACHE_COMPRESSION_LEVEL: "fast"
#  CI_DEBUG_TRACE: "true"
  CACHEDIR: "${CI_PROJECT_NAME}/${CI_COMMIT_BRANCH}/"

default:
  timeout: 30m
  image:
    # use this image for all later stages that happen after the build stage
    name: "${IMAGE_TARGET}:latest"
  before_script:    # These steps are run before EACH job.
    - export PERL5LIB
    - export PATH="$PATH:$EXTLIB/bin"
    - export Log4perl=1
  cache:
    paths:
      - ${CACHEDIR}
    #- docker login -u ${HARBOR_USER} -p ${HARBOR_PASSWORD} ${HARBOR_HOST}
  #after_script:
    #- docker logout

build-env-job:       # This job runs in the build stage, which runs first.
  stage: build
  tags:
    - "docker"
  image:
    # Use Kaniko base image to build a Docker image to use as the base image for later jobs.
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  # Do not run the before_script tasks here, they wouldn't be included in the Docker image. Instead, provide an empty list of tasks.
  before_script: []
  # docu: https://docs.gitlab.com/ee/ci/docker/using_kaniko.html, this is basically copy-pasted from there
  script:
    - mkdir -p /kaniko/.docker;
    - echo -n "{\"auths\":{\"${HARBOR_HOST}\":{\"auth\":\"$(echo -n ${HARBOR_USERNAME}:${HARBOR_PASSWORD} | base64 -w 0)\"}},\"$(printf "%s" "$CI_DEPENDENCY_PROXY_SERVER" | cut -d':' -f1)\":{\"auth\":\"$( printf "%s:%s" "$CI_DEPENDENCY_PROXY_USER" "$CI_DEPENDENCY_PROXY_PASSWORD" | base64 -w 0 )\"}}}" | tee /kaniko/.docker/config.json
    - echo "CI_PROJECT_DIR=${CI_PROJECT_DIR}";
    # In this task, Kaniko executor is called to build the Image based on the Dockerfile provided with "--dockerfile".
    - >-
      /kaniko/executor
      --context "${CI_PROJECT_DIR}"
      --dockerfile "${CI_PROJECT_DIR}/gitlab-ci/Dockerfile"
      --destination "${IMAGE_TARGET}:latest"
      --build-arg "GITDIR=${CI_PROJECT_DIR}"
      --build-arg "DEBIAN_RELEASE"
      --cache=true
      --cache-repo=${CI_REGISTRY_IMAGE}
      --cache-copy-layers=true
      --snapshot-mode=redo
      --use-new-run
      --ignore-var-run

perl-unit-test-job:   # This job runs in the test stage.
    stage: unittest    # It only starts when the job in the build stage completes successfully.
    timeout: 60m
    tags:
      - "docker"
    script:
      - prove -l t/
    dependencies:
      - build-env-job

perl-quality-test-job:   # This job runs in the test stage.
    stage: pretest    # It only starts when the job in the build stage completes successfully.
    timeout: 30m
    tags:
      - "docker"
    script:
      - prove -l xt/
    dependencies:
      - build-env-job

create-debian-packages:
  stage: packaging
  timeout: 30m
  tags:
    - "docker"
  script:
    - mkdir debian
    - |
      cat <<RULES | sed -e "s/##TAB##/\\t/g" > debian/rules
      #!/usr/bin/make -f
      export DH_VERBOSE=1
      %:
      ##TAB##dh \$@ --with dist-zilla
      RULES
    - |
      cat <<CONTROL | sed -e "s/##TAB##/\\t/g" > debian/control
      Source: tools-for-technical-analysts
      Priority: extra
      Maintainer: andreas.romeyke@slub-dresden.de
      Section: main
      Build-Depends:  apt-file, \
      apt-utils, \
      asciidoc-base, \
      asciidoctor, \
      bash, \
      build-essential, \
      coreutils, \
      cpanminus, \
      common-application4archivematica, \
      dh-make-perl, \
      dh-dist-zilla, \
      git, \
      libapp-cmd-perl, \
      libc-dev-bin, \
      libc6-dev, \
      libcryptx-perl, \
      libdate-calc-perl, \
      libdata-printer-perl, \
      libperl-prereqscanner-perl, \
      libperl-version-perl, \
      libregexp-optimizer-perl, \
      libsoap-lite-perl, \
      libssl-dev, \
      libsys-cpu-perl, \
      libtest-cmd-perl, \
      libtext-csv-perl, \
      libtext-csv-xs-perl, \
      zlib1g-dev

      Package: tools-for-technical-analysts
      Architecture: any
      Section: main
      Priority: extra
      Description: Tools for Technical Analysts for Archivematica, an open source based digital preservation system by Artefactual
      Depends: common-application4archivematica, \
      libcryptx-perl, \
      libdate-calc-perl, \
      libdata-printer-perl, \
      libperl-prereqscanner-perl, \
      libperl-version-perl, \
      libregexp-optimizer-perl, \
      libsoap-lite-perl
      CONTROL
    - |
      cat <<CHANGELOG > debian/changelog
      tools-for-technical-analysts (1.0) stable; urgency=medium
      CHANGELOG
    - echo 10 > debian/compat
    - echo "(c) 2015-2024 Romeyke @ SLUB Dresden" > debian/copyright
    - cat -e debian/rules
    - ls -lha ../
    - dpkg-buildpackage -uc -us
    - ls -lha ./
    - ls -lha ../
    - mkdir -p artifacts/
    - cp ../*.deb artifacts/
    #- cd ../ ; dh-make-perl ${CI_PROJECT_DIR}
  dependencies:
    - perl-unit-test-job
    - perl-quality-test-job
  artifacts:
    paths:
      - artifacts/*.deb