-
Notifications
You must be signed in to change notification settings - Fork 18
/
setup.sh
118 lines (106 loc) · 3.96 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
SLEEP_SCRIPTS_DIR=~/.sleepscripts
LAUNCH_AGENTS_PATH=~/Library/LaunchAgents/
KBOS_PLIST_PATH=~/Library/LaunchAgents/sleepwatch_bluetooth.plist
# Caputre arguments if any
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "**************************************"
echo "KBOS - Kill Bluetooth on sleep 🔪🎧😴"
echo "**************************************"
echo " "
echo "When run without arguments, this script installs Blueutil and sleepwatcher ( if not installed already ), and configures the two to work together in order to disable bluetooth everytime the computer goes to sleep."
echo " "
echo "options:"
echo "-h, --help show brief help"
echo "-d, --disable disables KBOS, but doesn't uninstall anything."
echo "-e, --enable re-enables KBOS"
echo "-u, --uninstall removes the sleepscripts directory and the KBOS Plist. Does NOT uninstall brew, Blueutil, or sleepwatcher."
exit 0
;;
-d|--disable)
OUTPUT="$(launchctl unload ${KBOS_PLIST_PATH} 2>&1)"
if [[ $OUTPUT == "" ]]; then
echo "KBOS Disabled ✋🏻"
else
echo "${OUTPUT}"
fi
;;
-e|--enable)
OUTPUT="$(launchctl load ${KBOS_PLIST_PATH} 2>&1)"
if [[ $OUTPUT == "" ]]; then
echo "KBOS Enabled 🔪"
exit 0;
else
echo "${OUTPUT}"
exit 0;
fi
;;
-u|--uninstall)
if [ -d "${SLEEP_SCRIPTS_DIR}" ]; then
echo "* Removing sleepscripts dir"
rm -r ${SLEEP_SCRIPTS_DIR}
fi
if [ -f "${KBOS_PLIST_PATH}" ]; then
echo "* Removing Plist"
rm ${KBOS_PLIST_PATH};
fi
echo "* To uninstall Blueutil and Sleepwatcher, please visit https://docs.brew.sh/FAQ#how-do-i-uninstall-a-formula"
echo "KBOS has successfully been uninstalled 🙅🏽♂️"
;;
*)
echo "${1} is an unknown argument."
exit 0;
;;
esac
exit 0;
done
# Install KBOS
echo "***************************"
echo "Checking for prerequisites"
echo "***************************"
if [[ $(which brew) == "" ]]; then
echo "Could not find Hombrew, It's required to install Blueutil. Check it out https://brew.sh/"
exit 1
else
echo "** Homebrew OK"
fi
# Check for blueutil
if [[ $(brew ls --versions blueutil) == "" ]]; then
echo "Could not find blueutil, Installing ..."
brew install blueutil || exit 1
else
echo "** Blueutil OK"
fi
# Check for sleepwatcher
if [[ $(brew ls --versions sleepwatcher) == "" ]]; then
echo "Could not find sleepwatcher, Installing ..."
brew install sleepwatcher || exit 1
brew services start sleepwatcher || exit 1
else
echo "** sleepwatcher OK"
fi
echo " "
echo "***********************"
echo "Preparing sleep scripts"
echo "***********************"
# Determine and escape absolute paths of sleepwatcher and Blueutil
SLEEPWATCHER_PATH=$(which sleepwatcher | sed 's_/_\\/_g')
BLUEUTIL_PATH=$(which blueutil | sed 's_/_\\/_g')
# Copy sleepscripts to user directory
mkdir -p ${SLEEP_SCRIPTS_DIR} || exit 1;
sed "s/blueutil/${BLUEUTIL_PATH}/" ./disable_bluetooth.sh > \
${SLEEP_SCRIPTS_DIR}/disable_bluetooth.sh || exit 1;
sed "s/blueutil/${BLUEUTIL_PATH}/" ./enable_bluetooth.sh > \
${SLEEP_SCRIPTS_DIR}/enable_bluetooth.sh || exit 1;
chmod +x ${SLEEP_SCRIPTS_DIR}/* || exit 1;
echo "** sleep scripts copied to ${SLEEP_SCRIPTS_DIR}"
# Copy plist to ~/Library/LaunchAgents - after creating the directory if it doesn't exist
mkdir -p ${LAUNCH_AGENTS_PATH} || exit 1;
sed "s/sleepwatcher/${SLEEPWATCHER_PATH}/" ./sleepwatch_bluetooth.plist > \
${LAUNCH_AGENTS_PATH}/sleepwatch_bluetooth.plist || exit 1;
echo "** sleepwatch_bluetooth.plist copied to ${LAUNCH_AGENTS_PATH}"
launchctl load ${KBOS_PLIST_PATH}
echo " "
echo "KBOS has been successfully installed 🔪"