forked from pesqair/network-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·132 lines (85 loc) · 2.48 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /bin/bash
# Copyright (c) 2022 HudsonOnHere
# This software is provided "as-is", with no warranties or guarantees of any kind
readonly arg="${1}"
readonly FILE=/Users/Shared/network_autoswitcher.sh
readonly SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
readonly version="0.1"
err() {
echo "$*" >&2
}
usage() {
echo "Network AutoSwitcher Setup Tool, Version: $version"
echo
echo "USAGE: $0 [flag...] (-h|-i|-u)"
echo
echo " -h | --help Print this message"
echo " -i | --install Install"
echo " -u | --uninstall Uninstall"
echo
}
checkInstalled() {
if test -f "$FILE"; then
return 1
elif test ! -f "$FILE"; then
return 0
else
err "ERROR: Unhandled exception occured"
exit 1
fi
}
install() {
if checkInstalled; then
cd $SCRIPT_DIR && cp network_autoswitcher.sh $FILE
launchctl load local.network_autoswitcher.plist
launchctl start local.network_autoswitcher.plist
echo "$0: network_autoswitcher.sh has been installed to $FILE successfully."
exit 0
elif ! checkInstalled; then
err "$0: ERROR: network_autoswitcher.sh is already installed at $FILE"
exit 1
fi
}
uninstall() {
if ! checkInstalled; then
rm -i $FILE
launchctl stop local.network_autoswitcher.plist
launchctl unload local.network_autoswitcher.plist
echo "$0: network_autoswitcher.sh has been uninstalled successfully."
exit 0
elif checkInstalled; then
err "$0: ERROR: network_autoswitcher.sh is not installed, nothing to uninstall."
exit 1
fi
}
validateArg() {
# Ensures at least 1 argument has been passed
if [[ -z "${arg}" ]]; then
echo "$0: Invalid argument, run '$0 --help' to see example usage."
echo
err "ERROR: $0 expected at least 1 argument, but $# were given."
exit 1
fi
}
main() {
validateArg
case "${arg}" in
-h | --help)
usage
exit 0
;;
-i | --install)
install
;;
-u | --uninstall)
uninstall
;;
*)
echo "$0: Invalid argument '${arg}', see usage for available commands."
echo
err "ERROR: Unrecognized command '${arg}', run '$0 --help' for help."
exit 1
;;
esac
}
main "${@}"