mirror of
https://github.com/RaunoT/plex-rewind.git
synced 2026-11-30 04:12:01 +01:00
64 lines
1.7 KiB
Bash
Executable File
64 lines
1.7 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
# Already running as a non-root user (e.g. `--user 1000:1000`), nothing to fix
|
|
if [ "$(id -u)" != "0" ]; then
|
|
exec "$@"
|
|
fi
|
|
|
|
PUID="${PUID:-1000}"
|
|
PGID="${PGID:-1000}"
|
|
|
|
case "$PUID$PGID" in
|
|
*[!0-9]*)
|
|
echo "PUID and PGID must be numeric, got PUID=$PUID PGID=$PGID" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
# PUID=0 keeps running as root, as older versions did
|
|
if [ "$PUID" = "0" ]; then
|
|
exec "$@"
|
|
fi
|
|
|
|
# Reuse the user and group when the id already exists (the node user is 1000:1000)
|
|
group="$(getent group "$PGID" | cut -d: -f1)"
|
|
|
|
if [ -z "$group" ]; then
|
|
group="rewind"
|
|
addgroup -g "$PGID" "$group"
|
|
fi
|
|
|
|
if [ -z "$(getent passwd "$PUID")" ]; then
|
|
adduser -D -H -u "$PUID" -G "$group" -s /sbin/nologin rewind
|
|
fi
|
|
|
|
# Existing installs have a root-owned config directory; skip recursive chown
|
|
# when ownership already matches so large config volumes start fast
|
|
fix_ownership() {
|
|
dir="$1"
|
|
mismatch="$(find "$dir" \( ! -user "$PUID" -o ! -group "$PGID" \) -print -quit 2>/dev/null || true)"
|
|
|
|
if [ -n "$mismatch" ]; then
|
|
chown -R "$PUID:$PGID" "$dir" 2>/dev/null || true
|
|
fi
|
|
|
|
if ! su-exec "$PUID:$PGID" test -w "$dir" 2>/dev/null ||
|
|
! su-exec "$PUID:$PGID" test -x "$dir" 2>/dev/null ||
|
|
{ [ -e "$dir/settings.json" ] && ! su-exec "$PUID:$PGID" test -w "$dir/settings.json" 2>/dev/null; }; then
|
|
echo "$dir is not writable by $PUID:$PGID, the app may not be able to save data" >&2
|
|
fi
|
|
|
|
if [ -e "$dir/auth-secret" ] && ! su-exec "$PUID:$PGID" test -r "$dir/auth-secret" 2>/dev/null; then
|
|
echo "$dir/auth-secret is not readable by $PUID:$PGID, sessions may reset on restart" >&2
|
|
fi
|
|
}
|
|
|
|
fix_ownership /app/config
|
|
fix_ownership /app/.next/cache
|
|
|
|
HOME="$(getent passwd "$PUID" | cut -d: -f6)"
|
|
export HOME
|
|
|
|
exec su-exec "$PUID:$PGID" "$@"
|