#! /bin/sh

# this script sets your machine to a clean slate by removing everything that Maxon App has installed with the exception of C4D, Redshift and ZBrush.
# v5 deletes the _redgiantservice user and group names that were negelected in the previous versions
# v6 deletes the _mxappservie user and group names for Maxon App 2023.1.3 and later
# v7 removed the lines to delete the Library/Application Support/Reprise folder

# stop the MxNotify process

killall MxNotify

# uninstall Maxon App if the uninstaller can be found
if [ -f "/Library/Application Support/Maxon/Tools/uninstall-appmanager.sh" ]; then
	"/Library/Application Support/Maxon/Tools/uninstall-appmanager.sh"
else
	# if the uninstaller cannot be found, remove the Maxon App application
	sudo rm -rf /Applications/Maxon.app
fi

# uninstall Maxon App Service if the uninstaller can be found
if [ -f "/Library/Application Support/Maxon/Tools/uninstall-maxon-service.sh" ]; then
	"/Library/Application Support/Maxon/Tools/uninstall-maxon-service.sh"

# if the uninstaller cannot ne found, unload the service
else
	sudo launchctl unload /Library/LaunchDaemons/com.redgiant.service.plist >/dev/null
fi

# remove all the Maxon App related files and folders if they exist
if [ -d /Library/Application\ Support/Maxon ]; then
	rm -r /Library/Application\ Support/Maxon
fi

if [ -d /Library/Application\ Support/Red\ Giant ]; then
	rm -r /Library/Application\ Support/Red\ Giant
fi

if [ -d /Users/Shared/Maxon ]; then
	rm -r /Users/Shared/Maxon
fi

if [ -d /Users/Shared/Red\ Giant ]; then
	rm -r /Users/Shared/Red\ Giant
fi

if [ -d /Library/Logs/Maxon ]; then
	rm -r /Library/Logs/Maxon
fi

if [ -d /Library/Logs/Red\ Giant ]; then
	rm -r /Library/Logs/Red\ Giant
fi

if [ -d /$HOME/Library/Application\ Support/Maxon ]; then
	rm -r /$HOME/Library/Application\ Support/Maxon
fi

if [ -d /$HOME/Library/Application\ Support/Red\ Giant ]; then
	rm -r /$HOME/Library/Application\ Support/Red\ Giant
fi

if [ -d /$HOME/Library/Preferences/Red\ Giant ]; then
	rm -r /$HOME/Library/Preferences/Red\ Giant
fi

if [ -f /Library/LaunchAgents/com.maxon.mxnotify.agent.plist ]; then
	rm -rf /Library/LaunchAgents/com.maxon.mxnotify.agent.plist
fi

if [ -f /Library/LaunchDaemons/net.maxon.deployservice.plist ]; then
	rm -rf /Library/LaunchDaemons/net.maxon.deployservice.plist
fi

# remove all the Red Giant plugins and files for Adobe if they exist
if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Instant\ HD ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Instant\ HD
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Bez\ Friend ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Bez\ Friend
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Colorista ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Colorista
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Cosmo ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Cosmo
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Denoiser ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Denoiser
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Film ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Film
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Frames ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Frames
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Mojo ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Mojo
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Renoiser ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Magic\ Bullet\ Renoiser
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Maxon\ Studio ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Maxon\ Studio
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/MB\ Looks ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/MB\ Looks
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Red\ Giant\ Universe ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Red\ Giant\ Universe
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Red\ Giant\ VFX ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Red\ Giant\ VFX
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Trapcode ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/Trapcode
fi

if [ -d /Library/Application\ Support/Adobe/CEP/extensions/com.redgiant.pe4 ]; then
	rm -r /Library/Application\ Support/Adobe/CEP/extensions/com.redgiant.pe4
fi

if [ -d /Library/Application\ Support/Adobe/CEP/extensions/com.redgiant.colorista ]; then
	rm -r /Library/Application\ Support/Adobe/CEP/extensions/com.redgiant.colorista
fi

if [ -d /Library/Application\ Support/Adobe/CEP/extensions/com.redgiant.uni.dashboard ]; then
	rm -r /Library/Application\ Support/Adobe/CEP/extensions/com.redgiant.uni.dashboard
fi

if [ -d /Library/Application\ Support/LooksBuilder ]; then
	rm -r /Library/Application\ Support/LooksBuilder
fi

if [ -d /Library/Frameworks/Gpu-Shader-Engine.framework ]; then
	rm -r /Library/Frameworks/Gpu-Shader-Engine.framework
fi

if [ -d /Library/Frameworks/MBLooks4UI.framework ]; then
	rm -r /Library/Frameworks/MBLooks4UI.framework
fi

if [ -d /$HOME/Documents/Red\ Giant ]; then
	rm -r /$HOME/Documents/Red\ Giant
fi

