#!/bin/sh

# =========================================================================
# Uninstall Script
# =========================================================================
# Template: MIDI+AUDIO with Control Panel
# Device  : STUDIO-CAPTURE
Device="StudioCapture"

RDID=0160
AUDIO="1"
MIDI="1"
APP="1"
fRet=1
AppName="STUDIO-CAPTURE Control Panel.app"

echo "Start Script"
echo "$1"

if [ "$1" = "RUN" ]
then
   echo "RUN"
fi

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

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

# old pkg receipt
if [ $majorVer -eq 10 -a $minorVer -ge 6 ]
then
    if [ $minorVer -ge 9 ]
    then
        echo "10.9 or later"
        pkgId="jp.co.roland.""$Device"".kext.""$majorVer""$minorVer"".pkg"
        if pkgutil --pkg-info $pkgId
        then
            if [ "$1" = "RUN" ] ; then
                pkgutil --forget $pkgId
            fi
            fRet=0
        fi
        pkgId="jp.co.roland.""$Device"".midi.""$majorVer""$minorVer"".pkg"
        if pkgutil --pkg-info $pkgId
        then
            if [ "$1" = "RUN" ] ; then
                pkgutil --forget $pkgId
            fi
            fRet=0
        fi
        pkgId="jp.co.roland.""$Device"".pref.""$majorVer""$minorVer"".pkg"
        if pkgutil --pkg-info $pkgId
        then
            if [ "$1" = "RUN" ] ; then
                pkgutil --forget $pkgId
            fi
            fRet=0
        fi
        pkgId="jp.co.roland.""$Device"".start.""$majorVer""$minorVer"".pkg"
        if pkgutil --pkg-info $pkgId
        then
            if [ "$1" = "RUN" ] ; then
                pkgutil --forget $pkgId
            fi
            fRet=0
        fi
        pkgId="jp.co.roland.""$Device"".app.""$majorVer""$minorVer"".pkg"
        if pkgutil --pkg-info $pkgId
        then
            if [ "$1" = "RUN" ] ; then
                pkgutil --forget $pkgId
            fi
            fRet=0
        fi
    fi
	echo "10.6 or later"
	if [ "$AandM" = "1" ]
	then
		pkgId=jp.co.roland.RDUSB"$RDID".SN
		altPkgId=jp.co.roland.RDUSB"$RDID".SN"$majorVer""$minorVer"
	elif [ "$AUDIO" = "1" ]
	then
		pkgId=jp.co.roland.RDUSB"$RDID".SN
		altPkgId=jp.co.roland.RDUSB"$RDID".SN"$majorVer""$minorVer"
	elif [ "$MIDI" = "1" ]
	then
		pkgId=jp.co.roland.RDUSB"$RDID".SN
		altPkgId=jp.co.roland.RDUSB"$RDID".SN"$majorVer""$minorVer"
	fi

	if pkgutil --pkg-info $pkgId
	then
		if [ "$1" = "RUN" ] ; then
			pkgutil --forget $pkgId
		fi
		fRet=0
	fi

	if pkgutil --pkg-info $altPkgId
	then
		if [ "$1" = "RUN" ] ; then
			pkgutil --forget $altPkgId
		fi
		fRet=0
	fi
fi

# =========================================================================
# Delete Kext (Audio, MIDI)
# =========================================================================

	if [ -e "/System/Library/Extensions/RDUSB"$RDID"Dev.kext" ]
	then
		if [ "$1" = "RUN" ]
		then
			echo "RUN"
			 mv "/System/Library/Extensions/RDUSB"$RDID"Dev.kext" "/tmp"
			rm -R "/tmp/RDUSB"$RDID"Dev.kext"
			touch "/System/Library/Extensions/"
		fi
		fRet=0
	else
		echo "RDUSB"$RDID"Dev.kext does not exist"
	fi

    #For 10.9
    if [ -e "/Library/Extensions/RDUSB"$RDID"Dev.kext" ]
    then
        if [ "$1" = "RUN" ]
        then
        echo "RUN"
        mv "/Library/Extensions/RDUSB"$RDID"Dev.kext" "/tmp"
        rm -R "/tmp/RDUSB"$RDID"Dev.kext"
        touch "/Library/Extensions/"
        fi
        fRet=0
	else
		echo "RDUSB"$RDID"Dev.kext does not exist"
	fi

