#!/bin/bash

# Cannot get this CMake feature to work; fill in manually for now
#VIEWERNAME="@VIEWER_CHANNEL_BASE@" #This should be the viewer's canonical base name, e.g. Second Life, Firestorm, Alchemy, etc. Handled by cmake.

#CHANNELNAME="@VIEWER_CHANNEL_TYPE@ @VIEWER_PROJECT_CODENAME@" #Use cmake to autofill the channel type (alpha, beta, release), and any project codename.
VIEWERNAME="Alchemy" #This should be the viewer's canonical base name, e.g. Second Life, Firestorm, Alchemy, etc. Handled by cmake.
CHANNEL_TYPE=""
PROJECT_NAME=""

CHANNELNAME="${CHANNEL_TYPE} ${PROJECT_NAME}" #Use cmake to autofill the channel type (alpha, beta, release), and any project codename.

CHANNELNOBREAK=$(echo $CHANNELNAME | tr " " "-") #Store a version of the channel name without any spaces to prevent breakage with some functions. Not filled by cmake to ensure consistency in it's output.

SCRIPTSRC="$(readlink -f "$0" || echo "$0")"
RUN_PATH="$(dirname "${SCRIPTSRC}" || echo .)"

install_prefix=$(builtin cd "${RUN_PATH}/.." || exit; pwd) #This is a cleaner way of getting the absolute path for the install directory.

function print() {
	log_prefix="${VIEWERNAME} ${CHANNELNAME} Desktop Integration:"
	echo -e "${log_prefix} $*"
}


function install_desktop_entry()
{
	local installation_prefix="${1}"

	desktoppath="${installation_prefix}/${VIEWERNAME,,}-${CHANNELNOBREAK,,}-viewer.desktop"
	cat >"${desktoppath}" <<desktopfile || print "Warning: Could not write $desktoppath"s
[Desktop Entry]
Version=1.4
Name=${VIEWERNAME} ${CHANNELNAME}
GenericName=Second Life Viewer
Comment=Client for the Online Virtual World, Second Life
Exec=${installation_prefix}/${VIEWERNAME,,}
Icon=${VIEWERNAME}-${CHANNELNOBREAK}-Viewer
Terminal=false
Type=Application
Categories=Game;Simulation;
StartupNotify=true
StartupWMClass=${VIEWERNAME} ${CHANNELNAME}
PrefersNonDefaultGPU=true
Actions=DefaultGPU;AssociateMIME;

[Desktop Action DefaultGPU]
Exec=env __GLX_VENDOR_LIBRARY_NAME="" ${installation_prefix}/${VIEWERNAME,,}
Name=Launch on default GPU

[Desktop Action AssociateMIME]
Exec=${installation_prefix}/etc/register_secondlifeprotocol.sh
Name=Associate SLURLs
desktopfile

#The above adds some options when the shortcut is right-clicked, to launch on the default (usually integrated) GPU, and to force MIME type association.

#The "PrefersNonDefaultGPU" line should automatically run the viewer on the most powerful GPU in the system, if it is not default. If it is, this is ignored.

# NOTE: DO NOT CHANGE THE "GenericName" FIELD - ONLY CHANGE THE "Name" FIELD. (This is to ensure that searching "Second Life" will show all the viewers installed in a user's system, regardless of their canonical name.)

	print "Installing menu entries via XDG..."
	xdg-icon-resource install --novendor --size 256 "${install_prefix}/${VIEWERNAME,,}_icon.png" "${VIEWERNAME}"-"${CHANNELNOBREAK}"-Viewer
	#NOTE: Above command takes the path to the icon to install && The name of the icon to be used by XDG. This should always be in the format of "x-Viewer" to avoid potential naming conflicts, as per XDG spec.
	xdg-desktop-menu install --novendor "${installation_prefix}"/"${VIEWERNAME,,}"-"${CHANNELNOBREAK,,}"-viewer.desktop
}

CHANNELNAME=$(echo "${CHANNELNAME}" | xargs) #Trim leading and / or tailing whitespace from CHANNELNAME
if [ "$UID" == "0" ]; then
	# system-wide
	install_desktop_entry "${install_prefix}" /usr/local/share/applications
else
	# user-specific
	install_desktop_entry "${install_prefix}" "${HOME}/.local/share/applications"
fi
