|
#!/usr/bin/env bash |
|
|
|
# Attempting to remove the existing system-wide Slack app |
|
if [ -d "/Applications/Slack.app" ]; then |
|
|
|
if [[ $(mdls -name kMDItemAppStoreReceiptIsVPPLicensed /Applications/Slack.app | awk '{ print $NF }') == "(null)" ]]; then |
|
echo "Direct-download system Slack app was detected. This copy of Slack app will be removed." |
|
|
|
# Kill Slack app if still running |
|
if [ ! -z "$(pgrep '[Ss]lack')" ]; then |
|
echo "Slack was running. Closing Slack..." |
|
killall -SIGKILL "Slack" |
|
fi |
|
|
|
# Removing Slack app |
|
echo "Deleting Slack app..." |
|
rm -rf /Applications/Slack.app |
|
else |
|
echo "The installed copy of Slack app either has a valid VPP license or is purchased through the App Store. The script will now exit." |
|
exit 1 |
|
fi |
|
else |
|
# Proceed to attempt installing Slack in user's ~/Applications directory |
|
echo "Slack app wasn't installed in /Applications. The script will now attempt to install Slack in user(s)' ~/Applications." |
|
fi |
|
|
|
|
|
################################################################################## |
|
# PLEASE UPDATE THE USER LIST HERE IF NECESSARY. THEY MUST BE SEPARATED BY SPACE # |
|
################################################################################## |
|
# List of users |
|
listed_users="bashtheshell travis.johnson testuser" |
|
|
|
|
|
# Slack download variables |
|
slackDownloadUrl=$(curl "https://slack.com/ssb/download-osx" -s -L -I -o /dev/null -w '%{url_effective}') |
|
dmgName=$(printf "%s" "${slackDownloadUrl[@]}" | sed 's@.*/@@') |
|
slackDmgPath="/private/tmp/$dmgName" |
|
|
|
|
|
# Check if there exists a listed user without Slack app in their ~/Applications directory before proceeding to download Slack |
|
set -- $listed_users |
|
no_user="true" |
|
while [ $# -gt 0 ]; do |
|
|
|
# Check if user's home directory actually exist before attempting to download Slack |
|
if [ ! -d "/Users/${1}/" ]; then |
|
# Skip to next user if user's home directory is non-existent |
|
shift |
|
else |
|
# Download Slack for user(s) with missing Slack app |
|
if [ ! -d "/Users/${1}/Applications/Slack.app" ]; then |
|
|
|
no_user="false" # Set to false as there exists a user |
|
|
|
# Detach Slack DMG volume if already mounted |
|
if [ ! -z "$(ls -d /Volumes/Slack* 2>/dev/null)" ]; then |
|
echo 'Ejecting all the DMG volumes...' |
|
for disk in $(hdiutil info | grep /dev/disk | grep partition | cut -f 1); do |
|
hdiutil detach ${disk} |
|
done |
|
fi |
|
|
|
|
|
# Downloads latest version of Slack or else exit script if download fails |
|
echo "Downloading latest Slack app (${dmgName})..." |
|
curl -L -o "$slackDmgPath" "$slackDownloadUrl" |
|
if [ $? -ne 0 ]; then |
|
echo "ERROR: Unable to download Slack. System may be offline. This script will now exit." |
|
exit 10 |
|
fi |
|
|
|
|
|
# Mounts the DMG volume |
|
echo 'Mounting the DMG volume...' |
|
hdiutil attach -nobrowse $slackDmgPath |
|
|
|
break |
|
fi |
|
shift |
|
fi |
|
done |
|
|
|
|
|
# Print if no eligible user is available |
|
if [[ "$no_user" == "true" ]]; then |
|
echo "There is no eligible user account with missing Slack app. The script will not install..." |
|
fi |
|
|
|
|
|
# Install Slack for user(s) without Slack app in their ~/Applications directory |
|
set -- $listed_users |
|
while [ $# -gt 0 ]; do |
|
|
|
# Check if user's home directory actually exist before creating ~/Applications path |
|
if [ ! -d "/Users/${1}/" ]; then |
|
# Skip to next user if user's home directory is non-existent |
|
shift |
|
else |
|
# Remove 'Slack' app from the user's Dock |
|
echo "Attempting to remove Slack app from ${1}'s Dock..." |
|
cat << 'EOF' > /tmp/remove_app_from_dock.sh |
|
#!/usr/bin/env bash |
|
listOfAppsToRemove=("Slack") |
|
for i in "${listOfAppsToRemove[@]}" |
|
do |
|
appFileLabel=$i # Name of the app as shown in the Dock |
|
lengthOfPlistArray=$(defaults read com.apple.dock persistent-apps | grep "file-label" | wc -l | tr -d " ") # Get length of array |
|
appPosition=$lengthOfPlistArray # The position as shown in dock (count starts after 'Finder'). Starting with an invalid index intentionally to prevent accidental deletion. |
|
numOfPersistApps=$(( $lengthOfPlistArray - 1 )) # Since counting starts at zero |
|
# Find the position that matches the app name |
|
for i in $(seq 0 $numOfPersistApps) |
|
do |
|
if [[ $(/usr/libexec/PlistBuddy -c "Print persistent-apps:${i}:tile-data:file-label" ~/Library/Preferences/com.apple.dock.plist) == $appFileLabel ]] |
|
then |
|
appPosition=$i |
|
fi |
|
done |
|
# Exit if app is not found in Dock |
|
if [[ $appPosition -eq $lengthOfPlistArray ]] |
|
then |
|
echo "The \"${appFileLabel}\" app is not found in dock. Moving on..." |
|
continue |
|
fi |
|
# Remove this entry from Dock's plist file : com.apple.dock.plist |
|
/usr/libexec/PlistBuddy -c "Delete persistent-apps:$appPosition" ~/Library/Preferences/com.apple.dock.plist |
|
# Print to STDOUT if command's successful |
|
if [[ $? -eq 0 ]] |
|
then |
|
echo "The \"${appFileLabel}\" app has been removed. Restarting Dock..." |
|
fi |
|
# Restart Dock to persist changes |
|
killall Dock |
|
done |
|
EOF |
|
chmod +x /tmp/remove_app_from_dock.sh |
|
su - $1 -c '/tmp/remove_app_from_dock.sh' |
|
rm /tmp/remove_app_from_dock.sh |
|
|
|
|
|
# Install Slack for user(s) with missing Slack app |
|
if [ ! -d "/Users/${1}/Applications/Slack.app" ]; then |
|
echo "Installing Slack app for ${1}..." |
|
mkdir -p /Users/${1}/Applications |
|
ditto -rsrc /Volumes/Slack*/Slack.app /Users/${1}/Applications/Slack.app |
|
chown -R $1: /Users/${1}/Applications |
|
fi |
|
|
|
|
|
# Add Slack app to Dock in user's profile if not added already |
|
account_user=$1 |
|
if [[ -z $(su - $account_user -c 'defaults read com.apple.dock persistent-apps | grep "Slack.app"') ]] |
|
then |
|
sleep 2 # OS doesn't update the Dock information fast enough at each iteration |
|
echo "Adding Slack app to $account_user's Dock..." |
|
su - $account_user -c "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Users/'"$account_user"'/Applications/Slack.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'" |
|
if [[ ! -z "$(sudo -u $account_user SHELL=/bin/bash -i sh -c 'pgrep -U $(id -u) [dD]ock')" ]]; then |
|
su - $account_user -c 'killall Dock' |
|
fi |
|
fi |
|
|
|
shift |
|
fi |
|
done |
|
|
|
|
|
# Unmount and eject the DMG volume |
|
echo 'Attempting to eject the DMG volumes if any...' |
|
hdiutil detach /Volumes/Slack.app |
|
|
|
|
|
# Clean up the downloaded files in /tmp directory |
|
echo 'Cleaning up temporary files if any...' |
|
rm -rf "$slackDmgPath" |