#!/usr/bin/env bash

set -e

export FPM_USER_ID=${FPM_USER_ID:-$(id -u)}
export FPM_GROUP_ID=${FPM_GROUP_ID:-$(id -g)}

CMD=$1

if [ "$CMD" == "" ]; then
    CMD="up -d"
fi

JELIX_FILE=jelix-1.6.30-dev

if [ ! -d lib/ ]; then
  echo download and install $JELIX_FILE
  mkdir -p temp
  (cd temp
  if [ ! -f $JELIX_FILE.zip ]; then
    curl https://github.com/jelix/jelix/releases/download/RELEASE_JELIX_1_6_30/$JELIX_FILE.zip -L -O
  fi
  unzip -qo $JELIX_FILE.zip
  mv $JELIX_FILE/lib ../
  )
fi

if [ ! -f docker-conf/certs/tests.jelix-CA.crt ]; then
  echo "generate CA certs"
  (
  cd docker-conf
  ./ssl.sh createCA tests.jelix
  )
fi


if [ ! -f docker-conf/certs/ldap.jelix.crt ]; then
  echo "generate certificates for the ldap server"
  (
  cd docker-conf
  ./ssl.sh createCert ldap.jelix tests.jelix
  )
fi

if [ "$TESTAPP_WEB_PORT" == "" ]; then
  export TESTAPP_WEB_PORT=8817
fi

if [ "$PHP_VERSION" == "" ]; then
  export PHP_VERSION=7.4
fi

docker-compose -p jelix-ldapdao-tests $CMD
