#!/usr/bin/env bash script_args=("$@") script_version='(in-development)' # auto-generated during release function help() { cat <&2; } function fail() { echo "FAIL: $@"; exit 1; } function process_args() { while [[ "${1-}" ]]; do case $1 in -h|-\?|--help) help exit ;; -v|--version) echo "$script_version" exit ;; --plex-pass) plex_pass=1 ;; *) warn "Unknown option (ignored): $1" ;; esac shift done } function dump_vars() { vars=( script_args script_version plex_pass pms_dir available_version installed_version tmp_dir package_file debug_json ) header 'Debugging info' for var in "${vars[@]}"; do (set -o posix ; set | grep ${var}= || true) done echo } function cleanup() { code=$? if [[ -d "${tmp_dir-}" ]]; then header 'Cleaning up' echo "Removing $tmp_dir" rm -rf $tmp_dir fi if [[ $code == 0 ]]; then echo echo 'Done!' else notify PlexUpdateError dump_vars echo 'Done, with errors!' echo echo 'If this problem persists, please file an issue here:' echo 'https://github.com/cowboy/synology-update-plex/issues' fi } function notify() { synonotify $1 '{"%PLEX_VERSION%":"'${available_version:-(unknown)}'"}' } function init_notifications() { local lang="$(source /etc/synoinfo.conf; echo "$maillang")" local mails_file=/var/cache/texts/$lang/mails if [[ ! -e "$mails_file" ]]; then header "Initializing notification system" echo "Notifications disabled (file $mails_file not found)" return fi if [[ ! "$(grep PlexUpdateInstalled $mails_file || true)" ]]; then header "Initializing notification system" cp $mails_file $mails_file.bak cat << 'EOF' >> $mails_file [PlexUpdateInstalled] Subject: Successfully updated Plex to %PLEX_VERSION% on %HOSTNAME% Dear user, Successfully updated Plex to %PLEX_VERSION% on %HOSTNAME% --- https://github.com/cowboy/synology-update-plex [PlexUpdateError] Subject: Unable to update Plex to %PLEX_VERSION% on %HOSTNAME% Dear user, Unable to update Plex to %PLEX_VERSION% on %HOSTNAME%. If this error persists, enable saving output results in Task Scheduler and file an issue at https://github.com/cowboy/synology-update-plex/issues including the script output. --- https://github.com/cowboy/synology-update-plex EOF echo 'Notifications installed' fi } function build_downloads_url() { downloads_url='https://plex.tv/api/downloads/5.json' if [[ "${plex_pass-}" ]]; then header "Enabling Plex Pass releases" pms_dir="$(echo /volume*"/Plex/Library/Application Support/Plex Media Server")" if [[ ! -d "$pms_dir" ]]; then pms_dir="$(find /volume* -type d -name 'Plex Media Server' -execdir test -e "{}/Preferences.xml" \; -print -quit)" fi if [[ ! -d "$pms_dir" ]]; then fail 'Unable to find "Plex Media Server" directory' fi local prefs_file="$pms_dir/Preferences.xml" if [[ ! -e "$prefs_file" ]]; then fail 'Unable to find Preferences.xml file' fi local token=$(grep -oP 'PlexOnlineToken="\K[^"]+' "$prefs_file" || true) if [[ -z "$token" ]]; then fail 'Unable to find Plex Token' fi echo "Found Plex Token" downloads_url="$downloads_url?channel=plexpass&X-Plex-Token=$token" fi } function retrieve_version_data() { header 'Retrieving version data' downloads_json="$(curl -s "$downloads_url")" if [[ -z "$downloads_json" ]]; then fail 'Unable to retrieve version data' fi } function set_available_version() { available_version=$(jq -r .nas.Synology.version <<< "$downloads_json") if [[ "$available_version" == "null" ]]; then debug_json="$downloads_json" fail 'Unable to retrieve version data' fi echo "Available version: $available_version" } function set_installed_version() { installed_version=$(synopkg version 'Plex Media Server') echo "Installed version: $installed_version" } # https://stackoverflow.com/a/4024263 function version_lte() { [[ "$1" == "$(echo -e "$1\n$2" | sort -V | head -n1)" ]] } function check_up_to_date() { set_available_version set_installed_version echo if version_lte "$available_version" "$installed_version"; then if [[ "$installed_version" != "$available_version" ]]; then echo 'The installed version of Plex is newer than the available version. If' \ 'you have Plex Pass, be sure to run this script with the --plex-pass option.' fi echo 'Plex is up-to-date.' exit fi echo 'New version available!' } function get_arch() { local arch machine=$1 hw_version=$2 if [[ "$machine" =~ armv7 ]]; then declare -A model_machine_map model_machine_map[DS414j]=armv7hf_neon model_machine_map[DS115j]=armv7hf model_machine_map[RS815]=armv7hf model_machine_map[DS216se]=armv7hf model_machine_map[DS215jv10-j]=armv7hf_neon if [[ "${model_machine_map[$hw_version]+_}" ]]; then arch=${model_machine_map[$hw_version]} elif [[ "${hw_version//[^0-9]/}" =~ 1[5-8]$ ]]; then arch=armv7hf_neon else arch=armv7hf fi elif [[ "$machine" =~ i686 ]]; then arch=x86 else arch=$machine fi echo $arch } function find_release() { header 'Finding release' local hw_version=$(