#!/bin/nu

touch /var/log/nginx/access.log
touch /var/log/nginx/error.log
touch /var/log/cron/owncloud.log

mkdir /config
echo "# Options in here overwrite the builtin php.ini\n" | save /config/php.ini
echo "# xdebug.mode = debug\n" | save -a /config/php.ini
echo "# xdebug.start_with_request = yes\n\n" | save -a /config/php.ini
chmod 0777 /config/php.ini
let PHP_INI_DIR = php --ini | grep 'Scan' | cut -d ' ' -f7 | tr -d '"'
ln -s /config/php.ini $"($PHP_INI_DIR)/zz_extra.ini"

let HAZE_UID = $env.HAZE_UID | default "1000"
let HAZE_GID = $env.HAZE_GID | default "1000"

nc-auto-config
shadow-setup

echo $"Running as ($HAZE_UID):($HAZE_GID)"

mkdir /var/www/html/core/skeleton /var/www/html/build/integration/vendor /var/www/html/build/integration/output /var/www/html/build/integration/work /var/www/html/core/skeleton /var/www/.composer/cache /var/www/html/apps/spreed/tests/integration/vendor/composer
chown -R $"($HAZE_UID):($HAZE_GID)" /var/www/html/data /var/www/html/config
chown $"($HAZE_UID):($HAZE_GID)" /var/www/html/core/skeleton /var/www/html/build/integration/vendor /var/www/html/build/integration/composer.lock /var/www/html/build/integration/output /var/www/html/build/integration/work /var/www/html/core/skeleton /var/www/.composer/cache /var/www/html/apps/spreed/tests/integration/vendor/composer

echo "{}\n" | save -f /var/www/html/build/integration/composer.lock

echo $"Starting server using ($env.SQL) database…"

chmod +sx /sbin/sudo

mkdir /var/log/nginx /tmp /var/run/blackfire
touch /var/log/nginx/access.log
touch /var/log/nginx/error.log

if ((getent group $HAZE_GID | length) > 0) {
  groupadd haze
  useradd -u $HAZE_UID -g $HAZE_GID -G haze haze
} else {
  groupadd -g $HAZE_GID haze
  useradd -u $HAZE_UID -g $HAZE_GID haze
}
chown -R $"haze:($HAZE_GID)" /home/haze

if ("/var/run/docker.sock" | path exists) {
  let dockerGid = stat --format "%g" /var/run/docker.sock
  groupadd docker -g $dockerGid
  usermod -a -G docker haze
}

if ("REDIS_TLS" in $env) {
  cp /etc/supervisor/redis-tls.conf /etc/supervisor/enabled/
} else {
  cp /etc/supervisor/redis-plain.conf /etc/supervisor/enabled/
}

if ("BLACKFIRE_SERVER_ID" in $env) {
  blackfire agent:config --server-id $env.BLACKFIRE_SERVER_ID --server-token $env.BLACKFIRE_SERVER_TOKEN
  cp /etc/supervisor/blackfire.conf /etc/supervisor/enabled/
}

if ("PROXY_BASE" in $env) {
  let UPSTREAM_DNS = cat /etc/resolv.conf | grep nameserver | cut -d' ' -f 2
  let RC = sed '/nameserver/d' /etc/resolv.conf
  echo $RC | save -f /etc/resolv.conf

  echo "\nnameserver 127.0.0.22\n" | save -a /etc/resolv.conf

  echo $"s/UPSTREAM_DNS/($UPSTREAM_DNS)"
  sed -i $"s/UPSTREAM_DNS/($UPSTREAM_DNS)/" /etc/dnsmasq.conf
  echo $"s/PROXY_BASE/($env.PROXY_BASE)"
  sed -i $"s/PROXY_BASE/($env.PROXY_BASE)/" /etc/dnsmasq.conf
  echo $"s/HOST_IP/($env.HOST_IP)"
  sed -i $"s/HOST_IP/($env.HOST_IP)/" /etc/dnsmasq.conf

  cp /etc/supervisor/dnsmasq.conf /etc/supervisor/enabled/
}

if ("FRANKENPHP" in $env) {
  cp /etc/supervisor/frankenphp.conf /etc/supervisor/enabled/
} else {
  cp /etc/supervisor/php-fpm.conf /etc/supervisor/enabled/
  cp /etc/supervisor/nginx.conf /etc/supervisor/enabled/
}

exec supervisord -c /etc/supervisor/supervisord.conf
