#!/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' )" "${@}"
}

# Add temporary directories used for artwork and potentially other uploads.
# APP_HOME is provided by the Dockerfile.
TEMPORARY_DIRECTORIES=('public/apps' 'public/assets' 'public/assets/artist' 'public/assets/release' 'public/filmtv' 'public/oa/images/users' 'public/tmp' 'public/tmp/upload/delivery')

for DIR in "${TEMPORARY_DIRECTORIES[@]}"; do
  if [ ! -d "${APP_HOME}/${DIR}" ]; then
    mkdir -p "${APP_HOME}/${DIR}"
    chown www-data "${APP_HOME}/${DIR}"
  fi
done

# Copy htaccess file into correct directory
cp -a "${APP_HOME}/deploy/.htaccess" "${APP_HOME}/public/.htaccess"

/usr/sbin/apache2ctl -k start -D FOREGROUND
