From 870011d3ddf1c8764ec728aef6057e55b4f15f83 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Wed, 30 Jul 2025 06:37:32 +0200 Subject: [PATCH] Refactor --- .../rootfs/etc/cont-init.d/90-create_links.sh | 66 ++++++++++--------- 1 file changed, 36 insertions(+), 30 deletions(-) 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 93a1bc234..a2199d036 100755 --- a/plex/rootfs/etc/cont-init.d/90-create_links.sh +++ b/plex/rootfs/etc/cont-init.d/90-create_links.sh @@ -1,36 +1,42 @@ -#!/usr/bin/env bashio +#!/usr/bin/with-contenv bashio +set -euo pipefail -################## -# SYMLINK CONFIG # -################## +# --- Config --- +PUID="$(bashio::config 'PUID' || echo 0)" +PGID="$(bashio::config 'PGID' || echo 0)" -if [ ! -d /share/plex ]; then - echo "Creating /share/plex" - mkdir -p /share/plex +bashio::log.info "Ensuring Plex library location and symlink ..." + +# 1) Ensure base dir exists +install -d -m 0775 /share/plex + +# 2) If a real /config/Library exists and /share/plex/Library doesn't, migrate it once +if [ -d /config/Library ] && [ ! -L /config/Library ] && [ ! -e /share/plex/Library ]; then + bashio::log.info "Migrating /config/Library to /share/plex/Library" + mv /config/Library /share/plex/ # results in /share/plex/Library fi -if [ ! -d /share/plex/Library ] && [ -d /config/Library ]; then - echo "moving Library folder" - mv /config/Library /share/plex -else - rm -r /config/Library - echo "Using existing config" -fi -ln -sf /share/plex/Library /config +# 3) Ensure target exists (in case there was nothing to migrate) +install -d -m 0775 /share/plex/Library -# Only fix ownership/mode if needed (top-level only—*not* blindly every file) -PUID="$(bashio::config "PUID")" -PGID="$(bashio::config "PGID")" -PUID="${PUID:-0}" -PGID="${PGID:-0}" - -# Only run fixes if not root (UID/GID != 0) -if [ "$PUID" != "0" ] && [ "$PGID" != "0" ]; then - WANTED_MODE="777" - CURRENT_MODE=$(stat -c '%a' /share/plex) - if [ "$CURRENT_MODE" != "$WANTED_MODE" ]; then - bashio::log.warning "Providing adequate permissions, please wait" - chown -R "$PUID:$PGID" /share/plex - chmod -R 777 /share/plex - fi +# 4) Ensure symlink /config/Library -> /share/plex/Library +if [ ! -L /config/Library ] || [ "$(readlink -f /config/Library || true)" != "/share/plex/Library" ]; then + # If a leftover dir/file exists at /config/Library, back it up instead of deleting + if [ -e /config/Library ] && [ ! -L /config/Library ]; then + TS="$(date +%s)" + bashio::log.warning "Found non-symlink at /config/Library; backing up to /config/Library.bak-${TS}" + mv /config/Library "/config/Library.bak-${TS}" + else + rm -f /config/Library || true + fi + ln -sfn /share/plex/Library /config/Library fi + +# 5) Ownership & top-level perms (independent checks; avoid recursive 777) +if [ "$PUID" != "0" ] || [ "$PGID" != "0" ]; then + chown "$PUID:$PGID" /share/plex /share/plex/Library + # 2775 keeps group for new items created inside + chmod 2775 /share/plex /share/plex/Library +fi + +bashio::log.info "Plex library directory and symlink are ready."