services:
  - name: postgres:10.1
    command: ["-c", "datestyle=iso,dmy"]
  - httpd:latest

variables:
  POSTGRES_DB: "MaarchCourrier"
  POSTGRES_USER: maarch
  POSTGRES_PASSWORD: ""

stages:
  - test
  - deploy

before_script:
  - apt-get update -yqq > /dev/null
  - mkdir -p /usr/share/man/man1
  - mkdir -p /usr/share/man/man7
  #- apt-cache search chromium
  #- apt-get install apt-utils -yqq > /dev/null
  - apt-get install cron -yqq > /dev/null
  #- apt-get install tar -yqq > /dev/null
  #- apt-get install wkhtmltopdf -yqq > /dev/null
  #- apt-get install wget -yqq > /dev/null
  #- wget https://downloads.wkhtmltopdf.org/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
  #- tar xvf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
  #- mv wkhtmltox/bin/wkhtmlto* /usr/bin
  #- chmod +x /usr/bin/wkhtmlto*
  #- apt-get install libreoffice -yqq > /dev/null
  #- apt-get install npm -yqq > /dev/null 
  - apt-get install git -yqq > /dev/null
  - bash ci/docker_install_php.sh > /dev/null
  - bash ci/docker_install_database.sh > /dev/null

job_e2e:
 image: php:7.4-apache
 stage: test
 script:
   - service apache2 start
   - mkdir -p /opt/maarch/docservers/indexes/{letterbox_coll,attachments_coll,version_attachments_coll}
   - mkdir -p /opt/maarch/docservers/{ai,manual,manual_attachments,templates,acknowledgement_receipts}
   - mkdir -p /opt/maarch/docservers/{convert_attachments,convert_attachments_version,convert_mlb}
   - mkdir -p /opt/maarch/docservers/{fulltext_attachments,fulltext_attachments_version,fulltext_mlb}
   - mkdir -p /opt/maarch/docservers/{thumbnails_attachments,thumbnails_attachments_version,thumbnails_mlb}
   - mkdir -p /var/www/html/
   - ln -s $CI_PROJECT_DIR /var/www/html/MaarchCourrier
   - cd /var/www/html/MaarchCourrier
   - touch installed.lck
   - chown -R www-data. /opt/maarch/
   - chown -R www-data. /var/www/html/
   - chmod -R 775 /opt/maarch/
   - chmod -R 775 /var/www/html/
   - curl -sL https://deb.nodesource.com/setup_13.x | bash - > /dev/null
   - apt-get install -yqq nodejs > /dev/null
   - npm install npm@latest -g 
   - npm set registry https://registry.npmjs.org/ 
   - npm install
   - npm run build-prod
   - apt-get install -yqq openjdk-11-jdk > /dev/null
   - sed 's/<databaseserver>.*<\/databaseserver>/<databaseserver>postgres<\/databaseserver>/;s/<databasepassword>.*<\/databasepassword>/<databasepassword><\/databasepassword>/;s/<databasename>.*<\/databasename>/<databasename>MaarchCourrier<\/databasename>/;s/<databaseuser>.*<\/databaseuser>/<databaseuser>maarch<\/databaseuser>/' apps/maarch_entreprise/xml/config.xml.default > apps/maarch_entreprise/xml/config.xml
   - curl -o /dev/null --silent --head --write-out '%{http_code}\n' http://127.0.0.1/MaarchCourrier
   - firefox --version
   - npm run ee-launch
 artifacts:
   paths:
     - queries_error.log
     - test/e2e/screenshots/
   expire_in: 2 days
   when: always
 allow_failure: true
#  only:
#    - schedules

job_php-7.4:
 image: php:7.4-apache
 stage: test
 script:
   - mkdir -p /opt/maarch/docservers/indexes/{letterbox_coll,attachments_coll,version_attachments_coll}
   - mkdir -p /opt/maarch/docservers/{ai,manual,manual_attachments,templates,acknowledgement_receipts}
   - mkdir -p /opt/maarch/docservers/{convert_attachments,convert_attachments_version,convert_mlb}
   - mkdir -p /opt/maarch/docservers/{fulltext_attachments,fulltext_attachments_version,fulltext_mlb}
   - mkdir -p /opt/maarch/docservers/{thumbnails_attachments,thumbnails_attachments_version,thumbnails_mlb}
   - mkdir -p /var/www/html/
   - ln -s $CI_PROJECT_DIR /var/www/html/MaarchCourrier
   - cd /var/www/html/MaarchCourrier
   - sed 's/<databaseserver>.*<\/databaseserver>/<databaseserver>postgres<\/databaseserver>/;s/<databasepassword>.*<\/databasepassword>/<databasepassword><\/databasepassword>/;s/<databasename>.*<\/databasename>/<databasename>MaarchCourrier<\/databasename>/;s/<databaseuser>.*<\/databaseuser>/<databaseuser>maarch<\/databaseuser>/' apps/maarch_entreprise/xml/config.xml.default > apps/maarch_entreprise/xml/config.xml
   - cp modules/visa/xml/remoteSignatoryBooks.xml.default modules/visa/xml/remoteSignatoryBooks.xml
   - curl --location -s --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar
   - chmod +x /usr/local/bin/phpunit
   - phpunit --coverage-text --colors=never -c phpunit.xml
 only:
   - develop
 except:
   - schedules
 artifacts:
   paths:
     - test/unitTests/build/
   expire_in: 2h

