#!/bin/bash

AUDIO_PACKAGE_FOLDER="/var/packages/${SYNOPKG_PKGNAME}"
AUDIO_CONF_FOLDER="${AUDIO_PACKAGE_FOLDER}/etc"
AUDIO_CONF="${AUDIO_CONF_FOLDER}/audio.conf"
AUDIOUSER="${AUDIO_PACKAGE_FOLDER}/target/pkgbin/audiouser"

version_check() {
	local currentver="$1"
	local requiredver="6.5.5"
	if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
		false
		return
	else
		true
		return
	fi
}

UPGRADE_FILE="/tmp/synoaudio.upgrade"
rm -f ${UPGRADE_FILE}

# AudioStation #2761 - reindex all personal music library due to the voice_search table
. "$AUDIO_CONF"
if [ -z "$reindex_voice_search_table" ]; then
	$AUDIOUSER --reindex_all_user
	echo -e "reindex_voice_search_table=\"yes\"\\n" >> "$AUDIO_CONF"
fi

# AudioStation #3103 - update portal server uri
if [ -n "$SYNOPKG_OLD_PKGVER" ]; then
	OLD_VERSION=${SYNOPKG_OLD_PKGVER%%.-}
	if version_check "$OLD_VERSION"; then
		echo -e "reregister_alexa_oauth_client=\"yes\"\\n" >> "$AUDIO_CONF"
	fi
fi

exit 0
