forked from pith/docker-plex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.sh
executable file
·117 lines (97 loc) · 4.19 KB
/
start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
# If debug mode, then enable xtrace
if [ "${DEBUG,,}" = "true" ]; then
set -x
fi
# Set the defaults
RUN_AS_ROOT=${RUN_AS_ROOT:-true}
CHANGE_DIR_RIGHTS=${CHANGE_DIR_RIGHTS:-false}
CHANGE_CONFIG_DIR_OWNERSHIP=${CHANGE_CONFIG_DIR_OWNERSHIP:-true}
if [ "$(id -u)" -eq 0 -a "$(id -g)" -eq 0 ]; then
GROUP=plextmp
TARGET_GID=$(stat -c "%g" /data)
EXISTS=$(getent group "${TARGET_GID}" | wc -l)
# Create new group using target GID and add plex user
if [ "$EXISTS" = "0" ]; then
groupadd --gid "${TARGET_GID}" "${GROUP}"
else
# GID exists, find group name and add
GROUP=$(getent group "$TARGET_GID" | cut -d: -f1)
fi
usermod -a -G "${GROUP}" plex
if [[ -n "${SKIP_CHOWN_CONFIG}" ]]; then
CHANGE_CONFIG_DIR_OWNERSHIP=false
fi
if [ "${CHANGE_CONFIG_DIR_OWNERSHIP,,}" = "true" ]; then
find /config ! -user plex -print0 | xargs -0 -I{} chown -R plex: {} &
fi
# Will change all files in directory to be readable by group
if [ "${CHANGE_DIR_RIGHTS,,}" = "true" ]; then
chgrp -R "${GROUP}" /data &
chmod -R g+rX /data &
fi
fi
# Preferences
[ -f /etc/default/plexmediaserver ] && . /etc/default/plexmediaserver
PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR:-${HOME}/Library/Application Support}"
PLEX_PREFERENCES="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/Preferences.xml"
PLEX_PID="${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}/Plex Media Server/plexmediaserver.pid"
getPreference(){
local preference_key="$1"
xmlstarlet sel -T -t -m "/Preferences" -v "@$preference_key" -n "${PLEX_PREFERENCES}"
}
setPreference(){
local preference_key="$1"
local preference_val="$2"
if [ -z "$(getPreference "$preference_key")" ]; then
xmlstarlet ed --inplace --insert "Preferences" --type attr -n "$preference_key" -v "$preference_val" "${PLEX_PREFERENCES}"
else
xmlstarlet ed --inplace --update "/Preferences[@$preference_key]/$preference_key" -v "$preference_val" "${PLEX_PREFERENCES}"
fi
}
if [ ! -f "${PLEX_PREFERENCES}" ]; then
mkdir -p "$(dirname "${PLEX_PREFERENCES}")"
cp /Preferences.xml "${PLEX_PREFERENCES}"
fi
# Set the PlexOnlineToken to PLEX_TOKEN if defined,
# otherwise get plex token if PLEX_USERNAME and PLEX_PASSWORD are defined,
# otherwise account must be manually linked via Plex Media Server in Settings > Server
echo "PLEX_TOKEN :"${PLEX_TOKEN}
echo "PLEX_USERNAME :"${PLEX_USERNAME}
echo "PLEX_PASSWORD :"${PLEX_PASSWORD}
if [ -n "${PLEX_TOKEN}" ]; then
setPreference PlexOnlineToken ${PLEX_TOKEN}
elif [ -n "${PLEX_USERNAME}" ] && [ -n "${PLEX_PASSWORD}" ] && [ -z "$(getPreference "PlexOnlineToken")" ]; then
# Ask Plex.tv a token key
PLEX_TOKEN=$(curl -u "${PLEX_USERNAME}":"${PLEX_PASSWORD}" 'https://plex.tv/users/sign_in.xml' \
-X POST -H 'X-Plex-Device-Name: PlexMediaServer' \
-H 'X-Plex-Provides: server' \
-H 'X-Plex-Version: 0.9' \
-H 'X-Plex-Platform-Version: 0.9' \
-H 'X-Plex-Platform: xcid' \
-H 'X-Plex-Product: Plex Media Server'\
-H 'X-Plex-Device: Linux'\
-H 'X-Plex-Client-Identifier: XXXX' --compressed | sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p')
fi
echo "AFTER PLEX_TOKEN :"${PLEX_TOKEN}
echo "PLEX_USERNAME :"${PLEX_USERNAME}
echo "PLEX_PASSWORD :"${PLEX_PASSWORD}
if [ "${PLEX_TOKEN}" ]; then
setPreference PlexOnlineToken "${PLEX_TOKEN}"
fi
# Tells Plex the external port is not "32400" but something else.
# Useful if you run multiple Plex instances on the same IP
[ -n "${PLEX_EXTERNALPORT}" ] && setPreference ManualPortMappingPort "${PLEX_EXTERNALPORT}"
# Allow disabling the remote security (hidding the Server tab in Settings)
[ -n "${PLEX_DISABLE_SECURITY}" ] && setPreference disableRemoteSecurity "${PLEX_DISABLE_SECURITY}"
# Detect networks and add them to the allowed list of networks
PLEX_ALLOWED_NETWORKS=${PLEX_ALLOWED_NETWORKS:-$(ip route | grep '/' | awk '{print $1}' | paste -sd "," -)}
[ -n "${PLEX_ALLOWED_NETWORKS}" ] && setPreference allowedNetworks "${PLEX_ALLOWED_NETWORKS}"
# Remove previous pid if it exists
rm -f "${PLEX_PID}"
# Current defaults to run as root while testing.
if [ "${RUN_AS_ROOT,,}" = "true" ]; then
/usr/sbin/start_pms
else
sudo -u plex -E sh -c "/usr/sbin/start_pms"
fi