2019-12-04 11:41:45 -05:00
#!/usr/bin/with-contenv bash
2024-01-08 21:17:32 -06:00
# shellcheck shell=bash
2019-12-04 11:41:45 -05:00
2024-01-08 21:17:32 -06:00
PLEX_MEDIA_SERVER_PREFERENCES="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/Preferences.xml"
if grep -qs "PlexOnlineToken" "${PLEX_MEDIA_SERVER_PREFERENCES}"; then
2022-10-19 10:17:56 -05:00
echo "**** Server already claimed ****"
exit 0
2024-01-08 21:17:32 -06:00
elif [[ -z "$PLEX_CLAIM" ]]; then
2022-10-19 10:17:56 -05:00
echo "**** Server is unclaimed, but no claim token has been set ****"
2022-07-18 10:17:18 -04:00
exit 0
2019-12-04 11:41:45 -05:00
fi
2024-01-08 21:17:32 -06:00
if [[ ! -f "${PLEX_MEDIA_SERVER_PREFERENCES}" ]]; then
2022-07-18 10:17:18 -04:00
UMASK_SET="${UMASK_SET:-022}"
umask "$UMASK_SET"
echo "Temporarily starting Plex Media Server."
2024-01-08 21:17:32 -06:00
PLEX_MEDIA_SERVER_INFO_MODEL=$(uname -m)
export PLEX_MEDIA_SERVER_INFO_MODEL
PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION=$(uname -r)
export PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION
2022-07-18 10:17:18 -04:00
s6-setuidgid abc /bin/bash -c \
2024-01-08 21:17:32 -06:00
'LD_LIBRARY_PATH=/usr/lib/plexmediaserver:/usr/lib/plexmediaserver/lib /usr/lib/plexmediaserver/Plex\ Media\ Server' &
PID=$!
2022-07-18 10:17:18 -04:00
echo "Waiting for Plex to generate its config"
DBNAME="/config/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db-wal"
2024-01-08 21:17:32 -06:00
until [[ -f "${DBNAME}" ]]; do
2022-07-18 10:17:18 -04:00
sleep 1
done
while true; do
echo "Waiting for database creation to complete..."
2024-01-08 21:17:32 -06:00
if [[ -z "${COMPARE_MD5+x}" ]]; then
COMPARE_MD5=$(md5sum "${DBNAME}" | cut -c1-8)
2022-07-18 10:17:18 -04:00
sleep 3
else
sleep 3
2024-01-08 21:17:32 -06:00
CURRENT_MD5=$(md5sum "${DBNAME}" | cut -c1-8)
if [[ "${CURRENT_MD5}" == "${COMPARE_MD5}" ]]; then
2022-07-18 10:17:18 -04:00
break
else
2024-01-08 21:17:32 -06:00
COMPARE_MD5=$(md5sum "${DBNAME}" | cut -c1-8)
2022-07-18 10:17:18 -04:00
fi
fi
done
2024-01-08 21:17:32 -06:00
until grep -qs "ProcessedMachineIdentifier" "${PLEX_MEDIA_SERVER_PREFERENCES}"; do
2022-07-18 10:17:18 -04:00
sleep 1
done
while true; do
echo "Waiting for pref file creation to complete..."
2024-01-08 21:17:32 -06:00
if [[ -z "${PREF_COMPARE_MD5+x}" ]]; then
PREF_COMPARE_MD5=$(md5sum "${PLEX_MEDIA_SERVER_PREFERENCES}" | cut -c1-8)
2022-07-18 10:17:18 -04:00
sleep 3
else
sleep 3
2024-01-08 21:17:32 -06:00
PREF_CURRENT_MD5=$(md5sum "${PLEX_MEDIA_SERVER_PREFERENCES}" | cut -c1-8)
if [[ "${PREF_CURRENT_MD5}" == "${PREF_COMPARE_MD5}" ]]; then
2022-07-18 10:17:18 -04:00
break
else
2024-01-08 21:17:32 -06:00
PREF_COMPARE_MD5=$(md5sum "${PLEX_MEDIA_SERVER_PREFERENCES}" | cut -c1-8)
2022-07-18 10:17:18 -04:00
fi
fi
done
echo "Stopping Plex to claim server"
2024-01-08 21:17:32 -06:00
while ps -p $PID >/dev/null; do
2022-07-18 10:17:18 -04:00
kill $PID
sleep 1
done
echo "Plex stopped"
2019-12-04 11:41:45 -05:00
fi
2024-01-08 21:17:32 -06:00
ProcessedMachineIdentifier=$(sed -n "s/^.*ProcessedMachineIdentifier=\"\([^\"]*\)\".*$/\1/p" "${PLEX_MEDIA_SERVER_PREFERENCES}")
2019-12-04 11:41:45 -05:00
PlexOnlineToken="$(curl -X POST \
2022-07-18 10:17:18 -04:00
-H 'X-Plex-Client-Identifier: '"${ProcessedMachineIdentifier}" \
2024-01-08 21:17:32 -06:00
-H 'X-Plex-Product: Plex Media Server' \
2022-07-18 10:17:18 -04:00
-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' \
2024-01-08 21:17:32 -06:00
"https://plex.tv/api/claim/exchange?token=${PLEX_CLAIM}" |
sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p')"
2019-12-04 11:41:45 -05:00
2024-01-08 21:17:32 -06:00
if [[ -n "$PlexOnlineToken" ]]; then
2022-07-18 10:17:18 -04:00
echo "Server claimed successfully, navigate to http://serverip:32400/web to complete plex setup."
2024-01-08 21:17:32 -06:00
sed -i "s/\/>/ PlexOnlineToken=\"${PlexOnlineToken}\"\/>/g" "${PLEX_MEDIA_SERVER_PREFERENCES}"
2019-12-04 11:41:45 -05:00
else
2022-07-18 10:17:18 -04:00
echo "Unable to claim Plex server. Either manually claim by connecting to http://serverip:32400/web from the same network subnet, or recreate container with a new claim token."
2022-10-19 10:17:56 -05:00
fi