mirror of
https://gh.con.sh/https://github.com/plexinc/pms-docker
synced 2024-09-20 14:55:46 +00:00
153 lines
4.5 KiB
Bash
Executable File
153 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
IFS=$'\n\t'
|
|
|
|
# set defaults
|
|
DEBUG=${DEBUG:-}
|
|
PLEX_UID=${PLEX_UID:-}
|
|
PLEX_GID=${PLEX_GID:-}
|
|
CHANGE_CONFIG_DIR_OWNERSHIP=${CHANGE_CONFIG_DIR_OWNERSHIP:-}
|
|
PLEX_CLAIM=${PLEX_CLAIM:-}
|
|
PLEX_CLAIM_FILE=${PLEX_CLAIM_FILE:-}
|
|
ADVERTISE_IP=${ADVERTISE_IP:-}
|
|
ALLOWED_NETWORKS=${ALLOWED_NETWORKS:-}
|
|
|
|
# If we are debugging, enable trace
|
|
if [ "${DEBUG,,}" = "true" ]; then
|
|
set -x
|
|
fi
|
|
|
|
# If the first run completed successfully, we are done
|
|
if [ -e /.firstRunComplete ]; then
|
|
exit 0
|
|
fi
|
|
|
|
function getPref {
|
|
local key="$1"
|
|
|
|
xmlstarlet sel -T -t -m "/Preferences" -v "@${key}" -n "${prefFile}"
|
|
}
|
|
|
|
function setPref {
|
|
local key="$1"
|
|
local value="$2"
|
|
|
|
count="$(xmlstarlet sel -t -v "count(/Preferences/@${key})" "${prefFile}")"
|
|
if [[ $count -gt 0 ]]; then
|
|
xmlstarlet ed --inplace --update "/Preferences/@${key}" -v "${value}" "${prefFile}"
|
|
else
|
|
xmlstarlet ed --inplace --insert "/Preferences" --type attr -n "${key}" -v "${value}" "${prefFile}"
|
|
fi
|
|
}
|
|
|
|
home=~plex
|
|
# shellcheck source=/dev/null
|
|
[ -f /etc/default/plexmediaserver ] && . /etc/default/plexmediaserver
|
|
pmsApplicationSupportDir="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR:-${home}/Library/Application Support}"
|
|
prefFile="${pmsApplicationSupportDir}/Plex Media Server/Preferences.xml"
|
|
|
|
# Setup user/group ids
|
|
if [ -n "${PLEX_UID}" ]; then
|
|
if [ ! "$(id -u plex)" -eq "${PLEX_UID}" ]; then
|
|
|
|
# usermod likes to chown the home directory, so create a new one and use that
|
|
# However, if the new UID is 0, we can't set the home dir back because the
|
|
# UID of 0 is already in use (executing this script).
|
|
if [ ! "${PLEX_UID}" -eq 0 ]; then
|
|
mkdir /tmp/temphome
|
|
usermod -d /tmp/temphome plex
|
|
fi
|
|
|
|
# Change the UID
|
|
usermod -o -u "${PLEX_UID}" plex
|
|
|
|
# Cleanup the temp home dir
|
|
if [ ! "${PLEX_UID}" -eq 0 ]; then
|
|
usermod -d /config plex
|
|
rm -Rf /tmp/temphome
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
if [ -n "${PLEX_GID}" ]; then
|
|
if [ ! "$(id -g plex)" -eq "${PLEX_GID}" ]; then
|
|
groupmod -o -g "${PLEX_GID}" plex
|
|
fi
|
|
fi
|
|
|
|
# Update ownership of dirs we need to write
|
|
if [ "${CHANGE_CONFIG_DIR_OWNERSHIP,,}" = "true" ]; then
|
|
if [ -f "${prefFile}" ]; then
|
|
if [ ! "$(stat -c %u "${prefFile}")" = "$(id -u plex)" ]; then
|
|
find /config \! \( -uid "$(id -u plex)" -gid "$(id -g plex)" \) -print0 | xargs -0 chown -h plex:plex
|
|
fi
|
|
else
|
|
find /config \! \( -uid "$(id -u plex)" -gid "$(id -g plex)" \) -print0 | xargs -0 chown -h plex:plex
|
|
fi
|
|
chown -R plex:plex /transcode
|
|
fi
|
|
|
|
# Create empty shell pref file if it doesn't exist already
|
|
if [ ! -e "${prefFile}" ]; then
|
|
echo "Creating pref shell"
|
|
mkdir -p "$(dirname "${prefFile}")"
|
|
cat > "${prefFile}" <<-EOF
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<Preferences/>
|
|
EOF
|
|
chown -R plex:plex "$(dirname "${prefFile}")"
|
|
fi
|
|
|
|
# Setup Server's client identifier
|
|
serial="$(getPref "MachineIdentifier")"
|
|
if [ -z "${serial}" ]; then
|
|
serial="$(uuidgen)"
|
|
setPref "MachineIdentifier" "${serial}"
|
|
fi
|
|
clientId="$(getPref "ProcessedMachineIdentifier")"
|
|
if [ -z "${clientId}" ]; then
|
|
clientId="$(echo -n "${serial}- Plex Media Server" | sha1sum | cut -b 1-40)"
|
|
setPref "ProcessedMachineIdentifier" "${clientId}"
|
|
fi
|
|
|
|
# Get server token and only turn claim token into server token if we have former but not latter.
|
|
token="$(getPref "PlexOnlineToken")"
|
|
if [ -z "${PLEX_CLAIM}" ] && [ -f "${PLEX_CLAIM_FILE}" ]; then
|
|
PLEX_CLAIM=$(cat "${PLEX_CLAIM_FILE}")
|
|
fi
|
|
if [ -n "${PLEX_CLAIM}" ] && [ -z "${token}" ]; then
|
|
echo "Attempting to obtain server token from claim token"
|
|
loginInfo="$(curl -X POST \
|
|
-H 'X-Plex-Client-Identifier: '"${clientId}" \
|
|
-H 'X-Plex-Product: Plex Media Server'\
|
|
-H 'X-Plex-Version: 1.1' \
|
|
-H 'X-Plex-Provides: server' \
|
|
-H 'X-Plex-Platform: Linux' \
|
|
-H 'X-Plex-Platform-Version: 1.0' \
|
|
-H 'X-Plex-Device-Name: PlexMediaServer' \
|
|
-H 'X-Plex-Device: Linux' \
|
|
"https://plex.tv/api/claim/exchange?token=${PLEX_CLAIM}")"
|
|
token="$(echo "$loginInfo" | sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p')"
|
|
|
|
if [ "$token" ]; then
|
|
echo "Token obtained successfully"
|
|
setPref "PlexOnlineToken" "${token}"
|
|
fi
|
|
fi
|
|
|
|
if [ -n "${ADVERTISE_IP}" ]; then
|
|
setPref "customConnections" "${ADVERTISE_IP}"
|
|
fi
|
|
|
|
if [ -n "${ALLOWED_NETWORKS}" ]; then
|
|
setPref "allowedNetworks" "${ALLOWED_NETWORKS}"
|
|
fi
|
|
|
|
# Set transcoder temp if not yet set
|
|
if [ -z "$(getPref "TranscoderTempDirectory")" ]; then
|
|
setPref "TranscoderTempDirectory" "/transcode"
|
|
fi
|
|
|
|
touch /.firstRunComplete
|
|
echo "Plex Media Server first run setup complete"
|