if [ -d /$HOME/Library/Application\ Support/LooksBuilder ]; then
	rm -r /$HOME/Library/Application\ Support/LooksBuilder
fi

if [ -f /$HOME/Library/Logs/Red\ Giant\ Application\ Manager_debug.log ]; then
	rm -rf /$HOME/Library/Logs/Red\ Giant\ Application\ Manager_debug.log
fi

if [ -f /$HOME/Library/Logs/Red\ Giant ]; then
	rm -rf /$HOME/Library/Logs/Red\ Giant
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.application-manager.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.application-manager.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.PluralEyes-4-Helper.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.PluralEyes-4-Helper.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.PluralEyes-4.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.PluralEyes-4.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.Red-Giant-Neutrino-Helper-1-4-Renderer.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.Red-Giant-Neutrino-Helper-1-4-Renderer.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.Red-Giant-Neutrino-Helper-1-5-Renderer.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.Red-Giant-Neutrino-Helper-1-5-Renderer.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.Red-Giant-Neutrino-Helper.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.Red-Giant-Neutrino-Helper.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.TCDesignerCore.15.2.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.TCDesignerCore.15.2.plist
fi

if [ -f /$HOME/Library/Preferences/com.redgiant.TCDesignerCore.17.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.redgiant.TCDesignerCore.17.plist
fi

if [ -f /$HOME/Library/Preferences/com.RedGiantSoftware.MBLooksUI.plist ]; then
	rm -rf /$HOME/Library/Preferences/com.RedGiantSoftware.MBLooksUI.plist
fi

if [ -f /$HOME/Library/Preferences/net.maxon.app-manager.plist ]; then
	rm -rf /$HOME/Library/Preferences/net.maxon.app-manager.plist
fi

if [ -f /$HOME/Library/Preferences/net.maxon.app-manager.plist ]; then
	rm -rf /$HOME/Library/Preferences/net.maxon.app-manager.plist
fi

if [ -d /Applications/Red\ Giant\ FxPlug ]; then
	rm -r /Applications/Red\ Giant\ FxPlug
fi

# remove all the Red Giant plugins for Avid if they exist
if [ -d /Library/Application\ Support/Avid/AVX2_Plug-ins/Red\ Giant\ Universe ]; then
	rm -r /Library/Application\ Support/Avid/AVX2_Plug-ins/Red\ Giant\ Universe
fi

if [ -d /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/RGSGrowBounds.plugin ]; then
	rm -r /Library/Application\ Support/Adobe/Common/Plug-ins/7.0/MediaCore/RGSGrowBounds.plugin
fi

# remove all the Red Giant plugins for FCP X if they exist
if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Magic\ Bullet\ Suite ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Magic\ Bullet\ Suite
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Blur ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Blur
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Distort ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Distort
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Generators ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Generators
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Glow ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Glow
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Motion\ Graphics ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Motion\ Graphics
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Stylize ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Stylize
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Text ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Text
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Utilities ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Effects.localized/Universe\ Utilities
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Generators.localized/Universe\ Generators ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Generators.localized/Universe\ Generators
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Generators.localized/Universe\ Text ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Generators.localized/Universe\ Text
fi

if [ -d /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Transitions.localized/Universe\ Transitions ]; then
	rm -r /Library/Application\ Support/Final\ Cut\ Pro/Templates.localized/Transitions.localized/Universe\ Transitions
fi

# remove all the Red Giant plugins for OFX if they exist
if [ -d /Library/OFX/Plugins/Magic\ Bullet\ Suite ]; then
	rm -r /Library/OFX/Plugins/Magic\ Bullet\ Suite
fi

if [ -d /Library/OFX/Plugins/Red\ Giant\ Universe ]; then
	rm -r /Library/OFX/Plugins/Red\ Giant\ Universe
fi

if [ -d /Library/OFX/Plugins/Red\ Giant\ VFX ]; then
	rm -r /Library/OFX/Plugins/Red\ Giant\ VFX
fi

# remove the error logs
if [ -f /private/tmp/redgiant.service.err.log ]; then
	rm -rf /private/tmp/redgiant.service.err.log
fi

if [ -f /private/tmp/redgiant.service.out.log ]; then
	rm -rf /private/tmp/redgiant.service.out.log
fi

# remove the magic file that might be corrupted
if [ -f /Users/Shared/.4oaLkgIGnA ]; then
	rm -rf /Users/Shared/.4oaLkgIGnA
fi

# delete the _redgiantservice user
sudo dscl . -delete /Users/_redgiantservice

# delete the _redgiantservice group
sudo dscl . -delete /Groups/_redgiantservice

# delete the _mxappservice user
sudo dscl . -delete /Users/_mxappservice

# delete the _mxappservice group
sudo dscl . -delete /Groups/_mxappservice

echo "Your machine is clean! Please reinstall Maxon App and all the Red Giant products."
