stages:
  - build
  - test
  - testall

build:
  stage: build
  image: debian:unstable
  script:
    - apt-get -y update
    - bash -c 'mkdir -p /usr/share/man/man{0..10}'
    - apt-get -y install build-essential devscripts fakeroot dh-exec jdupes doxygen graphviz php-cli
    - mv debian/changelog debian/changelog.old
    - >
      cat
      <(echo "davical ($(cat VERSION)-99~git$(date +"%Y%m%d")-$(git rev-parse --short $CI_COMMIT_SHA)) unstable; urgency=medium")
      <(echo "")
      <(echo "  * Build on CI")
      <(echo "")
      <(echo " -- ${GITLAB_USER_NAME} <${GITLAB_USER_EMAIL}>  $(date -R)")
      <(echo "")
      debian/changelog.old
      >debian/changelog
    - rm debian/changelog.old
    - debuild -us -uc -b -d
    - 'mv ../davical_*_all.deb ./davical.deb'
  artifacts:
    paths:
      - '*.deb'

test:
  stage: test
  image: debian:unstable
  artifacts:
    paths:
      - testing/report.xml
      - apache2_log/*
      - davical_log/*
    reports:
      junit: testing/report.xml
    when:
      always
  script:
    - apt-get -y update
    - bash -c 'mkdir -p /usr/share/man/man{0..10}'
    - apt-get -y install locales
    - echo "en_NZ.UTF-8 UTF-8" >> /etc/locale.gen
    - locale-gen
    - echo "LANG=en_NZ.UTF-8" > /etc/default/locale
    - apt-get -y install libdbd-pg-perl libyaml-perl php php-cli php-pgsql php-xml postgresql-client postgresql libapache2-mod-php curl xmlstarlet
    - curl 'https://gitlab.com/davical-project/awl/-/archive/master/awl-master.tar.gz' | tar zxf -
    - mv awl-master /usr/share/awl/
    - chown -R www-data /usr/share/awl/
    - dpkg --ignore-depends=libawl-php -i *.deb
    - echo '127.0.1.1  regression mycaldav myempty' >> /etc/hosts
    - rm /etc/apache2/ports.conf /etc/apache2/sites-enabled/000-default.conf && touch /etc/apache2/ports.conf
    - cp testing/apache-site.conf.example /etc/apache2/sites-enabled/davical-regression.conf
    - sed -i 's/\/path\/to/\/usr\/share/g' /etc/apache2/sites-enabled/davical-regression.conf
    - mkdir /usr/share/davical/testing/
    - cp testing/*.php /usr/share/davical/testing/
    - rm /etc/davical/config.php
    - cat testing/regression-conf.php.example | sed 's.//$c->dbg.$c->dbg.' > /etc/davical/regression-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/mycaldav-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/myempty-conf.php
    - mkdir -p /var/log/davical
    - chown www-data /var/log/davical
    - sed -i '/peer/d' /etc/postgresql/13/main/pg_hba.conf
    - echo 'local  all  all  trust' >> /etc/postgresql/13/main/pg_hba.conf
    - pg_ctlcluster 13 main start
    - su postgres -c 'createuser davical_dba --createdb --createrole --superuser'
    - su postgres -c 'createuser davical_app --superuser'
    - su postgres -c 'createuser testrunner --superuser'
    - pg_ctlcluster 13 main restart
    - a2enmod rewrite
    - apache2ctl start
    - useradd testrunner
    - cd testing && su testrunner -c 'IS_CI=yes ALLSUITES="regression-suite binding carddav scheduling" ./run_regressions.sh all x'
  after_script:
    - mkdir -p apache2_log
    - cp -r /var/log/apache2 apache2_log/test
    - bzip2 apache2_log/test/*
    - mkdir -p davical_log
    - cp -r /var/log/davical davical_log/test


test_bullseye_carddavclientinterop:
  stage: testall
  image: debian:bullseye
  artifacts:
    paths:
      - testing/report.xml
      - carddavclient-master/testreports/unit/results.html
      - apache2_log/*
      - davical_log/*
      - carddavclient-master/testreports/interop/*
    reports:
      junit:
        - testing/report.xml
        - carddavclient-master/testreports/unit/results.html
    when:
      always
  script:
    - apt-get -y update
    - bash -c 'mkdir -p /usr/share/man/man{0..10}'
    - apt-get -y install locales
    - echo "en_NZ.UTF-8 UTF-8" >> /etc/locale.gen
    - locale-gen
    - echo "LANG=en_NZ.UTF-8" > /etc/default/locale
    - apt-get -y install libdbd-pg-perl libyaml-perl php php-cli php-pgsql php-xml postgresql-client postgresql libapache2-mod-php curl xmlstarlet composer phpunit
    - curl 'https://gitlab.com/davical-project/awl/-/archive/master/awl-master.tar.gz' | tar zxf -
    - mv awl-master /usr/share/awl/
    - chown -R www-data /usr/share/awl/
    - dpkg --ignore-depends=libawl-php -i *.deb
    - echo '127.0.1.1  regression mycaldav myempty' >> /etc/hosts
    - rm /etc/apache2/ports.conf /etc/apache2/sites-enabled/000-default.conf && touch /etc/apache2/ports.conf
    - cp testing/apache-site.conf.example /etc/apache2/sites-enabled/davical-regression.conf
    - sed -i 's/\/path\/to/\/usr\/share/g' /etc/apache2/sites-enabled/davical-regression.conf
    - mkdir /usr/share/davical/testing/
    - cp testing/*.php /usr/share/davical/testing/
    - rm /etc/davical/config.php
    - cat testing/regression-conf.php.example | sed 's.//$c->dbg.$c->dbg.' > /etc/davical/regression-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/mycaldav-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/myempty-conf.php
    - mkdir -p /var/log/davical
    - chown www-data /var/log/davical
    - sed -i '/peer/d' /etc/postgresql/13/main/pg_hba.conf
    - echo 'local  all  all  trust' >> /etc/postgresql/13/main/pg_hba.conf
    - pg_ctlcluster 13 main start
    - su postgres -c 'createuser davical_dba --createdb --createrole --superuser'
    - su postgres -c 'createuser davical_app --superuser'
    - su postgres -c 'createuser testrunner --superuser'
    - pg_ctlcluster 13 main restart
    - a2enmod rewrite
    - apache2ctl start
    - useradd testrunner
    - cd testing && su testrunner -c 'IS_CI=yes ALLSUITES="regression-suite binding carddav scheduling" ./run_regressions.sh all x'
    - cd ..
    - curl https://codeload.github.com/mstilkerich/carddavclient/tar.gz/master | tar zxf -
    - cd carddavclient-master
    - composer install
    - echo '<?php declare(strict_types=1); namespace MStilkerich\Tests\CardDavClient\Interop; use MStilkerich\CardDavClient\{Account,AddressbookCollection,Config}; final class AccountData { public const ACCOUNTS = [ "Davical" => [ "username" => "user3", "password" => "user3", "discoveryUri" => "http://regression", "syncAllowExtraChanges" => false, "featureSet" => TestInfrastructureSrv::SRVFEATS_DAVICAL, ], ]; public const ADDRESSBOOKS = [ "Davical_0" => [ "account" => "Davical", "url" => "http://regression/caldav.php/user3/addresses/", "displayname" => "user3 addresses", ], ]; }' > tests/interop/AccountData.php
    - mkdir -p testreports/interop
    - phpunit -c tests/interop/phpunit.xml --no-coverage
  after_script:
    - mkdir -p apache2_log
    - cp -r /var/log/apache2 apache2_log/test_bullseye
    - bzip2 apache2_log/test_bullseye/*
    - mkdir -p davical_log
    - cp -r /var/log/davical davical_log/test_bullseye


build_buster_latestphp:
  stage: testall
  image: php:apache-buster
  script:
    - apt-get -y update
    - bash -c 'mkdir -p /usr/share/man/man{0..10}'
    - apt-get -y install build-essential devscripts fakeroot dh-exec jdupes doxygen graphviz
    - mv debian/changelog debian/changelog.old
    - >
      cat
      <(echo "davical ($(cat VERSION)-99~git$(date +"%Y%m%d")-$(git rev-parse --short $CI_COMMIT_SHA)) unstable; urgency=medium")
      <(echo "")
      <(echo "  * Build on CI")
      <(echo "")
      <(echo " -- ${GITLAB_USER_NAME} <${GITLAB_USER_EMAIL}>  $(date -R)")
      <(echo "")
      debian/changelog.old
      >debian/changelog
    - rm debian/changelog.old
    - debuild --prepend-path=/usr/local/bin -us -uc -b -d
    - 'mv ../davical_*_all.deb ./davical.deb'
  artifacts:
    paths:
      - '*.deb'

test_buster_latestphp:
  stage: testall
  image: php:apache-buster
  artifacts:
    paths:
      - testing/report.xml
      - apache2_log/*
      - davical_log/*
    reports:
      junit: testing/report.xml
    when:
      always
  script:
    - apt-get -y update
    - bash -c 'mkdir -p /usr/share/man/man{0..10}'
    - apt-get -y install locales
    - echo "en_NZ.UTF-8 UTF-8" >> /etc/locale.gen
    - locale-gen
    - echo "LANG=en_NZ.UTF-8" > /etc/default/locale
    - apt-get -y install libdbd-pg-perl libyaml-perl perl postgresql postgresql-client libpq-dev xmlstarlet
    - curl 'https://gitlab.com/davical-project/awl/-/archive/master/awl-master.tar.gz' | tar zxf -
    - mv awl-master /usr/share/awl/
    - chown -R www-data /usr/share/awl/
    - dpkg --ignore-depends=php,php-pgsql,php-xml,libawl-php,php-cli -i *.deb
    - docker-php-ext-install -j$(nproc) pgsql
    - docker-php-ext-install -j$(nproc) pdo_pgsql
    - docker-php-ext-install -j$(nproc) calendar
    - echo '127.0.1.1  regression mycaldav myempty' >> /etc/hosts
    - rm /etc/apache2/ports.conf /etc/apache2/sites-enabled/000-default.conf && touch /etc/apache2/ports.conf
    - cp testing/apache-site.conf.example /etc/apache2/sites-enabled/davical-regression.conf
    - sed -i 's/\/path\/to/\/usr\/share/g' /etc/apache2/sites-enabled/davical-regression.conf
    - mkdir /usr/share/davical/testing/
    - cp testing/*.php /usr/share/davical/testing/
    - rm /etc/davical/config.php
    - cat testing/regression-conf.php.example | sed 's.//$c->dbg.$c->dbg.' > /etc/davical/regression-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/mycaldav-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/myempty-conf.php
    - mkdir -p /var/log/davical
    - chown www-data /var/log/davical
    - sed -i '/peer/d' /etc/postgresql/11/main/pg_hba.conf
    - echo 'local  all  all  trust' >> /etc/postgresql/11/main/pg_hba.conf
    - pg_ctlcluster 11 main start
    - su postgres -c 'createuser davical_dba --createdb --createrole --superuser'
    - su postgres -c 'createuser davical_app --superuser'
    - su postgres -c 'createuser testrunner --superuser'
    - pg_ctlcluster 11 main restart
    - a2enmod rewrite
    - apache2ctl start
    - useradd testrunner
    - cd testing && su testrunner -c 'IS_CI=yes ALLSUITES="regression-suite binding carddav scheduling" ./run_regressions.sh all x'
  after_script:
    - mkdir -p apache2_log
    - cp -r /var/log/apache2 apache2_log/test_buster_latestphp
    - bzip2 apache2_log/test_buster_latestphp/*
    - mkdir -p davical_log
    - cp -r /var/log/davical davical_log/test_buster_latestphp


test_stretch_latestphp:
  stage: testall
  image: php:apache-stretch
  artifacts:
    paths:
      - testing/report.xml
      - apache2_log/*
      - davical_log/*
    reports:
      junit: testing/report.xml
    when:
      always
  script:
    - apt-get -y update
    - bash -c 'mkdir -p /usr/share/man/man{0..10}'
    - apt-get -y install locales
    - echo "en_NZ.UTF-8 UTF-8" >> /etc/locale.gen
    - locale-gen
    - echo "LANG=en_NZ.UTF-8" > /etc/default/locale
    - apt-get -y install libdbd-pg-perl libyaml-perl perl postgresql postgresql-client libpq-dev xmlstarlet
    - curl 'https://gitlab.com/davical-project/awl/-/archive/master/awl-master.tar.gz' | tar zxf -
    - mv awl-master /usr/share/awl/
    - chown -R www-data /usr/share/awl/
    - dpkg --ignore-depends=php,php-pgsql,php-xml,libawl-php,php-cli -i *.deb
    - docker-php-ext-install -j$(nproc) pgsql
    - docker-php-ext-install -j$(nproc) pdo_pgsql
    - docker-php-ext-install -j$(nproc) calendar
    - echo '127.0.1.1  regression mycaldav myempty' >> /etc/hosts
    - cp testing/apache-site.conf.example /etc/apache2/sites-enabled/davical-regression.conf
    - sed -i 's/\/path\/to/\/usr\/share/g' /etc/apache2/sites-enabled/davical-regression.conf
    - mkdir /usr/share/davical/testing/
    - cp testing/*.php /usr/share/davical/testing/
    - rm /etc/davical/config.php
    - cat testing/regression-conf.php.example | sed 's.//$c->dbg.$c->dbg.' > /etc/davical/regression-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/mycaldav-conf.php
    - ln -s /etc/davical/regression-conf.php /etc/davical/myempty-conf.php
    - mkdir -p /var/log/davical
    - chown www-data /var/log/davical
    - sed -i '/peer/d' /etc/postgresql/9.6/main/pg_hba.conf
    - echo 'local  all  all  trust' >> /etc/postgresql/9.6/main/pg_hba.conf
    - pg_ctlcluster 9.6 main start
    - su postgres -c 'createuser davical_dba --createdb --createrole --superuser'
    - su postgres -c 'createuser davical_app --superuser'
    - su postgres -c 'createuser testrunner --superuser'
    - pg_ctlcluster 9.6 main restart
    - a2enmod rewrite
    - apache2ctl start
    - useradd testrunner
    - cd testing && su testrunner -c 'IS_CI=yes ALLSUITES="regression-suite binding carddav scheduling" ./run_regressions.sh all x'
  after_script:
    - mkdir -p apache2_log
    - cp -r /var/log/apache2 apache2_log/test_stretch_latestphp
    - bzip2 apache2_log/test_stretch_latestphp/*
    - mkdir -p davical_log
    - cp -r /var/log/davical davical_log/test_stretch_latestphp
