#!/usr/bin/with-contenv bash

# create folders
mkdir -p \
  /config/www/{backgrounds,icons}

# set base url if needed
rm -rf /app/root
if [ -z "$BASEURL" ]; then
  echo "No base url set. You can access the app at http://SERVERIP:PORT"
  ln -s ./heimdall/public /app/root
else
  echo "Setting base url. You can access the app at http://SERVERIP:PORT/$BASEURL"
  mkdir -p /app/root
  ln -s ../heimdall/public /app/root/"$BASEURL"
fi

# symlink user config

symlinks=( \
/app/heimdall/storage/app/public/backgrounds \
/app/heimdall/storage/app/public/icons \
/app/heimdall/database/app.sqlite )

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

# permissions
chown -R abc:abc \
	/config \
	/app