job_php-7.3:
 image: php:7.3-apache
 stage: test
 script:
   - mkdir -p /opt/maarch/docservers/indexes/{letterbox_coll,attachments_coll,version_attachments_coll}
   - mkdir -p /opt/maarch/docservers/{ai,manual,manual_attachments,templates,acknowledgement_receipts}
   - mkdir -p /opt/maarch/docservers/{convert_attachments,convert_attachments_version,convert_mlb}
   - mkdir -p /opt/maarch/docservers/{fulltext_attachments,fulltext_attachments_version,fulltext_mlb}
   - mkdir -p /opt/maarch/docservers/{thumbnails_attachments,thumbnails_attachments_version,thumbnails_mlb}
   - mkdir -p /var/www/html/
   - ln -s $CI_PROJECT_DIR /var/www/html/MaarchCourrier
   - cd /var/www/html/MaarchCourrier
   - sed 's/<databaseserver>.*<\/databaseserver>/<databaseserver>postgres<\/databaseserver>/;s/<databasepassword>.*<\/databasepassword>/<databasepassword><\/databasepassword>/;s/<databasename>.*<\/databasename>/<databasename>MaarchCourrier<\/databasename>/;s/<databaseuser>.*<\/databaseuser>/<databaseuser>maarch<\/databaseuser>/' apps/maarch_entreprise/xml/config.xml.default > apps/maarch_entreprise/xml/config.xml
   - cp modules/visa/xml/remoteSignatoryBooks.xml.default modules/visa/xml/remoteSignatoryBooks.xml
   - curl --location -s --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar
   - chmod +x /usr/local/bin/phpunit
   - phpunit --coverage-text --colors=never -c phpunit.xml
 only:
   - develop
 except:
   - schedules

job_php-7.2:
  image: php:7.2-apache
  stage: test
  script:
    - mkdir -p /opt/maarch/docservers/indexes/{letterbox_coll,attachments_coll,version_attachments_coll}
    - mkdir -p /opt/maarch/docservers/{ai,manual,manual_attachments,templates,acknowledgement_receipts}
    - mkdir -p /opt/maarch/docservers/{convert_attachments,convert_attachments_version,convert_mlb}
    - mkdir -p /opt/maarch/docservers/{fulltext_attachments,fulltext_attachments_version,fulltext_mlb}
    - mkdir -p /opt/maarch/docservers/{thumbnails_attachments,thumbnails_attachments_version,thumbnails_mlb}
    - mkdir -p /var/www/html/
    - ln -s $CI_PROJECT_DIR /var/www/html/MaarchCourrier
    - cd /var/www/html/MaarchCourrier
    - sed 's/<databaseserver>.*<\/databaseserver>/<databaseserver>postgres<\/databaseserver>/;s/<databasepassword>.*<\/databasepassword>/<databasepassword><\/databasepassword>/;s/<databasename>.*<\/databasename>/<databasename>MaarchCourrier<\/databasename>/;s/<databaseuser>.*<\/databaseuser>/<databaseuser>maarch<\/databaseuser>/' apps/maarch_entreprise/xml/config.xml.default > apps/maarch_entreprise/xml/config.xml
    - cp modules/visa/xml/remoteSignatoryBooks.xml.default modules/visa/xml/remoteSignatoryBooks.xml
    - curl --location -s --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-8.phar
    - chmod +x /usr/local/bin/phpunit
    - phpunit --coverage-text --colors=never -c phpunit.xml
  only:
    - develop
  except:
    - schedules

pages:
  stage: deploy
  dependencies:
    - job_php-7.4
  script:
    - mv test/unitTests/build/ public/
  artifacts:
    paths:
      - public
    expire_in: 1 day
  only:
    - develop
