#!/bin/bash

# Script to remove Backblaze 
# Copyright (c) 2009-2024 Backblaze Inc. All rights reserved.
# Version 1.3
#
# removed use of sudo inside the script, and replaced it with su -m user
# add a few echos mostly for debuging, they do not hurt
# when running child shells, by quiet and do not print any errors, we don't care
# Version 1.4
#

if [ $EUID -ne 0 ]; then
    echo "Requres to be run as root"
    exit 1
fi

# Check if Backblaze installed.
if [ -d "/Library/Backblaze.bzpkg" ] ; then
    echo "Backblaze is installed. Going to remove all Backblaze related files"
else
    echo "Backblaze not installed, exiting now."
    exit 1
fi

BZBMENU_NAME="com.backblaze.bzmenu"
BZBMENU_PLIST="/Library/LaunchAgents/com.backblaze.bzbmenu.plist"

BZSERV_NAME="com.backblaze.bzserv"
BZSERV_PLIST="/Library/LaunchDaemons/com.backblaze.bzserv.plist"

# forget packages
# to see all pkg identifiers
# pkgutil --pkgs | grep backblaze
#
# expand a package to see what's id-design
# pkgutil --expand ~/Downloads/ExampleApp.pkg /tmp/ExampleApp.unpkg
#
# sudo pkgutil --forget com.backblaze.modern.pkg
# pkgutil --pkg-info com.backblaze.modern.pkg
# pkgutil --verify com.backblaze.modern.pkg"
# pkgutil --forget com.backblaze.modern.pkg -f
# pkgutil --files com.backblaze.modern.pkg
# pkgutil --unlink com.backblaze.modern.pkg -f

for PACKAGE in $(pkgutil --pkgs | grep backblaze | /usr/bin/awk '{ print $1 }'); do
    echo " *** " /usr/sbin/pkgutil --forget $PACKAGE
    /usr/sbin/pkgutil --forget $PACKAGE >/dev/null 2>&1
done

# Pausing Backblaze running services.
# /Library/Backblaze.bzpkg/bztransmit -pausebackup

user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }')

for NAME in $(/usr/bin/dscl /Local/Default list /Users UniqueID | /usr/bin/awk '$2 > 500 {print $1}'); do
    # Determine home directory of user
    HOME_DIR="$(/usr/bin/dscl /Local/Default read /Users/"$NAME" NFSHomeDirectory | /usr/bin/awk '{print $2}')"
    # UID="$(/usr/bin/id -u "$NAME")"

    # Unload LaunchAgent, run these in user space
    # using sudo was prompting me for password !!! again even though i was running this shell as root
    # one more important hack when we call this from the bzuninstallerswift_cli, is to setuid(0);
    #
    # /bin/launchctl list | grep bz
    # /bin/launchctl print gui/503/com.backblaze.bzbmenu
    #
    echo " *** " /usr/bin/su -m "$NAME" -c "/bin/launchctl stop $BZBMENU_NAME"
    /usr/bin/su - "$NAME" -c "/bin/launchctl stop $BZBMENU_NAME"

    echo " *** " /usr/bin/su -m "$NAME" -c "/bin/launchctl unload \"$HOME_DIR${BZBMENU_PLIST}\""
    /usr/bin/su - "$NAME" -c "/bin/launchctl unload \"$HOME_DIR${BZBMENU_PLIST}\"" >/dev/null 2>&1

    echo " *** " /usr/bin/su -m "$NAME" -c "/bin/rm -f \"$HOME_DIR${BZBMENU_PLIST}\""
    /usr/bin/su - "$NAME" -c "/bin/rm -f \"$HOME_DIR${BZBMENU_PLIST}\"" >/dev/null 2>&1
    
    ## Removing log files
    echo " ***  Removing log files"
    /bin/rm -fr "$HOME_DIR/Library/Logs/Backblaze"
    /bin/rm -fr "$HOME_DIR/Library/Logs/BackblazeGUIInstaller"
    /bin/rm -fr "$HOME_DIR/Library/Logs/BackblazeRestore"
    /bin/rm -fr "$HOME_DIR/Library/Logs/BackblazeSilentInstaller"
    /bin/rm -f  "$HOME_DIR/Library/Logs/com.backblaze.bzuninstallerswift.log"

    ## Removing the app preferences
    /bin/rm -f "$HOME_DIR/Library/Preferences/com.backblaze.Backblaze.plist"
    /bin/rm -f "$HOME_DIR/Library/Preferences/com.backblaze.bzbmenu.plist"

    ## Removing the app cache
    /bin/rm -fr "$HOME_DIR/Library/Caches/Preferences/com.backblaze.Backblaze11"
    /bin/rm -fr "$HOME_DIR/Library/Caches/com.backblaze.BackblazeRestore"

    ## Restore app user data
    /bin/rm -fr "$HOME_DIR/Library/Application Support/BackblazeRestore"
    /bin/rm -f  "$HOME_DIR/Library/Preferences/com.backblaze.BackblazeRestore.plist"
    
    echo ""
done

/usr/bin/killall -9 bzbmenu

# Unloading and stopping bzserve
/bin/launchctl unload $BZSERV_PLIST >/dev/null 2>&1
/bin/rm $BZSERV_PLIST

# killall -q does not work on older macs ...
/usr/bin/killall -9 bzserv
/usr/bin/killall -9 bztransmit
/usr/bin/killall -9 bzfilelist

# kill it, old or new
/usr/bin/killall -9 "System Preferences"
/usr/bin/killall -9 "System Settings"

## Removing the backblaze system PreferencePanes
/bin/rm -rf /Library/PreferencePanes/BackblazeBackup.prefPane
/bin/rm -rf /Library/PreferencePanes/Backblaze Settings Launcher.prefpane

## Removing the backblaze application
/usr/bin/killall -9 Backblaze11
/bin/rm -rf /Applications/Backblaze.app

## Removing the backblaze restore application
/usr/bin/killall -9 BackblazeRestore
/bin/rm -rf /Applications/BackblazeRestore.app

## Removing Backblaze.bzpkg
/bin/rm -rf /Library/Backblaze.bzpkg

## Removing system logs
/bin/rm -fr /Library/Logs/BackblazeSilentInstaller

echo "completed"
exit 0
