#!/bin/bash
set -e

: "${WEBROOT:?WEBROOT is not set}"
: "${USER_REPO:?USER_REPO is not set}"

echo "==> Installing PHP extensions"
apt-get install -y php-curl php-gd php-mbstring php-xml php-zip php-yaml php-intl

echo "==> Downloading Grav"
cd "$WEBROOT"
wget -q https://getgrav.org/download/core/grav-admin/latest -O grav-admin.zip
unzip -q grav-admin.zip
mv grav-admin/* grav-admin/.htaccess .
rm -rf grav-admin grav-admin.zip

echo "==> Cloning user repo"
rm -rf user
git clone "$USER_REPO" user

echo "==> Setting permissions"
find "$WEBROOT" -type f -exec chmod 664 {} \;
find "$WEBROOT" -type d -exec chmod 775 {} \;

echo "==> Done. Visit your domain to complete setup."
