#!/bin/sh set -eu : "${SWITCH_DATA_ROOT:?set the isolated app-owned Switch data root}" : "${SWITCH_LIBRARY:?set the read-only Switch source library}" : "${PUID:?set the non-root Eden UID}" : "${PGID:?set the non-root Eden GID}" case "$PUID:$PGID" in *[!0-9:]*|:*|*:) echo "PUID and PGID must be numeric." >&2; exit 1 ;; esac if [ "$PUID" -eq 0 ] || [ "$PGID" -eq 0 ]; then echo "Eden must use a non-root PUID and PGID." >&2 exit 1 fi case "$SWITCH_DATA_ROOT" in /*) ;; *) echo "SWITCH_DATA_ROOT must be absolute." >&2; exit 1 ;; esac case "$SWITCH_LIBRARY" in /*) ;; *) echo "SWITCH_LIBRARY must be absolute." >&2; exit 1 ;; esac data_parent=$(dirname -- "$SWITCH_DATA_ROOT") data_name=$(basename -- "$SWITCH_DATA_ROOT") if [ ! -d "$data_parent" ]; then echo "The SWITCH_DATA_ROOT parent must already exist: $data_parent" >&2 exit 1 fi data_parent=$(readlink -f -- "$data_parent") data_root=$(readlink -m -- "$data_parent/$data_name") library_root=$(readlink -f -- "$SWITCH_LIBRARY") case "$data_root" in /|/mnt|/mnt/user|/mnt/user/appdata) echo "Refusing unsafe SWITCH_DATA_ROOT: $data_root" >&2 exit 1 ;; esac case "$data_root/" in "$library_root"/*) echo "SWITCH_DATA_ROOT must not be inside the source library." >&2 exit 1 ;; esac case "$library_root/" in "$data_root"/*) echo "SWITCH_LIBRARY must not be inside app-owned data." >&2 exit 1 ;; esac mkdir -p -- "$data_root/config" data_root=$(readlink -f -- "$data_root") # Eden must be able to read and update its own persistent config after a PUID/PGID change. # This path is app-owned; the source library is intentionally never touched. chown -R "$PUID:$PGID" -- "$data_root/config" chmod 750 -- "$data_root/config" find "$data_root/config" -type f -name qt-config.ini -exec chmod 600 {} + printf 'Prepared %s for Eden identity %s:%s; source library untouched: %s\n' \ "$data_root/config" "$PUID" "$PGID" "$library_root"