#!/bin/sh

TRACE=echo

# =========================================================================
# Uninstall Script
# =========================================================================
$TRACE "========== STUDIO-CAPTURE Driver Uninstaller =========="

# =========================================================================
# Parameters
# =========================================================================
AandM="1"
AUDIO="0"
MIDI="0"
APP="1"
fRet=1

### check flags ###
if [ "$AandM" = "1" -a "$AUDIO" = "1" ] ; then
	$TRACE "Both AandM and Audio are defined..."
	exit -1
fi
if [ "$AandM" = "1" -a "$MIDI" = "1" ] ; then
	$TRACE "Both AandM and Midi are defined..."
	exit -1
fi

### check OS version ###
osVer=`sw_vers -productVersion`
majorVer=`echo $osVer | cut -d '.' -f 1`
minorVer=`echo $osVer | cut -d '.' -f 2`


$TRACE "Start Script" "$1"

# =========================================================================
# functions
# =========================================================================
function rm_old_pkg_receipt() {
	local fRun=$1
	local pkgId=$2

	if [ -e "$pkgId" ] ; then
		if [ "$fRun" = "RUN" ] ; then
			$TRACE "remove $pkgId"
			rm -R "$pkgId"
		else
			$TRACE "$pkgId exists"
		fi
		fRet=0
	fi
}

function forget_pkg_receipt() {
	local fRun=$1
	local pkg=$2

	pkgutil --pkgs | grep "$pkg" | while read pkgId ; do
		if pkgutil --pkg-info $pkgId ; then
			if [ "$fRun" = "RUN" ] ; then
				$TRACE "forget $pkgId"
				pkgutil --forget "$pkgId"
			else
				$TRACE "$pkgId exists"
			fi
			fRet=0
		fi
	done
}

function delete_files_if_exist() {
	local fRun=$1
	local dname=$2
	local fname=$3
	local fTouch=$4

	if [ -e "$dname""$fname" ] ; then
		if [ "$fRun" = "RUN" ] ; then
			$TRACE remove "$dname""$fname"
			mv "$dname""$fname" "/tmp"
			rm -R /tmp/"$fname"
			if [ "$fTouch" = "1" ] ; then
				$TRACE touch "$dname"
				touch "$dname"
			fi
		else
			$TRACE "$dname""$fname" exists
		fi
		fRet=0
	fi
}

function delete_launch_daemon() {
	local fRun=$1
	local plist=$2

	if [ -e "$plist" ] ; then
		if [ "$fRun" = "RUN" ] ; then
			$TRACE "unload and remove $plist"
			launchctl unload -w "$plist"
			rm -f "$plist"
		else
			$TRACE "$plist exists"
		fi
	fi
}

function remove_dir_if_empty() {
	local fRun=$1
	local path=$2

	if [ -d "$path" ] ; then
		if [ "$fRun" = "RUN" ] ; then
			$TRACE "remove $path"
			rm -f "$path/.DS_Store"
			rmdir "$path"
		else
			$TRACE "$path exists"
		fi
	fi
}

# =========================================================================
# Delete PKG in Receipts (Installer)
# =========================================================================

# remove old pkg receipt
rm_old_pkg_receipt "$1" "/Library/Receipts/StudioCaptureUSBDriver.pkg"
rm_old_pkg_receipt "$1" "/Library/Receipts/StudioCapture_USBDriver104.pkg"

# forget pkg receipt
if [ $majorVer -eq 10 -a $minorVer -ge 6 ] || [ $majorVer -ge 11 ] ; then
	# 10.9 or later
	if [ $minorVer -ge 9 ] || [ $majorVer -ge 11 ] ; then
		forget_pkg_receipt "$1" "jp.co.roland.StudioCapture.kext"
		forget_pkg_receipt "$1" "jp.co.roland.StudioCapture.midi"
		forget_pkg_receipt "$1" "jp.co.roland.StudioCapture.pref"
		forget_pkg_receipt "$1" "jp.co.roland.StudioCapture.start"
		forget_pkg_receipt "$1" "jp.co.roland.StudioCapture.app"
	fi

	# 10.6 or later
	if [ "$AandM" = "1" ] ; then
		forget_pkg_receipt "$1" "jp.co.roland.RDUSB0160.SN"
	fi
	if [ "$AUDIO" = "1" ] ; then
		forget_pkg_receipt "$1" "jp.co.roland.RDUSB0000.SN"
	fi
	if [ "$MIDI" = "1" ] ; then
		forget_pkg_receipt "$1" "jp.co.roland.RDUSB0000.SN"
    fi
fi

# =========================================================================
# Delete Files
# =========================================================================
### Delete Kext (AandM) ###
if [ "$AandM" = "1" ] ; then
	delete_files_if_exist "$1" "/System/Library/Extensions/" "RDUSB0160Dev.kext" 1
	delete_files_if_exist "$1" "/Library/Extensions/" "RDUSB0160Dev.kext" 1
fi

