#!/bin/bash

echo "Toon Boom Harmony Shortcut Fixer for Linux v1.0"
echo "Copyright (c) 2017 Toon Boom Animation Inc."
echo

function fatalError
{
   echo "${2}" 1>&2
   exit $1
}

function showHelp
{
	echo "This utility adds the full path to Harmony executables for all the Harmony installations that can be found on the machine. Note that it will use Harmony's default installation directory." | fold -sw 80
	echo
	echo "	-h|--help	Show this help."
	echo
}

if [ "$(id -u)" != "0" ]
then
	fatalError 1 "Please run this utility with elevated privileges."
fi

case $1 in
	-h|--help)
		showHelp
		exit 0
	;;
esac

function buildBinPath
{
	TBAPATH="/usr/local/ToonBoomAnimation/harmony${TBAEDITION}_${TBAVERSION}"
	while true 
	do 		
		if [[ -d "${TBAPATH}/lnx86_64/bin" ]]
		then
			TBA_BIN="${TBAPATH}/lnx86_64/bin"
			break
		elif [[ -d "${TBAPATH}/lnx86/bin" ]]
		then 	
			TBA_BIN="${TBAPATH}/lnx86/bin"
			break
		else
			echo "Could not find Harmony ${TBAEDITION} ${TBAVERSION} binaries in ${TBAPATH}."
			echo "Please enter the installation path for this version of Harmony, or input nothing to skip this version:" 
			read -p '' TBAPATH		
			if [[ $TBAPATH == '' ]]
			then 
				break
			fi
		fi
	done
}

function changeShortcutPath
{
	echo "Adding path $TBA_BIN to shortcut $1"
	# Fixes shortcuts pointing to HarmonyPlay instead of Play
	sed -i "s:^\(TryExec\|Exec\)=HarmonyPlay:\1=${TBA_BIN}/Play:" $1
	# Adds the path specified in parameter 1 to the shortcut's command
	sed -i "s:^\(TryExec\|Exec\)=\(/.*/\)\?:\1=${TBA_BIN}/:" $1
}	

IFS=$'\n'

echo "This utility will do the following:

- Make all Toon Boom Harmony shortcuts point to the full path of their respective application, for all versions of Harmony.
- Give an unique file name to each shortcut so that they are processed differently.
- Organize the shortcuts in a GNOME 3-compatible structure. 

If shortcuts for a version of Harmony are found, but that version of Harmony is not installed in its default installation folder, you will be prompted to enter the path to this Harmony installation.
" | fold -sw 80

read -p "Would you like to continue? [yes/no] "

if [[ $REPLY =~ [yY]([eE][sS])? ]] 
then 
	# Move legacy shortcuts to the proper location.
	for i in $(find /usr/share/applnk/ -name "ToonBoom*" -type d | sort)
	do
		echo "Processing shortcuts in ${i}..."
		[[ $i =~ ^/.*/ToonBoom-Harmony_?(Advanced|Premium)?_([0-9\.]+)$ ]]
		TBAEDITION=${BASH_REMATCH[1]}
		TBAVERSION=${BASH_REMATCH[2]}
		buildBinPath
		
		cat > /etc/xdg/menus/applications-merged/ToonBoom_Harmony_${TBAEDITION}_${TBAVERSION}.menu <<END_MENU		
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 0.8//EN"
       "http://www.freedesktop.org/standards/menu-spec/menu-0.8.dtd">
<Menu>
   <Name>Applications</Name>
   <Menu>
       <Name>Harmony ${TBAVERSION} ${TBAEDITION}</Name>
       <Directory>ToonBoom_Harmony_${TBAEDITION}_${TBAVERSION}.directory</Directory>
       <Include>
