#!/bin/sh # plexinit # Plex Media Server Extension for XigmaNAS 11.0 and later version. # (https://www.xigmanas.com/forums/viewtopic.php?f=71&t=11184) # License: BSD2CLAUSE (BSD 2-clause Simplified License). # *Plex(c) (Plex Media Server) is a registered trademark of Plex(c), Inc. # Debug script. #set -x # Copyright (c) 2018 José Rivera (JoseMR) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that following conditions are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 'AS IS' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Set environment. PATH=${PATH}:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin # Set global variables. SCRIPTNAME=$(basename $0) CWDIR=$(dirname $(realpath $0)) PLATFORM=$(uname -m) PRODUCT=$(uname -i) PRDVERSION=$(uname -r | cut -d '-' -f1 | tr -d '.') PRDPLATFORM=$(cat /etc/platform) DATE=$(date +"%a %b %d %T %Y") APPNAME="Plex Extension" PRDNAME="plexmediaserver" PKGCONF="/etc/pkg/FreeBSD.conf" WWWPATH="/usr/local/www" PKGCACHE="/var/cache/pkg" PKGDB="/var/db/pkg" PLEXCONF="/conf/plex_config" PLEXCONFLINK="/var/etc/plexconf" COMPATPATH1="/usr/local/lib/compat" COMPATPATH2="/usr/local/lib32/compat" COMPATPATH3="/usr/local/libdata/ldconfig" COMPATPATH4="/usr/local/libdata/ldconfig32" PLEXMINVER="1150573" PLEXPIDFILE="/var/run/plex/plex.pid" USRLOCALPATH="/usr/local/share" PLEXPATH="${USRLOCALPATH}/${PRDNAME}" RCDPATH="/usr/local/etc/rc.d" PLEXRCD="${RCDPATH}/${PRDNAME}" SUPPORTPATH="${CWDIR}/plexdata" TARFILE=${2} CONFIG="/cf/conf/config.xml" RCCONF="/etc/rc.conf" FULLPRDNAME="Plex Media Server" PLEXLOGFILE="${CWDIR}/log/plex_ext.log" CMDNAME=${PRDNAME} CMDFILE="Plex_Media_Server" BRANCH="master" PINGHOST="freebsd.org" GITURL="https://github.com/JRGTH/xigmanas-plex-extension/archive/${BRANCH}.zip" VERFILE="https://raw.githubusercontent.com/JRGTH/xigmanas-plex-extension/${BRANCH}/plex/version" error_notify() { # Log/notify message on error and exit. MSG="$*" logger -t "${SCRIPTNAME}" "${MSG}" echo -e "${MSG}" >&2; exit 1 } runtime_config() { # Get current plex extension version. if [ -f "${CWDIR}/version" ]; then APPVERSION=$(cat ${CWDIR}/version) else APPVERSION="version file not found!" fi # Create required directories if missing. if [ ! -d "${CWDIR}/conf" ]; then mkdir -p ${CWDIR}/conf fi if [ ! -d "${CWDIR}/log" ]; then mkdir -p ${CWDIR}/log fi # Create extension config file. if [ ! -f "${CWDIR}${PLEXCONF}" ]; then touch ${CWDIR}${PLEXCONF} fi # Set requested plex channel variables. if [ -f "${CWDIR}${PLEXCONF}" ]; then PLEXCHANNEL=$(sysrc -f ${CWDIR}${PLEXCONF} -qn PLEX_CHANNEL) if [ "${PLEXCHANNEL}" = "plexpass" ]; then plexpass_channel elif [ "${PLEXCHANNEL}" = "standard" ]; then standard_channel elif [ -z "${PLEXCHANNEL}" ]; then plex_detect fi else plex_detect fi } plex_detect() { # Set the proper channel variables based on the installed plex package and write default configuration. # We have to use this method since plexmediaserver --version or equivalent command is broken on FreeBSD. STANDARD=1 PLEXPASS=1 if [ -f "${USRLOCALPATH}/${PRDNAME}/${CMDFILE}" ]; then STANDARD=0 fi if [ -f "${USRLOCALPATH}/${PRDNAME}-plexpass/${CMDFILE}" ]; then PLEXPASS=0 fi # Stop script execution if multiple plex versions detected. if [ "${STANDARD}" -eq 0 ] && [ "${PLEXPASS}" -eq 0 ]; then error_notify "Error: Multiple ${FULLPRDNAME} versions found." elif [ "${STANDARD}" -eq 0 ]; then PLEXCH="standard" default_config standard_channel elif [ "${PLEXPASS}" -eq 0 ]; then PLEXCH="plexpass" default_config plexpass_channel fi } standard_channel() { CMD_ARG="" PKG_ARG="" } plexpass_channel() { CMD_ARG="_plexpass" PKG_ARG="-plexpass" } get_plex_version() { # Get current plex version. if [ -z "${PKG_ARG}" ]; then PLEXVERSION=$(pkg info -I ${PRDNAME}${PKG_ARG} | awk '{print $1}' | cut -d- -f2 | tr -d . | sed 's/_[0-9]//g') else PLEXVERSION=$(pkg info -I ${PRDNAME}${PKG_ARG} | awk '{print $1}' | cut -d- -f3 | tr -d . | sed 's/_[0-9]//g') fi } default_config() { # Generate default configuration. if [ ! -f "${CWDIR}${PLEXCONF}" ]; then touch ${CWDIR}${PLEXCONF} fi sysrc -f ${CWDIR}${PLEXCONF} PLEX_CHANNEL=${PLEXCH} PLEX_ENABLE=NO GUI_ENABLE=YES INSTALL_DIR=${CWDIR} >/dev/null 2>&1 } config_reset() { if [ -f "${CWDIR}${PLEXCONF}" ]; then echo "Removing stale extension config and gui files..." FILELIST="${CWDIR}/conf ${WWWPATH}/plex-gui.php ${WWWPATH}/plex-gui-lib.inc ${WWWPATH}/plex-gui-lib.php ${WWWPATH}/plex-maintain-gui.php ${WWWPATH}/ext/plex-gui ${USRLOCALPATH}/locale-plex ${PLEXCONFLINK} ${CWDIR}/version ${CWDIR}/plexversion" for file in ${FILELIST}; do if [ -f "${file}" ] || [ -d "${file}" ]; then rm -rf ${file} fi done echo "Please run 'plexinit' to restore default configuration." exit 0 fi } plexdata_backup() { if [ -f "${CWDIR}${PLEXCONF}" ]; then BACKUPDIR=$(sysrc -f ${CWDIR}${PLEXCONF} -n BACKUP_DIR) if [ -z "${BACKUPDIR}" ]; then echo "Backup directory is not configured." exit 1 elif [ ! -d "${BACKUPDIR}" ]; then mkdir -p ${BACKUPDIR} fi if [ -d "${BACKUPDIR}" ]; then echo "Plexdata backup in progress..." cd ${CWDIR} tar -cf plexdata-`date +%Y-%m-%d-%H%M%S`.tar plexdata && mv plexdata-*.tar ${BACKUPDIR} if [ 0 -eq "$?" ]; then MSG="Plexdata backup created successfully." echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} logger -t ${SCRIPTNAME} ${MSG} exit 0 else MSG="A problem has occurred during the backup process." echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} error_notify ${MSG} fi else error_notify "Backup directory not found." fi fi } plexdata_rename() { if [ -d "${CWDIR}/plexdata" ]; then mv ${CWDIR}/plexdata ${CWDIR}/plexdata-`date +%Y-%m-%d-%H%M%S` if [ 0 -ne "$?" ]; then MSG="A problem has occurred while attempting to rename previous plexdata directory." echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} error_notify ${MSG} exit 1 fi fi } plexdata_restore() { if [ -f "${CWDIR}${PLEXCONF}" ]; then BACKUPDIR=$(sysrc -f ${CWDIR}${PLEXCONF} -n BACKUP_DIR) if [ -z "${BACKUPDIR}" ]; then echo "Backup directory is not configured." exit 1 elif [ ! -d "${BACKUPDIR}" ]; then mkdir -p ${BACKUPDIR} fi if [ -f "${BACKUPDIR}/${TARFILE}" -o -f "${TARFILE}" -o -d "${CWDIR}/${TARFILE}" -o -d "${TARFILE}" ]; then echo "Plexdata restore in progress..." if [ -f "${BACKUPDIR}/${TARFILE}" -o -f "${TARFILE}" ]; then if echo ${TARFILE} | grep -qw 'plexdata-[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}.tar'; then plexdata_rename cd ${BACKUPDIR} tar -xf ${TARFILE} -C ${CWDIR}/ else echo "Invalid plexdata tar file specified." exit 1 fi elif [ -d "${CWDIR}/${TARFILE}" -o -d "${TARFILE}" ]; then if echo ${TARFILE} | grep -qw 'plexdata-[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}'; then plexdata_rename cd ${CWDIR} mv ${TARFILE} plexdata else echo "Invalid plexdata directory specified." exit 1 fi fi if [ 0 -eq "$?" ]; then MSG="Plexdata restored successfully" echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} logger -t ${SCRIPTNAME} ${MSG} if ps -acx | grep -q "Plex Media Server"; then service ${CMDNAME}${CMD_ARG} onerestart 2> /dev/null || ${PLEXRCD}${CMD_ARG} onerestart fi exit 0 else MSG="A problem has occurred during the restore process." echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} error_notify ${MSG} fi else error_notify "Plexdata directory/file not found." fi fi } update_freebsdconf() { # Update FreeBSD.conf to fetch latest packages. if [ -f "${PKGCONF}" ]; then if grep -q "quarterly" ${PKGCONF}; then sed -i '' -E "s/quarterly/latest/g" ${PKGCONF} || \ error_notify "Error: A problem has occurred while updating FreeBSD.conf file." fi fi } plex_initial_download() { # Update FreeBSD.conf file. update_freebsdconf # Check if Plex already exist. if [ ! -f "${PLEXPATH}${PKG_ARG}/${CMDFILE}" ]; then # Fetch and install latest plex package. if [ -f "${CWDIR}/plexversion" ]; then # Remove previous version file from tarball upgrades. rm -f ${CWDIR}/plexversion fi pkg install -y ${PRDNAME}${PKG_ARG} # Re-check if Plex diretory/application exist. if [ ! -f "${PLEXPATH}${PKG_ARG}/${CMDFILE}" ]; then error_notify "Error: A problem has occurred while fetching/locating plexmediaserver." fi fi # Backup latest plex rcd after install if embedded. if [ "${PRDPLATFORM}" = "x64-embedded" ]; then if [ -f "${PLEXRCD}${CMD_ARG}" ]; then cp ${PLEXRCD}${CMD_ARG} ${CWDIR}/system/rc.d/ || \ error_notify "Error: A problem has occurred while copying rcd file." fi fi } pkg_upgrade() { if [ ! -f "${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/${CMDFILE}" ]; then echo "${FULLPRDNAME} package it not installed." fi if pkg info -I ${PRDNAME}${PKG_ARG} >/dev/null 2>&1; then # Update FreeBSD.conf file. update_freebsdconf # Check if freebsd.org is reachable and prevent for long connection waiting. if ping -c1 ${PINGHOST} > /dev/null 2>&1; then pkg update else echo "Error: ${PINGHOST} is unreachable, Please check your Internet connection." exit 1 fi # Get current plex version. get_plex_version # Check for latest plex packages for upgrade. if ! pkg upgrade -n ${PRDNAME}${PKG_ARG} > /dev/null 2>&1; then # Stop current plex instance. pre_upgrade_cmd if [ -f "${CWDIR}/plexversion" ]; then echo "Removing stale files from previous tarball upgrade..." # Remove previous version file from tarball upgrades. rm -rf ${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/* rm -f ${CWDIR}/plexversion pkg upgrade -y -f ${PRDNAME}${PKG_ARG} else pkg upgrade -y ${PRDNAME}${PKG_ARG} fi else echo "${FULLPRDNAME} package is up to date." fi # Get new plex version. if [ -z "${PKG_ARG}" ]; then PLEXNEWVERSION=$(pkg info -I ${PRDNAME}${PKG_ARG} | awk '{print $1}' | cut -d- -f2 | tr -d . | sed 's/_[0-9]//g') else PLEXNEWVERSION=$(pkg info -I ${PRDNAME}${PKG_ARG} | awk '{print $1}' | cut -d- -f3 | tr -d . | sed 's/_[0-9]//g') fi # Logging on upgrade success. if [ "${PLEXNEWVERSION}" -gt "${PLEXVERSION}" ]; then PLEXNEWVER=$(pkg info -I ${PRDNAME}${PKG_ARG} | awk '{print $1}' | cut -d- -f2) echo "${DATE}: ${FULLPRDNAME} upgraded to ${PLEXNEWVER}" >> ${PLEXLOGFILE} echo "${FULLPRDNAME} package upgrade completed!" fi # Backup latest plex rcd after upgrade if embedded. if [ "${PRDPLATFORM}" = "x64-embedded" ]; then if [ -f "${PLEXRCD}${CMD_ARG}" ]; then cp ${PLEXRCD}${CMD_ARG} ${CWDIR}/system/rc.d/ || \ error_notify "Error: A problem has occurred while copying plex rc file." fi fi else echo "This setup requires latest ${FULLPRDNAME} tarball for upgrades." fi # Check and upgrade extension package. ext_upgrade # Check and start plex after upgrade. post_upgrade_cmd } pre_upgrade_cmd() { PENDING_START=0 if ps -acx | grep -q "Plex Media Server"; then service ${CMDNAME}${CMD_ARG} onestop 2> /dev/null || ${PLEXRCD}${CMD_ARG} onestop PENDING_START=1 fi } post_upgrade_cmd() { plex_usergroup if ! ps -acx | grep -q "Plex Media Server"; then if [ -n "${PENDING_START}" ]; then if [ "${PENDING_START}" -eq 1 ]; then service ${CMDNAME}${CMD_ARG} onestart 2> /dev/null || ${PLEXRCD}${CMD_ARG} onestart fi fi fi } ext_upgrade() { # Check and upgrade extension package. if [ -f "${CWDIR}/version" ]; then extension_upgrade else ext_initial_download fi } extract_tarball() { # Alternative Plex Media Server manual upgrades from official tarball files. # This will overwrite/override the current Plex installation files and the # pkg utility will still reference for the older package version from pkg info # as expected, so we will create a plexversion file for the extension reference. TOPIC="upgrade" if [ ! -f "${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/${CMDFILE}" ]; then TOPIC="install" echo "Creating directory for ${PRDNAME}${PKG_ARG}." mkdir -p ${USRLOCALPATH}/${PRDNAME}${PKG_ARG} fi PLEXTARBALL=$(ls ${CWDIR} | egrep -io 'PlexMediaServer-([1-9]{1,}\.)+[0-9]{1,}.*-freebsd-amd64.tar.bz2' | tail -n1) if [ -n "${PLEXTARBALL}" ]; then echo "${FULLPRDNAME} tarball file found, trying to ${TOPIC} from file..." # Stop current plex instance. pre_upgrade_cmd echo "Extracting ${PLEXTARBALL}..." EXCLUDE="--exclude=start.sh" tar ${EXCLUDE} -xf ${CWDIR}/${PLEXTARBALL} --strip-components 1 -C ${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/ if [ 0 -eq "$?" ]; then # Set new version on success. SETVERSION=$(echo "${PLEXTARBALL}" | cut -d'-' -f2) echo "${PRDNAME}${PKG_ARG}-${SETVERSION} ${FULLPRDNAME} component" > ${CWDIR}/plexversion MSG="${FULLPRDNAME} tarball extracted successfully." echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} logger -t ${SCRIPTNAME} ${MSG} rm ${CWDIR}/${PLEXTARBALL} if [ ! -L "${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/${CMDFILE}" ]; then ln -fs "${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/${FULLPRDNAME}" "${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/${CMDFILE}" if [ "${PRDPLATFORM}" = "x64-embedded" ]; then if [ -f "${CWDIR}/conf/rc.d/${CMDNAME}${CMD_ARG}" ]; then cp ${CWDIR}/conf/rc.d/${CMDNAME}${CMD_ARG} ${CWDIR}/system/rc.d/${CMDNAME}${CMD_ARG} plex_rcd_link fi elif [ "${PRDPLATFORM}" = "x64-full" ]; then if [ -f "${CWDIR}/conf/rc.d/${CMDNAME}${CMD_ARG}" ]; then cp ${CWDIR}/conf/rc.d/${CMDNAME}${CMD_ARG} ${PLEXRCD}${CMD_ARG} fi fi fi # Check and start plex after upgrade. post_upgrade_cmd exit 0 else MSG="Error: A problem has occurred during tarball file extraction." echo "${DATE}: ${MSG}" | tee -a ${PLEXLOGFILE} error_notify ${MSG} fi else echo "${FULLPRDNAME} tarball file not found!" exit 1 fi } install_pkg() { plex_initial_download } extension_upgrade() { # Check if local .zip achive exist then auto-upgrade extension files, otherwise perform online upgrade. echo "Looking for new ${APPNAME} package!" if [ -f "${CWDIR}/*${BRANCH}.zip" ]; then echo "New ${APPNAME} package found, performing upgrade..." mkdir -p ${CWDIR}/update tar -xf ${CWDIR}/*${BRANCH}.zip --exclude='.git*' --strip-components 1 -C ${CWDIR}/update chmod +x ${CWDIR}/update/plex/${SCRIPTNAME} cp -Rf ${CWDIR}/update/plex/* ${CWDIR}/ rm -f ${CWDIR}/*${BRANCH}.zip rm -R ${CWDIR}/update update_guifiles # Logging the update event. UPDATEVERSION=$(cat ${CWDIR}/version) echo "${DATE}: ${APPNAME} upgraded to ${UPDATEVERSION}" >> ${PLEXLOGFILE} echo "${APPNAME} upgraded to version ${UPDATEVERSION}" echo "${APPNAME} package upgrade completed!" else # Perform an online extension upgrade. mkdir -p ${CWDIR}/update fetch -ao ${CWDIR}/update --no-verify-peer --timeout=30 ${VERFILE} || \ error_notify "Error: A problem has occurred while fetching version file." # Compare version files and fetch latest package if available. if [ -f "${CWDIR}/update/version" ]; then UPDATEVER=$(cat ${CWDIR}/update/version | tr -d .) CURRENTVER=$(cat ${CWDIR}/version | tr -d .) if [ "${UPDATEVER}" -gt "${CURRENTVER}" ]; then echo "New ${APPNAME} package found, performing upgrade..." fetch -ao ${CWDIR}/update --no-verify-peer --timeout=30 ${GITURL} || \ error_notify "Error: A problem has occurred while fetching extension package." tar -xf ${CWDIR}/update/*${BRANCH}.zip --exclude='.git*' --strip-components 1 -C ${CWDIR}/update chmod +x ${CWDIR}/update/plex/${SCRIPTNAME} cp -Rf ${CWDIR}/update/plex/* ${CWDIR}/ rm -R ${CWDIR}/update update_guifiles # Logging the update event. UPDATEVERSION=$(cat ${CWDIR}/version) echo "${DATE}: ${APPNAME} upgraded to ${UPDATEVERSION}" >> ${PLEXLOGFILE} echo "${APPNAME} upgraded to version ${UPDATEVERSION}" echo "${APPNAME} package upgrade completed!" else echo "${APPNAME} is on the latest version!" rm -R ${CWDIR}/update fi fi fi } ext_initial_download() { # Always ensure the version file is present, otherwise update the extension files on startup. if [ ! -f "${CWDIR}/version" ]; then mkdir -p ${CWDIR}/update fetch -ao ${CWDIR}/update --no-verify-peer --timeout=30 ${GITURL} || \ error_notify "Error: A problem has occurred while fetching extension package." tar -xf ${CWDIR}/update/${BRANCH}.zip --exclude='.git*' --strip-components 1 -C ${CWDIR}/update chmod +x ${CWDIR}/update/plex/${SCRIPTNAME} cp -Rf ${CWDIR}/update/plex/* ${CWDIR}/ rm -R ${CWDIR}/update fi } gui_obsolete_files() { # Plex Extension no longer copy the files into www, we will symlink the files instead. OBSOLETE_FILES="ext/plex-gui plex-gui.php plex-gui-lib.inc plex-maintain-gui.php" for file in ${OBSOLETE_FILES}; do if [ -d "${WWWPATH}/${file}" ] || [ -f "${WWWPATH}/${file}" ]; then rm -rf ${WWWPATH}/${file} fi done } update_guifiles() { # Check if gui enabled then copy/overwrite new files to www. GUI_STATUS=$(sysrc -f ${CWDIR}${PLEXCONF} -qn GUI_ENABLE) if [ "${GUI_STATUS}" = "YES" ]; then gui_obsolete_files # Ensure this directory exist if [ ! -d "${WWWPATH}/ext" ]; then mkdir ${WWWPATH}/ext fi ln -fhs ${CWDIR}/gui/ext/plex-gui ${WWWPATH}/ext/ || error_notify "Error: A problem has occurred while copying extension gui files." ln -fhs ${CWDIR}/gui/plex-gui.php ${WWWPATH}/ || error_notify "Error: A problem has occurred while copying extension gui files." ln -fhs ${CWDIR}/gui/plex-maintain-gui.php ${WWWPATH}/ || error_notify "Error: A problem has occurred while copying extension gui files." fi } product_check() { # Check for the working product. if [ "${PRODUCT}" = "NAS4FREE-x64" ] || [ "${PRODUCT}" = "XIGMANAS-x64" ]; then postinit_cmd gui_start fi } platform_check() { # Check for working platform. if [ "${PRDPLATFORM}" = "x64-embedded" ]; then pkg_symlinkdir compat_symlinkdir plex_symlinkdir plex_rcd_link fi } postinit_cmd() { # Check and generate temporary php script for postinit command. if ! grep -qw ${CWDIR}/${SCRIPTNAME} ${CONFIG}; then touch ${CWDIR}/postinit || error_notify "Error: A problem has occurred while creating the postinit file." chmod +x ${CWDIR}/postinit if [ ! "${PRDVERSION}" -ge "110" ]; then # Generate php script for previous versions. cat << EOF > ${CWDIR}/postinit EOF else # Generate php script for XigmaNAS 11.x versions. cat << EOF > ${CWDIR}/postinit EOF fi # Set standard plex pkg for informational purposes. if [ -z "${PKG_ARG}" ]; then PLEX_CHANNEL="standard" fi # Execute temporary php script. if [ "${OBI_INSTALL}" != "ON" ]; then php-cgi -f ${CWDIR}/postinit && rm ${CWDIR}/postinit fi # Do not start Plex by default. sysrc -f ${CWDIR}${PLEXCONF} PLEX_ENABLE=NO GUI_ENABLE=YES INSTALL_DIR=${CWDIR} >/dev/null 2>&1 fi } gui_start() { # Initialize the extension gui. if [ -d "${CWDIR}/gui" ]; then # Always ensure the config directory/file exist. if [ ! -f "${CWDIR}${PLEXCONF}" ]; then # Try to restore default configuration. runtime_config fi GUI_STATUS=$(sysrc -f ${CWDIR}${PLEXCONF} -qn GUI_ENABLE) if [ "${GUI_STATUS}" = "YES" ]; then # Store the installation path and link conf. if ! sysrc -f ${CWDIR}${PLEXCONF} -n INSTALL_DIR | grep -q "${CWDIR}"; then sysrc -f ${CWDIR}${PLEXCONF} INSTALL_DIR=${CWDIR} >/dev/null 2>&1 fi mkdir -p ${PLEXCONFLINK} # Ensure this directory exist if [ ! -d "${WWWPATH}/ext" ]; then mkdir ${WWWPATH}/ext fi ln -Ffhs ${CWDIR}/conf ${PLEXCONFLINK}/conf # Copy the gui files. gui_obsolete_files ln -fhs ${CWDIR}/gui/ext/plex-gui ${WWWPATH}/ext/ || error_notify "Error: A problem has occurred while copying extension gui files." ln -fhs ${CWDIR}/gui/plex-gui.php ${WWWPATH}/ || error_notify "Error: A problem has occurred while copying extension gui files." ln -fhs ${CWDIR}/gui/plex-maintain-gui.php ${WWWPATH}/ || error_notify "Error: A problem has occurred while copying extension gui files." fi fi } gui_enable() { # Relink conf and copy the gui files. if [ -d "${CWDIR}/gui" ]; then mkdir -p ${PLEXCONFLINK} # Ensure this directory exist if [ ! -d "${WWWPATH}/ext" ]; then mkdir ${WWWPATH}/ext fi ln -Ffhs ${CWDIR}/conf ${PLEXCONFLINK}/conf sysrc -f ${CWDIR}${PLEXCONF} GUI_ENABLE=YES >/dev/null 2>&1 ln -fhs ${CWDIR}/gui/ext/plex-gui ${WWWPATH}/ext/ || error_notify "Error: A problem has occurred while copying extension gui files." ln -fhs ${CWDIR}/gui/plex-gui.php ${WWWPATH}/ || error_notify "Error: A problem has occurred while copying extension gui files." ln -fhs ${CWDIR}/gui/plex-maintain-gui.php ${WWWPATH}/ || error_notify "Error: A problem has occurred while copying extension gui files." exit 0 else error_notify "Error: Extension gui files not found." fi } gui_disable() { # Disable gui if -t option specified. if [ -d "${CWDIR}/gui" ]; then rm -f ${WWWPATH}/plex-gui.php rm -f ${WWWPATH}/plex-gui-lib.inc rm -f ${WWWPATH}/plex-gui-lib.php rm -f ${WWWPATH}/plex-maintain-gui.php rm -Rf ${WWWPATH}/ext/plex-gui rm -f ${USRLOCALPATH}/locale-plex rm -Rf ${PLEXCONFLINK} sysrc -f ${CWDIR}${PLEXCONF} GUI_ENABLE=NO >/dev/null 2>&1 || error_notify "Error: A problem while removing extension gui files." exit 0 else error_notify "Error: Extension gui files not found." fi # Remove empty ext folder to prevent empty "Extensions" tab. if [ -d "${WWWPATH}/ext" ]; then if [ ! "$(ls -A ${WWWPATH}/ext)" ]; then rm -R ${WWWPATH}/ext fi fi } plex_usergroup() { # Check and create plex user/group. if ! pw groupshow plex >/dev/null 2>&1; then pw groupadd plex -g 972 >/dev/null 2>&1 fi if ! pw usershow plex >/dev/null 2>&1; then pw useradd plex -u 972 -g 972 -c "Plex Media Server" -d /nonexistent -s /usr/sbin/nologin >/dev/null 2>&1 fi } plex_rc_params() { # Plex Media Server rc parameters. if ! sysrc -f ${RCCONF} -qn ${CMDNAME}${CMD_ARG}_enable | grep -q "NO"; then sysrc ${CMDNAME}${CMD_ARG}_enable="NO" >/dev/null 2>&1 fi if ! sysrc -f ${RCCONF} -qn ${CMDNAME}${CMD_ARG}_support_path | grep -q "${SUPPORTPATH}"; then sysrc ${CMDNAME}${CMD_ARG}_support_path="${SUPPORTPATH}" >/dev/null 2>&1 fi if ! sysrc -f ${RCCONF} -qn ${CMDNAME}${CMD_ARG}_maxplugins | grep -q "6"; then sysrc ${CMDNAME}${CMD_ARG}_maxplugins="6" >/dev/null 2>&1 fi } pkg_symlinkdir() { # Check and create/relink required symlinks/dirs for package cache and db. # This may be already performed by any prevously installed Extension, # supporting this aproach, in that case will reuse the existing environment. if [ -d "${PKGCACHE}" ]; then if [ ! -L "${PKGCACHE}" ]; then rm -R ${PKGCACHE} mkdir -p ${CWDIR}/system/pkg/cache ln -Ffhs ${CWDIR}/system/pkg/cache ${PKGCACHE} fi else mkdir -m 0755 -p /var/cache mkdir -p ${CWDIR}/system/pkg/cache ln -Ffhs ${CWDIR}/system/pkg/cache ${PKGCACHE} fi if [ -d "${PKGDB}" ]; then if [ ! -L "${PKGDB}" ]; then rm -R ${PKGDB} mkdir -p ${CWDIR}/system/pkg/db ln -Ffhs ${CWDIR}/system/pkg/db ${PKGDB} fi else mkdir -p ${CWDIR}/system/pkg/db ln -Ffhs ${CWDIR}/system/pkg/db ${PKGDB} fi } compat_symlinkdir() { # Check and create/relink required symlinks/dirs for compat libs. # This is for backward compatibility only. if [ -f "${USRLOCALPATH}/${PRDNAME}${PKG_ARG}/${CMDFILE}" ]; then if [ ! -f "${CWDIR}/plexversion" ]; then get_plex_version if [ "${PLEXVERSION}" -lt "${PLEXMINVER}" ]; then if [ ! -d "${COMPATPATH1}" ]; then mkdir -m 0755 -p /usr/local/lib mkdir -p ${CWDIR}/system/compat/lib/compat ln -Ffhs ${CWDIR}/system/compat/lib/compat ${COMPATPATH1} fi if [ ! -d "${COMPATPATH2}" ]; then mkdir -m 0755 -p /usr/local/lib32 mkdir -p ${CWDIR}/system/compat/lib32/compat ln -Ffhs ${CWDIR}/system/compat/lib32/compat ${COMPATPATH2} fi if [ ! -d "${COMPATPATH3}" ]; then mkdir -m 0755 -p /usr/local/libdata mkdir -p ${CWDIR}/system/compat/libdata/ldconfig ln -Ffhs ${CWDIR}/system/compat/libdata/ldconfig ${COMPATPATH3} fi if [ ! -d "${COMPATPATH4}" ]; then mkdir -m 0755 -p /usr/local/libdata mkdir -p ${CWDIR}/system/compat/libdata/ldconfig32 ln -Ffhs ${CWDIR}/system/compat/libdata/ldconfig32 ${COMPATPATH4} fi fi fi fi } plex_symlinkdir() { # Check and create/relink required symlinks/dirs for plexmediaserver. if [ ! -d "${RCDPATH}" ]; then mkdir -m 0755 -p ${RCDPATH} fi if [ -d "${CWDIR}/${PRDNAME}${PKG_ARG}" ]; then mv ${CWDIR}/${PRDNAME}${PKG_ARG} ${CWDIR}/system/${PRDNAME}${PKG_ARG} fi if [ ! -d "${PLEXPATH}${PKG_ARG}" ]; then mkdir -p ${CWDIR}/system/${PRDNAME}${PKG_ARG} ln -Ffhs ${CWDIR}/system/${PRDNAME}${PKG_ARG} ${USRLOCALPATH}/${PRDNAME}${PKG_ARG} fi if [ ! -d "${CWDIR}/system/rc.d" ]; then mkdir -p ${CWDIR}/system/rc.d fi } plex_rcd_link() { # Restore current plex rcd if embedded. if [ "${PRDPLATFORM}" = "x64-embedded" ]; then if [ -f "${CWDIR}/system/rc.d/${CMDNAME}${CMD_ARG}" ]; then cp ${CWDIR}/system/rc.d/${CMDNAME}${CMD_ARG} ${RCDPATH}/ || error_notify "Error: A problem has occurred while copying rcd file." fi fi } ext_enable_disable() { # Start plex extension gui if enabled otherwise start plex cli version. if [ -d "${CWDIR}/gui" ]; then GUI_STATUS=$(sysrc -f ${CWDIR}${PLEXCONF} -qn GUI_ENABLE) if [ "${GUI_STATUS}" = "YES" ]; then PLEX_STATUS=$(sysrc -f ${CWDIR}${PLEXCONF} -qn PLEX_ENABLE) if [ "${PLEX_STATUS}" = "NO" ]; then exit 0 fi else break fi fi } get_versions() { echo "${APPNAME} ${APPVERSION}" if [ -f "${CWDIR}/plexversion" ]; then VERSION=$(cat ${CWDIR}/plexversion) echo "${VERSION}" else pkg info -I ${PRDNAME} 2> /dev/null || pkg info -I ${PRDNAME}-plexpass 2> /dev/null || \ echo "Can not determine plex version." fi exit 0 } plex_log_start() { # Log script startup. if [ "$?" -eq 0 ]; then MSG="script has been started successfully!" logger -t ${SCRIPTNAME} ${MSG} else MSG="script started with faults" logger -t ${SCRIPTNAME} ${MSG} fi # Auto start Plex Media Server if enabled. if ! ps -acx | grep -q "Plex Media Server"; then PLEX_STATUS=$(sysrc -f ${CWDIR}${PLEXCONF} -qn PLEX_ENABLE) if [ "${PLEX_STATUS}" = "YES" ]; then plex_start fi fi } plex_start() { # Workaround to check and restore plex user/group if missing. # Seems that some built-in services resets/rebuild the passwd file # without respecting the third-party generated entries through the shell. plex_usergroup # Start Plex Media Server. service ${CMDNAME}${CMD_ARG} onestart 2> /dev/null || ${PLEXRCD}${CMD_ARG} onestart if [ "$?" -eq 0 ]; then exit 0 else exit 1 fi } plex_stop() { # Workaround to check and restore plex user/group if missing. # Seems that some built-in services resets/rebuild the passwd file # without respecting the third-party generated entries through the shell. plex_usergroup # Stop Plex Media Server. service ${CMDNAME}${CMD_ARG} onestop 2> /dev/null || ${PLEXRCD}${CMD_ARG} onestop if [ "$?" -eq 0 ]; then plex_rmpid exit 0 else exit 1 fi } plex_restart() { # Workaround to check and restore plex user/group if missing. # Seems that some built-in services resets/rebuild the passwd file # without respecting the third-party generated entries through the shell. plex_usergroup # Restart Plex Media Server. service ${CMDNAME}${CMD_ARG} onerestart 2> /dev/null || ${PLEXRCD}${CMD_ARG} onerestart if [ "$?" -eq 0 ]; then exit 0 else exit 1 fi } plex_rmpid() { # Remove pid file if stop success. if ! ps -acx | grep -q "Plex Media Server"; then if [ -f "${PLEXPIDFILE}" ]; then rm -f ${PLEXPIDFILE} fi fi } plex_init() { echo "Initializing ${FULLPRDNAME}..." # Check for system compatibility. if [ ! "${PLATFORM}" = "amd64" ]; then echo "Unsupported platform!"; exit 1 fi # Check for product compatibility. if [ ! "${PRDVERSION}" -ge "110" ]; then echo "Unsupported version!"; exit 1 fi # Link plexinit to /usr/local/sbin. if [ ! -f "/usr/local/sbin/${SCRIPTNAME}" ]; then ln -fs ${CWDIR}/${SCRIPTNAME} /usr/local/sbin/ fi # Function calls. platform_check ext_initial_download plex_usergroup plex_rc_params product_check ext_enable_disable plex_log_start } # Run-time configuration. runtime_config # Plexinit common control switches. while getopts ":aogtsprxbfeivuUh" option; do case ${option} in [h]) echo "Usage: ${SCRIPTNAME} -[option] | [path|file]"; echo "Options:" echo " -s Start ${FULLPRDNAME}." echo " -p Stop ${FULLPRDNAME}." echo " -r Restart ${FULLPRDNAME}." echo " -u Upgrade Plex/Extension packages." echo " -U Upgrade Extension packages only." echo " -g Enables the addon GUI." echo " -t Disable the addon GUI." echo " -x Reset Plex Extension Config." echo " -b Backup Plexdata Directory." echo " -f Restore Plexdata Directory." echo " -e Install/Upgrade Plex package from tarball." echo " -i Install/Upgrade Plex package from pkg tool" echo " -v Display product version." echo " -h Display this help message."; exit 0 ;; [a]) OPT_INSTALL="1";; # Alternate installer option. [o]) OBI_INSTALL="ON";; # To prevent nested PHP-CGI call for installation with OBI. [g]) gui_enable; exit 0 ;; # For enable the addon gui. [t]) gui_disable; exit 0 ;; # For disable the addon gui. [s]) plex_start;; [p]) plex_stop;; [r]) plex_restart;; [x]) config_reset;; [b]) plexdata_backup;; [f]) plexdata_restore;; [e]) extract_tarball;; [i]) install_pkg;; [v]) get_versions;; [u]) pkg_upgrade; exit 0 ;; [U]) ext_upgrade; exit 0 ;; [?]) echo "Invalid option, -h for usage."; exit 1 ;; esac done # Set the requested plex channel during initial install. if [ "${OPT_INSTALL}" = 1 ]; then if [ ! -d "${CWDIR}/conf" ]; then mkdir -p ${CWDIR}/conf fi if [ ! -f "${CWDIR}${PLEXCONF}" ]; then touch ${CWDIR}${PLEXCONF} fi sysrc -f ${CWDIR}${PLEXCONF} PLEX_CHANNEL=plexpass >/dev/null 2>&1 plexpass_channel fi plex_init