#!/bin/sh
################################################################################
# While using "nano" to edit this script (nano /aioscript.sh),
# Use the up, down, left and right arrow keys to navigate. Once done editing,
# Press "X" while holding "Ctrl", Press "Y" then press "Enter" to save changes

# Get IP
jailip=$(ifconfig | grep -e "inet" -e "addr:" | grep -v "inet6" | grep -v "127.0.0.1" | head -n 1 | awk '{print $2}')
#Grab the date & time to be used later
date=$(date +"%Y.%m.%d-%I.%M%p")

# Add some colour!
nc='\033[0m'        # Default Text (No Formatting / No Color)
alt='\033[0;31m'    # Alert Text
emp='\033[1;31m'    # Emphasis Text
msg='\033[1;37m'    # Message Text
url='\033[1;32m'    # URL
qry='\033[0;36m'    # Query Text
ssep='\033[1;30m#----------------------#\033[0m'    # Small Line Separator
msep='\033[1;30m#--------------------------------------#\033[0m'    # Medium Line Separator
sep='\033[1;30m----------------------------------------------------------------------\033[0m'   # Line Separator
cmd='\033[1;35m'    # Command to be entered
fin='\033[0;32m'    # Green Text
inf='\033[0;33m'    # Information Text
ul='\033[4m'        # Underline Text
lbt='\033[1;34m'    # Light Blue Text
yt='\033[1;33m'     # Yellow Text
lct='\033[1;36m'    # Light Cyan Text
ca='\033[1;30m'     # Currently Unavailable (Dark Grey Text)

################################################################################
##### Debug
################################################################################

OPTIND=1         # Reset in case getopts has been used previously in the shell.

while getopts "dnvh" opt; do
      case $opt in
        h)	echo " "
			echo -e "${inf} -v ${msg}Run the script run in verbose mode${nc}"
			echo -e "${inf} -d ${msg}Print command traces before executing command${nc}"
			echo -e "${inf} -n ${msg}Read commands but do not execute them${nc}"
			echo " "
			exit 1;;
        d)	set -x;;
        n)	set -n;;
        v)	set -v;;
        *)  echo " "
			echo -e "${alt}        Invalid choice, please try again${nc}"
			echo " "
            exit 1;;

      esac
done
################################################################################
# Load config
#. $scriptPath/Streaming/Emby/emby.cfg
#------------------------------------------------------------------------------#
### EMBY SERVER UPDATE

update.emby.continue ()
{
echo -e "${msep}"
echo -e "${emp}   Press Enter To Continue${nc}"
echo -e "${msep}"
read -r -p " " response
case "$response" in
    *)
              ;;
esac
}


echo " "
echo -e "${sep}"
echo -e "${msg}   Emby Updater${nc}"
echo -e "${sep}"
echo " "
echo "Checking Versions.."
INSTALLED_VERSION=$(pkg info emby-server | grep Version | cut -d : -f2)
LATEST_RELEASE=$(curl -L -s -H 'Accept: application/json' https://github.com/MediaBrowser/Emby.Releases/releases/latest)
LATEST_VERSION=$(echo $LATEST_RELEASE | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
LATEST_BETA_RELEASE=$(curl -L -s -H 'Accept: application/json' https://api.github.com/repos/MediaBrowser/Emby.Releases/tags)
LATEST_BETA_VERSION=$(curl -L -s -H 'Accept: application/json' https://api.github.com/repos/MediaBrowser/Emby.Releases/tags | grep '"name":' |  sed -E 's/.*"([^"]+)".*/\1/' | head -n1)
RELEASE_URL="https://github.com/MediaBrowser/Emby.Releases/releases/download/${LATEST_BETA_VERSION}/emby-server-freebsd11_${LATEST_BETA_VERSION}_amd64.txz"
echo " "
echo "Latest Version: $LATEST_VERSION"
echo "Latest Beta:    $LATEST_BETA_VERSION"
echo "Installed:     $INSTALLED_VERSION"
echo " "
update.emby.continue

echo -e "${sep}"
echo -e "${msg} Updating packages..${nc}"
echo -e "${msg} (You may see some things get uninstalled/reinstalled here)${nc}"
echo -e "${sep}"
echo " "

pkg update
pkg upgrade -y
#pkg install -y emby-server # In case it gets uninstalled

echo " "

echo -e "${msg} Package updates done${nc}"

echo " "
echo -e "${sep}"
echo -e "${msg} Grab the update for Emby from github${nc}"
echo -e "${sep}"
echo " "

fetch --no-verify-peer -o /tmp/emby-${LATEST_VERSION}.txz ${RELEASE_URL}

echo " "
echo -e "${sep}"
echo -e "${msg} Download done, let's stop the server${nc}"
echo -e "${sep}"
echo " "

service emby-server stop

echo " "
echo -e "${sep}"
echo -e "${msg} Now to extract the download and replace old version${nc}"
echo -e "${sep}"
echo " "

pkg install -y /tmp/emby-$LATEST_VERSION.txz 

echo " "
echo -e "${sep}"
echo -e "${msg} And finally, start the server back up.${nc}"
echo -e "${sep}"
echo " "

service emby-server start

update.emby.continue