END_MENU
		
		echo "These shortcuts will point to executables in ${TBA_BIN}."
		for j in $(find $i/ -name "*.desktop" -type f)
		do
			SHORTCUTNAME="$j"
			echo "Processing shortcut $SHORTCUTNAME"
			changeShortcutPath "$SHORTCUTNAME"
			# Add version number to filename so that each shortcut is considered different by the desktop environment.
			[[ ${SHORTCUTNAME} =~ ^(/.*/ToonBoom_Harmony)(_Advanced|_Premium)?(_[0-9\.]+|)(-[A-Za-z]+|)(\.desktop)$ ]]
			NEWSHORTCUTNAME="${BASH_REMATCH[1]}${BASH_REMATCH[2]}_${TBAVERSION}${BASH_REMATCH[4]}.desktop"
			mv -v "$SHORTCUTNAME" "$NEWSHORTCUTNAME"

			if [[ ! -z ${BASH_REMATCH[3]} ]]
			then
				echo "Categories=AudioVideo;" >> "$NEWSHORTCUTNAME"
			fi
			chmod 644 "$NEWSHORTCUTNAME"
			mv -v "$NEWSHORTCUTNAME" /usr/share/applications/
			NEWSHORTCUTFILENAME="$(basename $NEWSHORTCUTNAME)"
			echo "          <Filename>$NEWSHORTCUTFILENAME</Filename>" >> /etc/xdg/menus/applications-merged/ToonBoom_Harmony_${TBAEDITION}_${TBAVERSION}.menu
		done
		echo "Creating desktop directory for Harmony $TBAVERSION $TBAEDITION..."

		cat >> /etc/xdg/menus/applications-merged/ToonBoom_Harmony_${TBAEDITION}_${TBAVERSION}.menu <<END_MENU
       </Include>
   </Menu>
</Menu>
END_MENU
		cat > /usr/share/desktop-directories/ToonBoom_Harmony_${TBAEDITION}_${TBAVERSION}.directory <<END_DIRECTORY
[Desktop Entry]
Name=Harmony ${TBAVERSION} ${TBAEDITION}
Comment=Harmony ${TBAVERSION} ${TBAEDITION}
Icon=/usr/share/pixmaps/ToonBoom-Harmony_${TBAEDITION}.png
Type=Directory
Encoding=UTF-8
END_DIRECTORY
		rmdir "$i"
	done

	# Process Harmony 14 shortcuts
	echo "Processing shortcuts in /usr/share/applications/"
	for TBAEDITION in Advanced Premium 
	do
		TBAVERSION=''
		for j in $(find /usr/share/applications/ -name "ToonBoom_Harmony_${TBAEDITION}_*.desktop" | sort)  # Process shortcuts by order or version.
		do
			echo $j
			SHORTCUTNAME="$j"
			[[ $SHORTCUTNAME =~ ^(/.*/ToonBoom_Harmony)(_Advanced|_Premium)?_([0-9\.]+)(-[A-Za-z]+)?(\.desktop)$ ]]					
			if [[ $TBAVERSION != "${BASH_REMATCH[3]}" ]]
			then
				TBAVERSION="${BASH_REMATCH[3]}"				
				TBAMAJOR=${TBAVERSION%%.*}
				if [[ $TBAMAJOR == 14 ]]	
				then 
					echo "Processing shortcuts for Harmony ${TBAVERSION} ${TBAEDITION}..."
					buildBinPath
					echo "These shortcuts will point to executables in ${TBA_BIN}."
				fi 
			fi 

			if [[ $TBAMAJOR == 14 ]]
			then 				
				echo "Processing shortcut $j"
				changeShortcutPath "$SHORTCUTNAME"

				# Remove category unless it's the main application shortcut.
				if [[ ! ${SHORTCUTNAME} =~ ^(/.*/ToonBoom_Harmony)(_Advanced|_Premium)?_([0-9\.]+)\.desktop$ ]]
				then
					sed -i "/Categories=AudioVideo;/d" "${SHORTCUTNAME}"
				fi
			fi
		done
	done
	
	update-desktop-database
	echo "Done. Please reboot to ensure the changes are processed by your desktop environment."
fi
