Merge pull request #1 from mrworf/master

Update PlexUpdate
This commit is contained in:
Duvrazh 2016-05-18 13:00:45 -04:00
commit 92d0ecda2c
2 changed files with 85 additions and 40 deletions

View file

@ -59,11 +59,6 @@ It's very simple, just execute the tool once configured. It will complain if you
Overall it tries to give you hints regarding why it isn't doing what you expected it to. Overall it tries to give you hints regarding why it isn't doing what you expected it to.
# known issues
- Command-line option handling needs cleanup
- Should extract the help text into a function instead
# trivia # trivia
- "kaka" is swedish for "cookie" - "kaka" is swedish for "cookie"

View file

@ -56,6 +56,9 @@ AUTOINSTALL=no
AUTODELETE=no AUTODELETE=no
AUTOUPDATE=no AUTOUPDATE=no
AUTOSTART=no AUTOSTART=no
CRON=no
QUIET=no
SILENT=no
# Default options for package managers, override if needed # Default options for package managers, override if needed
REDHAT_INSTALL="yum -y install" REDHAT_INSTALL="yum -y install"
@ -64,7 +67,7 @@ DEBIAN_INSTALL="dpkg -i"
# Sanity, make sure wget is in our path... # Sanity, make sure wget is in our path...
wget >/dev/null 2>/dev/null wget >/dev/null 2>/dev/null
if [ $? -eq 127 ]; then if [ $? -eq 127 ]; then
echo "Error: This script requires wget in the path. It could also signify that you don't have the tool installed." echo "Error: This script requires wget in the path. It could also signify that you don't have the tool installed." >&2
exit 1 exit 1
fi fi
@ -89,50 +92,84 @@ URL_LOGIN=https://plex.tv/users/sign_in
URL_DOWNLOAD=https://plex.tv/downloads?channel=plexpass URL_DOWNLOAD=https://plex.tv/downloads?channel=plexpass
URL_DOWNLOAD_PUBLIC=https://plex.tv/downloads URL_DOWNLOAD_PUBLIC=https://plex.tv/downloads
usage() {
echo "Usage: $(basename $0) [-aCfhkopqsSuU]"
echo " -a Auto install if download was successful (requires root)"
echo " -C Cron mode. Only output to stdout on an actionable operation"
echo " -d Auto delete after auto install"
echo " -f Force download even if it's the same version or file"
echo " already exists (WILL NOT OVERWRITE)"
echo " -h This help"
echo " -k Reuse last authentication"
echo " -o 32-bit version (default 64 bit)"
echo " -p Public Plex Media Server version"
echo " -q Quiet mode. No stdout, only stderr and exit codes"
echo " -r Print download URL and exit"
echo " -s Auto start (needed for some distros)"
echo " -S Silent mode. No text output, only exit codes"
echo " -u Auto update plexupdate.sh before running it (experimental)"
echo " -U Do not autoupdate plexupdate.sh (experimental, default)"
echo
exit 0
}
# Parse commandline # Parse commandline
ALLARGS="$@" ALLARGS="$@"
set -- $(getopt aufhkro: -- "$@") set -- $(getopt aCdfhkopqruU: -- "$@")
while true; while true;
do do
case "$1" in case "$1" in
(-h) echo -e "Usage: $(basename $0) [-afhkopsuU]\n\na = Auto install if download was successful (requires root)\nd = Auto delete after auto install\nf = Force download even if it's the same version or file already exists (WILL NOT OVERWRITE)\nh = This help\nk = Reuse last authentication\no = 32-bit version (default 64 bit)\np = Public Plex Media Server version\nu = Auto update plexupdate.sh before running it (experimental)\nU = Do not autoupdate plexupdate.sh (experimental, default)\ns = Auto start (needed for some distros)\np = Print download URL and exit\n"; exit 0;; (-h) usage;;
(-a) AUTOINSTALL=yes;; (-a) AUTOINSTALL=yes;;
(-d) AUTODELETE=yes;; (-C) CRON=yes;;
(-f) FORCE=yes;; (-d) AUTODELETE=yes;;
(-k) KEEP=yes;; (-f) FORCE=yes;;
(-o) RELEASE="32";; (-k) KEEP=yes;;
(-p) PUBLIC=yes;; (-o) RELEASE="32";;
(-u) AUTOUPDATE=yes;; (-p) PUBLIC=yes;;
(-U) AUTOUPDATE=no;; (-q) QUIET=yes;;
(-s) AUTOSTART=yes;; (-r) PRINT_URL=yes;;
(-r) PRINT_URL=yes;; (-s) AUTOSTART=yes;;
(--) ;; (-S) SILENT=yes;;
(-*) echo "Error: unrecognized option $1" 1>&2; exit 1;; (-u) AUTOUPDATE=yes;;
(*) break;; (-U) AUTOUPDATE=no;;
(--) ;;
(-*) echo "Error: unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac esac
shift shift
done done
# send all stdout to /dev/null
if [ "${QUIET}" = "yes" ] || [ "${SILENT}" = "yes" ]; then
exec 1> /dev/null
fi
# send all stdout and stderr to /dev/null
if [ "${SILENT}" = "yes" ]; then
exec 2> /dev/null
fi
if [ "${AUTOUPDATE}" == "yes" ]; then if [ "${AUTOUPDATE}" == "yes" ]; then
git >/dev/null 2>/dev/null git >/dev/null 2>/dev/null
if [ $? -eq 127 ]; then if [ $? -eq 127 ]; then
echo "Error: You need to have git installed for this to work" echo "Error: You need to have git installed for this to work" >&2
exit 1 exit 1
fi fi
pushd "$(dirname "$0")" >/dev/null pushd "$(dirname "$0")" >/dev/null
if [ ! -d .git ]; then if [ ! -d .git ]; then
echo "Error: This is not a git repository, auto update only works if you've done a git clone" echo "Error: This is not a git repository, auto update only works if you've done a git clone" >&2
exit 1 exit 1
fi fi
git status | grep "git commit -a" >/dev/null 2>/dev/null git status | grep "git commit -a" >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Error: You have made changes to the script, cannot auto update" echo "Error: You have made changes to the script, cannot auto update" >&2
exit 1 exit 1
fi fi
echo -n "Auto updating..." echo -n "Auto updating..."
git pull >/dev/null git pull >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo 'Error: Unable to update git, try running "git pull" manually to see what is wrong' echo 'Error: Unable to update git, try running "git pull" manually to see what is wrong' >&2
exit 1 exit 1
fi fi
echo "OK" echo "OK"
@ -141,7 +178,7 @@ if [ "${AUTOUPDATE}" == "yes" ]; then
if [ -f "$0" ]; then if [ -f "$0" ]; then
/bin/bash "$0" ${ALLARGS} -U /bin/bash "$0" ${ALLARGS} -U
else else
echo "Error: Unable to relaunch, couldn't find $0" echo "Error: Unable to relaunch, couldn't find $0" >&2
exit 1 exit 1
fi fi
else else
@ -152,14 +189,14 @@ fi
# Sanity check # Sanity check
if [ "${EMAIL}" == "" -o "${PASS}" == "" ] && [ "${PUBLIC}" == "no" ] && [ ! -f /tmp/kaka ]; then if [ "${EMAIL}" == "" -o "${PASS}" == "" ] && [ "${PUBLIC}" == "no" ] && [ ! -f /tmp/kaka ]; then
echo "Error: Need username & password or -k option to download PlexPass version. Otherwise run with -p to download public version." echo "Error: Need username & password or -k option to download PlexPass version. Otherwise run with -p to download public version." >&2
exit 1 exit 1
fi fi
if [ "${AUTOINSTALL}" == "yes" -o "${AUTOSTART}" == "yes" ]; then if [ "${AUTOINSTALL}" == "yes" -o "${AUTOSTART}" == "yes" ]; then
id | grep -i 'uid=0(' 2>&1 >/dev/null id | grep -i 'uid=0(' 2>&1 >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Error: You need to be root to use autoinstall/autostart option." echo "Error: You need to be root to use autoinstall/autostart option." >&2
exit 1 exit 1
fi fi
fi fi
@ -168,7 +205,7 @@ fi
# Remove any ~ or other oddness in the path we're given # Remove any ~ or other oddness in the path we're given
DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } ; if [ $? -eq 0 ]; then pwd; fi)" DOWNLOADDIR="$(eval cd ${DOWNLOADDIR// /\\ } ; if [ $? -eq 0 ]; then pwd; fi)"
if [ -z "${DOWNLOADDIR}" ]; then if [ -z "${DOWNLOADDIR}" ]; then
echo "Error: Download directory does not exist or is not a directory" echo "Error: Download directory does not exist or is not a directory" >&2
exit 1 exit 1
fi fi
@ -229,14 +266,16 @@ trap cleanup EXIT
# If user wants, we skip authentication, but only if previous auth exists # If user wants, we skip authentication, but only if previous auth exists
if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then
echo -n "Authenticating..." if [ "${CRON}" = "no" ]; then
echo -n "Authenticating..."
fi
# Clean old session # Clean old session
rm /tmp/kaka 2>/dev/null rm /tmp/kaka 2>/dev/null
# Get initial seed we need to authenticate # Get initial seed we need to authenticate
SEED=$(wget --save-cookies /tmp/kaka --keep-session-cookies ${URL_LOGIN} -O - 2>/dev/null | grep 'name="authenticity_token"' | sed 's/.*value=.\([^"]*\).*/\1/') SEED=$(wget --save-cookies /tmp/kaka --keep-session-cookies ${URL_LOGIN} -O - 2>/dev/null | grep 'name="authenticity_token"' | sed 's/.*value=.\([^"]*\).*/\1/')
if [ $? -ne 0 -o "${SEED}" == "" ]; then if [ $? -ne 0 -o "${SEED}" == "" ]; then
echo "Error: Unable to obtain authentication token, page changed?" echo "Error: Unable to obtain authentication token, page changed?" >&2
exit 1 exit 1
fi fi
@ -251,7 +290,7 @@ if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then
# Authenticate # Authenticate
wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_LOGIN}" --post-file=/tmp/postdata -O /tmp/raw 2>/dev/null wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_LOGIN}" --post-file=/tmp/postdata -O /tmp/raw 2>/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Error: Unable to authenticate" echo "Error: Unable to authenticate" >&2
exit 1 exit 1
fi fi
# Delete authentication data ... Bad idea to let that stick around # Delete authentication data ... Bad idea to let that stick around
@ -259,10 +298,12 @@ if [ "${KEEP}" != "yes" -o ! -f /tmp/kaka ] && [ "${PUBLIC}" == "no" ]; then
# Provide some details to the end user # Provide some details to the end user
if [ "$(cat /tmp/raw | grep 'Sign In</title')" != "" ]; then if [ "$(cat /tmp/raw | grep 'Sign In</title')" != "" ]; then
echo "Error: Username and/or password incorrect" echo "Error: Username and/or password incorrect" >&2
exit 1 exit 1
fi fi
echo "OK" if [ "${CRON}" = "no" ]; then
echo "OK"
fi
elif [ "$PUBLIC" != "no" ]; then elif [ "$PUBLIC" != "no" ]; then
# It's a public version, so change URL and make doubly sure that cookies are empty # It's a public version, so change URL and make doubly sure that cookies are empty
rm 2>/dev/null >/dev/null /tmp/kaka rm 2>/dev/null >/dev/null /tmp/kaka
@ -271,10 +312,13 @@ elif [ "$PUBLIC" != "no" ]; then
fi fi
# Extract the URL for our release # Extract the URL for our release
echo -n "Finding download URL for ${RELEASE}..." if [ "${CRON}" = "no" ]; then
echo -n "Finding download URL for ${RELEASE}..."
fi
DOWNLOAD=$(wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep "${PKGEXT}" | grep -m 1 "${RELEASE}" | sed "s/.*href=\"\([^\"]*\\${PKGEXT}\)\"[^>]*>.*/\1/" ) DOWNLOAD=$(wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep "${PKGEXT}" | grep -m 1 "${RELEASE}" | sed "s/.*href=\"\([^\"]*\\${PKGEXT}\)\"[^>]*>.*/\1/" )
echo -e "OK" if [ "${CRON}" = "no" ]; then
echo -e "OK"
fi
if [ "${DOWNLOAD}" == "" ]; then if [ "${DOWNLOAD}" == "" ]; then
echo "Sorry, page layout must have changed, I'm unable to retrieve the URL needed for download" echo "Sorry, page layout must have changed, I'm unable to retrieve the URL needed for download"
@ -305,12 +349,16 @@ else
INSTALLED_VERSION=$(rpm -qv plexmediaserver 2>/dev/null) INSTALLED_VERSION=$(rpm -qv plexmediaserver 2>/dev/null)
fi fi
if [[ $FILENAME == *$INSTALLED_VERSION* ]] && [ "${FORCE}" != "yes" ] && [ ! -z "${INSTALLED_VERSION}" ]; then if [[ $FILENAME == *$INSTALLED_VERSION* ]] && [ "${FORCE}" != "yes" ] && [ ! -z "${INSTALLED_VERSION}" ]; then
echo "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download." if [ "${CRON}" = "no" ]; then
echo "Your OS reports the latest version of Plex ($INSTALLED_VERSION) is already installed. Use -f to force download."
fi
exit 5 exit 5
fi fi
if [ -f "${DOWNLOADDIR}/${FILENAME}" -a "${FORCE}" != "yes" ]; then if [ -f "${DOWNLOADDIR}/${FILENAME}" -a "${FORCE}" != "yes" ]; then
echo "File already exists, won't download." if [ "${CRON}" = "no" ]; then
echo "File already exists, won't download."
fi
if [ "${AUTOINSTALL}" != "yes" ]; then if [ "${AUTOINSTALL}" != "yes" ]; then
exit 2 exit 2
fi fi
@ -319,7 +367,9 @@ fi
if [ "${SKIP_DOWNLOAD}" == "no" ]; then if [ "${SKIP_DOWNLOAD}" == "no" ]; then
if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then
echo "Note! File exists, but asked to overwrite with new copy" if [ "${CRON}" = "no" ]; then
echo "Note! File exists, but asked to overwrite with new copy"
fi
fi fi
echo -ne "Downloading release \"${FILENAME}\"..." echo -ne "Downloading release \"${FILENAME}\"..."