#!/bin/bash

if [[ $EUID -ne 0 ]]; then
    echo "Please run as root (sudo)"
    exit 1;
fi

if [ -f /Users/$SUDO_USER/Library/LaunchAgents/com.shinywhitebox.SWBAudioDaemon.plist ]; then
    echo "Removing the launch agent..."
    rm -rf /Users/$SUDO_USER/Library/LaunchAgents/com.shinywhitebox.SWBAudioDaemon.plist
fi

echo "Removing the shared files..."
rm -rf "/Users/Shared/SWBAudioDaemon"

if [ -d "/Library/Audio/Plug-Ins/HAL/SWBAudioCapturePlugIn.driver" ]; then
    echo "Removing the audio driver..."
    rm -rf "/Library/Audio/Plug-Ins/HAL/SWBAudioCapturePlugIn.driver"
fi

# Unload helpers
echo "Stopping helper tool..."
killall -TERM com.shinywhitebox.SWBAudioHelperTool

# Kick coreaudiod in the head
echo "Restarting core audio..."
launchctl kickstart -k system/com.apple.audio.coreaudiod

echo "Completed!"