# =========================================================================
# Delete Preference Pane (Audio)
# =========================================================================

if [ "$AUDIO" = "1" ]
then
	if [ -e "/Library/PreferencePanes/RDUSB"$RDID"Pref.prefPane" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "/Library/PreferencePanes/RDUSB"$RDID"Pref.prefPane" "/tmp"
			rm -R "/tmp/RDUSB"$RDID"Pref.prefPane"
		fi
		fRet=0
	else
		echo "RDUSB"$RDID"Pref.prefPane does not exist"		
	fi
fi

# =========================================================================
# Delete Startup Item (Audio)
# =========================================================================

if [ "$AUDIO" = "1" ]
then
	if [ -e "/Library/StartupItems/RDUSB"$RDID"Startup" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "/Library/StartupItems/RDUSB"$RDID"Startup" "/tmp"
			rm -R "/tmp/RDUSB"$RDID"Startup"
		fi
		fRet=0
	else
		echo "RDUSB"$RDID"Startup does not exist"
	fi
fi

# =========================================================================
# Delete Preference (Audio)
# =========================================================================

if [ "$AUDIO" = "1" ]
then
	if [ -e "/Library/Preferences/jp_co_roland_RDUSB"$RDID"Dev.pref.plist" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "/Library/Preferences/jp_co_roland_RDUSB"$RDID"Dev.pref.plist" "/tmp"
			rm -R "/tmp/jp_co_roland_RDUSB"$RDID"Dev.pref.plist"
		fi
		fRet=0
	else
		echo "jp_co_roland_RDUSB"$RDID"Dev.pref.plist does not exist"
	fi
fi

# =========================================================================
# Delete MIDI plugin (MIDI)
# =========================================================================

if [ "$MIDI" = "1" ]
then
	if [ -e "/Library/Audio/MIDI Drivers/RDUSB"$RDID"Midi.plugin" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "/Library/Audio/MIDI Drivers/RDUSB"$RDID"Midi.plugin" "/tmp"
			rm -R "/tmp/RDUSB"$RDID"Midi.plugin"
		fi
		fRet=0
	else
		echo "RDUSB"$RDID"Midi.plugin does not exist"
	fi
fi

# =========================================================================
# Delete Control Panel Application (APP)
# =========================================================================

if [ "$APP" = "1" ]
then
	if [ -e "/Applications/$AppName" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "/Applications/$AppName" "/tmp"
			rm -R "/tmp/$AppName"
		fi
		fRet=0
	else
		echo $AppName" does not exist"
	fi
fi

# =========================================================================
# Check Control Panel Application preferences(APP)
# =========================================================================

PREFDIR="$HOME"/Library/Preferences
PREFNAME=jp.co.roland.RDUSB"$RDID".cpl.plist

if [ "$APP" = "1" ]
then
	if [ -e "$PREFDIR/$PREFNAME" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "$PREFDIR/$PREFNAME" "/tmp"
			rm -R "/tmp/$PREFNAME"
		fi
		fRet=0
	else
		echo ""$PREFDIR/$PREFNAME" does NOT exist"
	fi
fi

# =========================================================================
# Delete Uninstaller / Readme
# =========================================================================
TempDir="/tmp/$Device.$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"
	else
		echo $TempDir is already existed.
	fi
fi

for Target in "$InstDir/$UninstName" "$InstDir/$ReadmeName" "$InstDir/$ReadmeDir"
do
	if [ -e "$Target" ]
	then
		if [ "$1" = "RUN" ]
		then
			mv "$Target" "$TempDir"
		fi
		fRet=0
	else
		echo $Target" does not exist"
	fi
done

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


# =========================================================================
# Return Value
# =========================================================================

exit $fRet
