#!/bin/sh

#  install-driver.sh
#  CleanShot Driver Control
#
#  Copyright © 2020 MakeTheWeb. All rights reserved.
#
#  Pass -u to uninstall.

PATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH

# The dir CoreAudio HAL plug-ins are installed to.
plugins_path="/Library/Audio/Plug-Ins/HAL"

# The name of the driver bundle, i.e. the dir containing the compiled driver.
driver_bundle="CleanShot Device.driver"

# Make sure the dir for HAL plug-ins exists.
mkdir -p "$plugins_path"

# Remove the existing copy of the driver if there is one.
# "${:?}" is just to guard against it ever being possible for this to evaluate to "rm -rf /".
rm -rf "${plugins_path:?}/$driver_bundle"

# If we're uninstalling, just skip the install step.
if [ "$1" != "-u" ]; then
    # Install the driver.
    cp -r "$driver_bundle" "$plugins_path"
fi

# Restart coreaudiod so it will load the driver (or unload it, if we're uninstalling).
launchctl kickstart -k system/com.apple.audio.coreaudiod
