#!/usr/bin/env bash
set -euo pipefail

# Etherpad's upstream Docker image runs as uid 5001, gid 0.
# Migrate only the Etherpad app container's bind-mounted writable dirs
# from the previous Umbrel uid 1000 ownership. Do not touch Postgres data.
DESIRED_OWNER="5001:0"

PLUGINS_DATA_DIR="${APP_DATA_DIR}/data/plugins"
ETHERPAD_VAR_DATA_DIR="${APP_DATA_DIR}/data/etherpad-var"

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

  mkdir -p "${path}"

  owner="$(stat -c "%u:%g" "${path}")"
  if [[ "${owner}" != "${DESIRED_OWNER}" ]]; then
    chown -R "${DESIRED_OWNER}" "${path}"
  fi
}

set_correct_permissions "${PLUGINS_DATA_DIR}"
set_correct_permissions "${ETHERPAD_VAR_DATA_DIR}"
