2016-06-30 22:59:35 +01:00
#!/usr/bin/with-contenv bash
2016-01-18 09:46:00 +00:00
2016-08-22 22:02:36 +01:00
# copy config on first run, regardless of update status
[[ ! -e /etc/default/plexmediaserver ]] && \
cp /defaults/plexmediaserver /etc/default/plexmediaserver
2016-08-22 13:20:40 +01:00
# set no update message
2016-08-22 19:26:13 +01:00
[[ -e /tmp/no-version.nfo ]] && \
2016-08-22 22:02:36 +01:00
rm /tmp/no-version.nfo
2016-08-22 19:26:13 +01:00
NOVERSION_SET='/tmp/no-version.nfo'
cat > "${NOVERSION_SET}" <<-EOFVERSION
2016-08-22 13:20:40 +01:00
#######################################################
2016-08-22 19:26:13 +01:00
# Update routine will not run because you havent set #
2016-08-22 22:47:27 +01:00
# the VERSION variable or you opted out of updates. #
# For more information checkout :- #
2016-08-22 19:26:13 +01:00
# https://github.com/linuxserver/docker-plex #
2016-08-22 13:20:40 +01:00
#######################################################
2016-08-22 19:26:13 +01:00
EOFVERSION
2016-08-22 13:20:40 +01:00
2016-08-23 02:32:22 +01:00
# set update failed message
[[ -e /tmp/update_fail.nfo ]] && \
rm /tmp/update_fail.nfo
UPGRADE_FAIL='/tmp/update_fail.nfo'
cat > "${UPGRADE_FAIL}" <<-EOFFAIL
########################################################
# Upgrade attempt failed, this could be because either #
# plex update site is down, local network issues, or #
# you were trying to get a version that simply doesn't #
# exist, check over the VERSION variable thoroughly & #
# correct it or try again later. #
########################################################
EOFFAIL
2016-08-22 13:20:40 +01:00
# test for no version set or opt out for autoupdates
2016-08-22 20:41:24 +01:00
if [[ -z "$VERSION" ]] || [[ "$VERSION" == "0" ]] || [[ ! -z "$ADVANCED_DISABLEUPDATES" ]]; then
2016-08-22 22:47:27 +01:00
printf "\n\n\n%s\n\n\n" "$(</tmp/no-version.nfo)"
2016-08-22 19:26:13 +01:00
exit 0
fi
2016-01-18 09:46:00 +00:00
2016-07-02 20:15:50 +01:00
# set header for no preferences/token message
2016-08-22 12:44:21 +01:00
[[ -e /tmp/no-token.nfo ]] && \
rm /tmp/no-token.nfo
2016-07-02 20:15:50 +01:00
NOTOKEN_SET='/tmp/no-token.nfo'
cat > "${NOTOKEN_SET}" <<-EOFTOKEN
#####################################################
2016-07-02 20:28:43 +01:00
# Login via the webui at http://<ip>:32400/web #
# and restart the docker, because there was no #
2016-07-02 20:15:50 +01:00
EOFTOKEN
2016-03-30 10:20:52 +02:00
2016-07-02 20:15:50 +01:00
# if preferences files doesn't exist, exit out
if [ ! -e "/config/Library/Application Support/Plex Media Server/Preferences.xml" ]; then
cat >> "${NOTOKEN_SET}" <<-EOFTOKEN
# preference file found, possibly first startup. #
#####################################################
EOFTOKEN
2016-08-22 22:47:27 +01:00
printf "\n\n\n%s\n\n\n" "$(</tmp/no-token.nfo)"
2016-07-02 20:15:50 +01:00
exit 0
fi
# attempt to read plex token
PLEX_TOKEN=$( sed -n 's/.*PlexOnlineToken="//p' \
2016-08-22 12:44:21 +01:00
"/config/Library/Application Support/Plex Media Server/Preferences.xml" \
| sed "s/\".*//")
2016-05-23 05:17:14 -07:00
2016-07-02 20:15:50 +01:00
# if plex token isn't found, exit out
if [ -z "$PLEX_TOKEN" ]; then
cat >> "${NOTOKEN_SET}" <<-EOFTOKEN
2016-07-02 20:28:43 +01:00
# plex token found in the preference file #
2016-07-02 20:15:50 +01:00
#####################################################
EOFTOKEN
2016-08-22 22:47:27 +01:00
printf "\n\n\n%s\n\n\n" "$(</tmp/no-token.nfo)"
2016-07-02 20:15:50 +01:00
exit 0
fi
# determine installed version of plex
2016-08-23 02:32:22 +01:00
INSTALLED_VERSION=$(dpkg-query -W -f='${Version}' plexmediaserver)
2015-08-28 12:36:53 +02:00
2016-07-02 20:15:50 +01:00
# start update routine
2016-08-22 22:02:36 +01:00
if [[ "$VERSION" = latest ]] || [[ "$VERSION" = plexpass ]] || [[ "$PLEXPASS" == "1" ]]; then
2016-08-23 08:57:18 +01:00
REMOTE_VERSION=$(curl -s "${PLEX_INSTALL}&X-Plex-Token=$PLEX_TOKEN"| cut -d "/" -f 5 )
2016-08-23 02:32:22 +01:00
elif [[ "$VERSION" = public ]]; then
2016-08-22 22:02:36 +01:00
PLEX_TOKEN=""
2016-08-23 08:57:18 +01:00
REMOTE_VERSION=$(curl -s "${PLEX_INSTALL}&X-Plex-Token=$PLEX_TOKEN"| cut -d "/" -f 5 )
2016-08-23 02:32:22 +01:00
else
REMOTE_VERSION="${VERSION}"
2015-05-10 10:50:00 +02:00
fi
2015-08-28 12:36:53 +02:00
2016-08-23 02:32:22 +01:00
if [[ "$REMOTE_VERSION" == "$INSTALLED_VERSION" ]]; then
2016-08-22 22:02:36 +01:00
echo "No update required"
exit 0
fi
2016-08-23 02:32:22 +01:00
echo "Atempting to upgrade to: $REMOTE_VERSION"
2016-08-23 10:01:37 +01:00
rm -f /tmp/plexmediaserver_*.deb
wget -nv -P /tmp \
"${PLEX_DOWNLOAD}/$REMOTE_VERSION/plexmediaserver_${REMOTE_VERSION}_amd64.deb"
2016-08-23 08:57:18 +01:00
last=$?
2016-08-23 03:47:23 +01:00
2016-08-23 10:01:37 +01:00
# test if deb file size is ok, or if download failed
if [[ "$last" -gt "0" ]] || [[ $(stat -c %s /tmp/plexmediaserver_"${REMOTE_VERSION}"_amd64.deb) -lt 10000 ]]; then
2016-08-23 02:32:22 +01:00
printf "\n\n\n%s\n\n\n" "$(</tmp/update_fail.nfo)"
exit 0
2016-08-23 10:01:37 +01:00
# if ok, try to install it.
else
2016-08-23 10:10:39 +01:00
dpkg -i /tmp/plexmediaserver_"${REMOTE_VERSION}"_amd64.deb
2016-08-23 10:01:37 +01:00
rm -f /tmp/plexmediaserver_*.deb
2016-08-23 02:32:22 +01:00
fi
2016-07-02 20:15:50 +01:00
2016-08-23 10:01:37 +01:00
# test if plex is installed and try re-pulling latest if not
dpkg -s plexmediaserver > /dev/null 2>&1 || \
(echo "for some reason the update has failed and plex doesn't seem to be installed, retrying a known version" && curl -o /tmp/plexmediaserver.deb -L "${PLEX_INSTALL}" && dpkg -i /tmp/plexmediaserver.deb)
2016-08-22 22:02:36 +01:00
# recopy config file in case update overwrites our copy
2016-08-23 10:01:37 +01:00
cp /defaults/plexmediaserver /etc/default/plexmediaserver