# Убедитесь, что каталоги безбумажного потребления и экспорта созданы с правильными разрешениями.
DFWEBOS_DATA_DIR="${DFWEBOS_ROOT}/data"
DFWEBOS_DATA_STORAGE_DIR="${DFWEBOS_DATA_DIR}/storage"
DFWEBOS_DATA_STORAGE_PAPERLESS_DIR="${DFWEBOS_DATA_STORAGE_DIR}/paperless"
DFWEBOS_DATA_STORAGE_PAPERLESS_CONSUME_DIR="${DFWEBOS_DATA_STORAGE_PAPERLESS_DIR}/consume"
DFWEBOS_DATA_STORAGE_PAPERLESS_EXPORT_DIR="${DFWEBOS_DATA_STORAGE_PAPERLESS_DIR}/export"
DESIRED_OWNER="1000:1000"

if [[ ! -d "${DFWEBOS_DATA_STORAGE_PAPERLESS_CONSUME_DIR}" ]]; then
  mkdir -p "${DFWEBOS_DATA_STORAGE_PAPERLESS_CONSUME_DIR}"
fi

if [[ ! -d "${DFWEBOS_DATA_STORAGE_PAPERLESS_EXPORT_DIR}" ]]; then
  mkdir -p "${DFWEBOS_DATA_STORAGE_PAPERLESS_EXPORT_DIR}"
fi

set_paperless_correct_permissions() {
	local -r path="${1}"

	if [[ -d "${path}" ]]; then
		owner=$(stat -c "%u:%g" "${path}")

		if [[ "${owner}" != "${DESIRED_OWNER}" ]]; then
			chown "${DESIRED_OWNER}" "${path}"
		fi
	fi
}

set_paperless_correct_permissions "${DFWEBOS_DATA_STORAGE_DIR}"
set_paperless_correct_permissions "${DFWEBOS_DATA_STORAGE_PAPERLESS_DIR}"
set_paperless_correct_permissions "${DFWEBOS_DATA_STORAGE_PAPERLESS_CONSUME_DIR}"
set_paperless_correct_permissions "${DFWEBOS_DATA_STORAGE_PAPERLESS_EXPORT_DIR}"
