#!/bin/bash

# enable strict mode
set -euo pipefail
IFS=$'\n\t'

# Log function
function log {
  printf '[%s] [%s] %s\n' "${0##*/}" "$( date '+%Y-%m-%dT%H:%M:%S' )" "${@}"
}

SHARED_MOUNT_DIRECTORIES=('public/apps' 'public/assets' 'public/filmtv' 'public/oa/images/users' 'public/tmp' 'public/asset_downloads')
ADDITIONAL_SUBDIRECTORIES=('public/assets/artist' 'public/assets/release' 'public/tmp/upload/delivery')
# APP_HOME is provided by the Dockerfile and EFS_VOLUME_CONTAINER_PATH is provided by the Docker runtime (e.g. Fargate)

# Add temporary directories used for artwork and potentially other uploads.
# These should be backed by shared storage across all workstation hosts, so test for EFS_VOLUME_CONTAINER_PATH
for DIR in "${SHARED_MOUNT_DIRECTORIES[@]}"; do
  if [ ! -d "${APP_HOME}/${DIR}" ]; then
    if [ -d "${EFS_VOLUME_CONTAINER_PATH}" ]; then
      # Each shared directory should be individually stubbed out in the shared volume and owned by apache
      if [ ! -d "${EFS_VOLUME_CONTAINER_PATH}/${DIR}" ]; then
        printf '%s\n' "Creating ${EFS_VOLUME_CONTAINER_PATH}/${DIR} as www-data user"
        su -s /bin/bash www-data -c "mkdir -p ${EFS_VOLUME_CONTAINER_PATH}/${DIR}"
      fi
      ln -s "${EFS_VOLUME_CONTAINER_PATH}/${DIR}" "${APP_HOME}/${DIR}"
      printf '%s\n' "Linked ${APP_HOME}/${DIR} to ${EFS_VOLUME_CONTAINER_PATH}/${DIR}"
    else
      mkdir -p "${APP_HOME}/${DIR}"
      chown www-data "${APP_HOME}/${DIR}"
    fi
  fi
done

# Irrespective of shared mounts, stub out some additional required directories.
# If the parent directories are symlinked to shared mounts, it will be created there; if not, locally is fine.
for DIR in "${ADDITIONAL_SUBDIRECTORIES[@]}"; do
  if [ ! -d "${APP_HOME}/${DIR}" ]; then
    if [ -d "${EFS_VOLUME_CONTAINER_PATH}" ]; then
      su -s /bin/bash www-data -c "mkdir -p ${APP_HOME}/${DIR}"
    else
      mkdir -p "${APP_HOME}/${DIR}"
      chown www-data "${APP_HOME}/${DIR}"
    fi
  fi
done

# Retrieve configs if in QA or Prod
if [ "${Environment}" = 'qa' ] || [ "${Environment}" = 'prod' ]; then

  AWS_COMMAND='/usr/local/bin/aws'

  if [ -x "${AWS_COMMAND}" ]; then
    ${AWS_COMMAND} s3 cp --recursive s3://"${S3_CONFIG_LOCATION}"/configs/ "${APP_HOME}"/application/configs/
    ${AWS_COMMAND} s3 cp --recursive s3://"${S3_CONFIG_LOCATION}"/public/ "${APP_HOME}"/public/
    find "${APP_HOME}/application/configs/" -type f -exec chmod 640 {} \;
    find "${APP_HOME}/public/" -type f -name "*.inc" -exec chmod 640 {} \;
    chgrp -R www-data "${APP_HOME}/application/configs/" "${APP_HOME}/public/"
  else
    printf '%s\n' "awscli not found" && exit 1
  fi

  # Copy correct haproxy config file into place
  cp -a "${APP_HOME}/deploy/haproxy_${Environment}.cfg" /etc/haproxy/haproxy.cfg

else
  cp -a "${APP_HOME}/deploy/haproxy_dev.cfg" /etc/haproxy/haproxy.cfg
fi

# Don't leave system config files in dangerous places
rm -rf "${APP_HOME}/deploy"

# Replace DNS servers with primary nameserver at runtime
CURRENT_NAMESERVER=$(cat /etc/resolv.conf |grep -m 1 -i '^nameserver' | cut -d ' ' -f2)
sed -i "s/DNS_PLACEHOLDER/${CURRENT_NAMESERVER}/g" /etc/haproxy/haproxy.cfg

log 'Starting disk space script'
/usr/local/bin/disk_space_check.sh 2>&1 &

haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -D && /usr/sbin/apache2ctl -k start -D FOREGROUND
