#!/usr/bin/with-contenv bash

# make our folders
mkdir -p \
	/config/www/{backgrounds,icons,avatars,SupportedApps}

# create symlinks

symlinks=( \
/var/www/localhost/heimdall/storage/app/public/avatars \
/var/www/localhost/heimdall/storage/app/public/backgrounds \
/var/www/localhost/heimdall/storage/app/public/icons \
/var/www/localhost/heimdall/app/SupportedApps \
/var/www/localhost/heimdall/database/app.sqlite \
/var/www/localhost/heimdall/.env )

for i in "${symlinks[@]}"
do
[[ -e "$i" && ! -L "$i" ]] && rm -rf "$i"
[[ ! -L "$i" ]] && ln -s /config/www/"$(basename "$i")" "$i"
done

# copy .env if not exists
[[ ! -f /config/www/.env ]] && \
  cp /var/www/localhost/heimdall/.env.example /config/www/.env && \
  php /var/www/localhost/heimdall/artisan key:generate
# set queue driver to database
sed -i 's/QUEUE_DRIVER=sync/QUEUE_DRIVER=database/' /config/www/.env

# permissions
chown -R abc:abc \
	/config \
	/var/www/localhost/heimdall
