#!/bin/bash
#
#	fix perms on NextCloud install dir
#
# ::do_not_edit::
# ::maintainer::
#
set -e -u

# get needed vars from apache2 environment
#
envfile="/etc/apache2/envvars"

var=$(grep "APACHE_RUN_USER=" "$envfile" | sed -e 's/.*=//' -e "s/['\"]//")
[ "X$var" = "X" ] && { echo "ERROR: cannot get APACHE_RUN_USER from '$envfile'" >&2; exit 1; }
APACHE_RUN_USER=$var

var=$(grep "APACHE_RUN_GROUP=" "$envfile" | sed -e 's/.*=//' -e "s/['\"]//")
[ "X$var" = "X" ] && { echo "ERROR: cannot get APACHE_RUN_GROUP from '$envfile'" >&2; exit 1; }
APACHE_RUN_GROUP=$var

cd "::srv-nextcloud.instdir::"

echo -n "  NextCloud: fixing perms on '$(pwd)' ($APACHE_RUN_USER:$APACHE_RUN_GROUP) ... "

echo -n "files ... "
chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP .
chmod -R g+rw .

echo -n "dirs ... "
find . -type d -exec chmod 2775 {} \;

echo "ok"
exit 0
