Skip to content

Commit f3ffa31

Browse files
committed
Fixed some bugs in setup.sh
setup would fail on Big Sur (shift from 10.x naming to 11+)
1 parent 3de0962 commit f3ffa31

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

setup.sh

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env bash
2+
# Unofficial Bash Strict Mode
3+
set -euo pipefail
4+
IFS=$'\n\t'
25

36
function abspath {
47
if [[ -d "$1" ]]; then
@@ -17,31 +20,35 @@ function abspath {
1720

1821
function root_check {
1922
if [[ $EUID -ne 0 ]]; then
20-
>&2 echo "Please run script with 'sudo $(basename $0) $@', exiting..."
23+
>&2 echo "Please run script with 'sudo $(basename "$0") $*', exiting..."
2124
exit 1
2225
fi
2326
}
2427

2528
function os_check {
2629
VERSION=$(sw_vers | grep "ProductVersion" | sed -E 's/[^.0-9]+//g')
27-
#From "10.11.12" get "11"
28-
MAJOR=${VERSION:3:2}
30+
#From "10.11.12" (sw_vers) get "11"
31+
#From "12.0.1" (sw_vers) get "12"
32+
MAJOR=$(echo "$VERSION" | awk -F . '{ if ($1 >= 11) print $1; else print $2; }')
2933
MIN_MAJOR_VER=10
3034
MIN_OS_NAME=Yosemite
3135
if [[ $MAJOR -lt $MIN_MAJOR_VER ]]; then
3236
>&2 echo "osagitfilter needs at least macOS 10.$MIN_MAJOR_VER ($MIN_OS_NAME) but you have macOS ${VERSION}, exiting..."
3337
exit 1
3438
elif [[ -n $1 ]]; then
35-
echo "osagitfilter needs at least macOS 10.$MIN_MAJOR_VER ($MIN_OS_NAME) but you have macOS ${VERSION}, so that's OK..."
39+
if [[ $VERBOSE == 1 ]]; then
40+
echo "osagitfilter needs at least macOS 10.$MIN_MAJOR_VER.* ($MIN_OS_NAME) but you have macOS ${VERSION}, so that's OK..."
41+
fi
3642
fi
3743
}
3844

3945
SCRIPT_NAME=$0
40-
BASE_DIR=$(dirname $(abspath $0))
46+
BASE_DIR=$(dirname "$(abspath "$0")")
4147
INSTALL_INTO=/usr/local/bin
42-
COMMANDS='osagetlang osagitfilter'
48+
COMMANDS=$'osagetlang\tosagitfilter'
4349
LOG_PATH=~/Library/Logs/Catsdeep/
4450
VERBOSE=0
51+
GIT_OPTION=""
4552
C=unknown
4653

4754
while [[ $# -gt 0 ]]; do
@@ -65,9 +72,9 @@ if [[ $C = configure ]]; then
6572
fi
6673
for CMD in $COMMANDS; do
6774
if [[ -f $INSTALL_INTO/$CMD ]]; then
68-
echo "- ERROR: the command '$CMD' is already exists in '$INSTALL_INTO'. No install."
75+
echo "- WARNING: the command '$CMD' is already exists in '$INSTALL_INTO'."
6976
else
70-
if ln -s $BASE_DIR/$CMD.sh $INSTALL_INTO/$CMD; then
77+
if ln -s "$BASE_DIR/$CMD.sh" "$INSTALL_INTO/$CMD"; then
7178
echo "- '$CMD' installed"
7279
else
7380
echo "- ERROR: couldn't create symbolic link ($?)"
@@ -94,20 +101,20 @@ if [[ $C = configure ]]; then
94101
elif [[ $C = reset ]]; then
95102
for CMD in $COMMANDS; do
96103
if [[ -h $INSTALL_INTO/$CMD ]]; then
97-
if rm -f $INSTALL_INTO/$CMD; then
104+
if rm -f "$INSTALL_INTO/$CMD"; then
98105
echo "- '$CMD' reset"
99106
else
100107
echo "- ERROR: couldn't remove '$CMD' ($?)"
101108
fi
102109
else
103-
echo "- ERROR: the command '$CMD' is not installed in '$INSTALL_INTO' as symbolic link."
110+
echo "- WARNING: the command '$CMD' is not installed in '$INSTALL_INTO' as symbolic link."
104111
fi
105112
done
106113
if [[ $(which git) ]]; then
107114
echo "Removing git configuration"
108-
git config --global --remove-section filter.osa
115+
git config --global --remove-section filter.osa || echo "- WARNING: git exitted with code $?"
109116
else
110-
echo "git is not found; filter is not removed."
117+
echo "WARNING: git is not found; filter is not removed."
111118
fi
112119
elif [[ $C = rotate ]]; then
113120
LOG_NAME=osagitfilter
@@ -116,22 +123,22 @@ elif [[ $C = rotate ]]; then
116123
echo "Rotating log (if any) in $LOG_PATH:"
117124
if [[ -f $LOG_PATH/$LOG_NAME.log ]]; then
118125
echo "- renaming $LOG_NAME.log -> ${ROTATE_STAMP}_$LOG_NAME.log"
119-
mv $LOG_PATH/$LOG_NAME.log $LOG_PATH/${ROTATE_STAMP}_$LOG_NAME.log
126+
mv $LOG_PATH/$LOG_NAME.log "$LOG_PATH/${ROTATE_STAMP}_$LOG_NAME.log"
120127
fi
121128
else
122129
echo "Creating log directory, nothing to rotate"
123130
mkdir -p $LOG_PATH
124131
fi
125132
touch $LOG_PATH/$LOG_NAME.log
126133
elif [[ $C = create_local_bin ]]; then
127-
root_check
134+
root_check "$@"
128135
if [[ ! -d $INSTALL_INTO ]]; then
129136
echo "Creating $INSTALL_INTO"
130137
mkdir $INSTALL_INTO
131138
fi
132-
if [[ $(stat -f '%u' $INSTALL_INTO) != $(id -u ${SUDO_USER}) ]]; then
139+
if [[ $(stat -f '%u' $INSTALL_INTO) != $(id -u "${SUDO_USER}") ]]; then
133140
echo "Change ownership to ${SUDO_USER}:admin"
134-
chown -R ${SUDO_USER}:admin $INSTALL_INTO
141+
chown -R "${SUDO_USER}:admin" $INSTALL_INTO
135142
fi
136143
if [[ ":$PATH:" == *":$INSTALL_INTO:"* ]]; then
137144
echo "The folder $INSTALL_INTO is created successfully."

0 commit comments

Comments
 (0)