### Delete Kext (Audio) ###
if [ "$AUDIO" = "1" ] ; then
	delete_files_if_exist "$1" "/System/Library/Extensions/" "RDUSB0000Dev.kext" 1
	delete_files_if_exist "$1" "/Library/Extensions/" "RDUSB0000Dev.kext" 1
fi

### Delete Kext (Midi) ###
if [ "$MIDI" = "1" ] ; then
	delete_files_if_exist "$1" "/System/Library/Extensions/" "RDUSB0000Dev.kext" 1
	delete_files_if_exist "$1" "/Library/Extensions/" "RDUSB0000Dev.kext" 1
fi

### Delete Preference Pane (AandM) ###
if [ "$AandM" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/PreferencePanes/" "RDUSB0160Pref.prefPane"
fi

### Delete Preference Pane (Audio) ###
if [ "$AUDIO" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/PreferencePanes/" "RDUSB0000Pref.prefPane"
fi

### Delete Startup Item (AandM) ###
if [ "$AandM" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/StartupItems/" "RDUSB0160Startup"
fi

### Delete Startup Item (Audio) ###
if [ "$AUDIO" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/StartupItems/" "RDUSB0000Startup"
fi

### Unload and Delete LaunchDaemon (AandM) ###
if [ "$AandM" = "1" ] ; then
	delete_launch_daemon "$1" "/Library/LaunchDaemons/jp.co.roland.RDUSB0160Setupd.plist"
	delete_files_if_exist "$1" "/Library/Application Support/RolandDrv/RDUSB0160/" "RDUSB0160Setupd.plist"
	delete_files_if_exist "$1" "/Library/Application Support/RolandDrv/RDUSB0160/" "RDUSB0160Setupd"
	remove_dir_if_empty "$1" "/Library/Application Support/RolandDrv/RDUSB0160"
fi

### Unload and Delete LaunchDaemon (Audio) ###
if [ "$AUDIO" = "1" ] ; then
	delete_launch_daemon "$1" "/Library/LaunchDaemons/jp.co.roland.RDUSB0000Setupd.plist"
	delete_files_if_exist "$1" "/Library/Application Support/RolandDrv/RDUSB0000/" "RDUSB0000Setupd.plist"
	delete_files_if_exist "$1" "/Library/Application Support/RolandDrv/RDUSB0000/" "RDUSB0000Setupd"
	remove_dir_if_empty "$1" "/Library/Application Support/RolandDrv/RDUSB0000"
fi

### Delete Roland Application Support root dir ###
	remove_dir_if_empty "$1" "/Library/Application Support/RolandDrv"

### Delete Preference (AandM) ###
if [ "$AandM" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/Preferences/" "jp_co_roland_RDUSB0160Dev.pref.plist"
fi

### Delete Preference (Audio) ###
if [ "$AUDIO" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/Preferences/" "jp_co_roland_RDUSB0000Dev.pref.plist"
fi

### Delete MIDI plugin (AandM) ###
if [ "$AandM" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/Audio/MIDI Drivers/" "RDUSB0160Midi.plugin"
fi

### Delete MIDI plugin (MIDI) ###
if [ "$MIDI" = "1" ] ; then
	delete_files_if_exist "$1" "/Library/Audio/MIDI Drivers/" "RDUSB0000Midi.plugin"
fi

### Delete Control Panel Application (APP) ###
if [ "$APP" = "1" ] ; then
	delete_files_if_exist "$1" "/Applications/" "STUDIO-CAPTURE Control Panel.app"
fi

### Check Control Panel Application preferences(APP) ###
if [ "$APP" = "1" ] ; then
	delete_files_if_exist "$1" "$HOME""/Library/Preferences/" "jp.co.roland.RDUSB0160.cpl.plist"
fi

# =========================================================================
# Delete Uninstaller / Readme
# =========================================================================
TempDir="/tmp/StudioCapture.$RANDOM"
UninstName=Uninstaller.app
ReadmeDir=Readme
ReadmeName=Readme.htm
InstRoot="/Applications/Roland"
InstDir="$InstRoot"/"STUDIO-CAPTURE Driver"

if [ "$1" = "RUN" ] ; then
	if [ ! -e "$TempDir" ] ; then
		mkdir "$TempDir"
	fi
fi

for Target in "$InstDir/$UninstName" "$InstDir/$ReadmeName" "$InstDir/$ReadmeDir"
do
	if [ -e "$Target" ] ; then
		$TRACE $Target" exists"
		if [ "$1" = "RUN" ] ; then
			mv "$Target" "$TempDir"
		fi
		fRet=0
	fi
done

if [ "$1" = "RUN" ]
then
	rm -R "$TempDir"
	rm "$InstDir/.DS_Store"
	rmdir "$InstDir"
	rm "$InstRoot/.DS_Store"
	rmdir "$InstRoot"
fi

# =========================================================================
# Return Value
# =========================================================================
$TRACE "STUDIO-CAPTURE Driver Uninstaller returns = "$fRet
exit $fRet
