diff --git a/plex/rootfs/etc/cont-init.d/90-create_links.sh b/plex/rootfs/etc/cont-init.d/90-create_links.sh index a172ba153..2549c1ef9 100755 --- a/plex/rootfs/etc/cont-init.d/90-create_links.sh +++ b/plex/rootfs/etc/cont-init.d/90-create_links.sh @@ -1,25 +1,66 @@ -#!/bin/bash +#!/usr/bin/env bashio + +set -Eeuo pipefail ################## # SYMLINK CONFIG # ################## +# Helper: Fix ownership only if needed (on folder, not all files) +fix_owner_if_needed() { + local path="$1" + local want_uid="$2" + local want_gid="$3" + local curr_uid curr_gid + + if [ -e "$path" ]; then + curr_uid=$(stat -c '%u' "$path") + curr_gid=$(stat -c '%g' "$path") + if [ "$curr_uid" -ne "$want_uid" ] || [ "$curr_gid" -ne "$want_gid" ]; then + echo "Fixing ownership: $path ($curr_uid:$curr_gid -> $want_uid:$want_gid)" + chown -R "$want_uid:$want_gid" "$path" + fi + fi +} + +# Helper: Fix mode only if needed (on folder, not all files) +fix_mode_if_needed() { + local path="$1" + local want_mode="$2" + local curr_mode + + if [ -e "$path" ]; then + curr_mode=$(stat -c '%a' "$path") + if [ "$curr_mode" -ne "$want_mode" ]; then + echo "Fixing mode: $path ($curr_mode -> $want_mode)" + chmod -R "$want_mode" "$path" + fi + fi +} + +# Ensure /share/plex exists if [ ! -d /share/plex ]; then echo "Creating /share/plex" mkdir -p /share/plex fi +# Library folder move/link +mkdir -p /config/Library if [ ! -d /share/plex/Library ]; then - echo "moving Library folder" + echo "Moving Library folder" mv /config/Library /share/plex ln -s /share/plex/Library /config - echo "links done" + echo "Links done" else - rm -r /config/Library + rm -rf /config/Library ln -s /share/plex/Library /config echo "Using existing config" fi -chown -R "$PUID:$PGID" /config/Library -chown -R "$PUID:$PGID" /share/plex -chmod -R 777 /share/plex +# Only fix ownership/mode if needed (top-level only—*not* blindly every file) +PUID="$(bashio::config "PUID")" +PGID="$(bashio::config "PGID")" + +fix_owner_if_needed "/share/plex" "$PUID" "$PGID" +fix_owner_if_needed "/share/plex/Library" "$PUID" "$PGID" +fix_mode_if_needed "/share/plex" 777