-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunfix_compinit.sh
More file actions
executable file
·150 lines (137 loc) · 4.95 KB
/
unfix_compinit.sh
File metadata and controls
executable file
·150 lines (137 loc) · 4.95 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
########################################################################
# unfix_compinit.sh: Temporarily revert secure settings for compinit
#
# Description:
# This script temporarily adjusts the ownership and permissions of
# Homebrew-related directories to align with Homebrew's recommended
# configuration. Specifically, it changes the ownership of the following
# directories to the current user and their primary group, and ensures
# that the user has write permissions:
# - /usr/local/Homebrew
# - /usr/local/share/zsh/
# - /usr/local/share/zsh/site-functions
# These changes are intended to resolve issues with `brew update`
# and warnings from `brew doctor` about insecure directories. After
# finishing Homebrew-related tasks, you can revert these changes using
# `fix_compinit.sh` to restore the secure configuration.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# ./unfix_compinit.sh
#
# Notes:
# - This script requires root privileges to execute. Run it with `sudo`.
# - This script is specifically tailored for macOS and will not function
# on other operating systems.
# - After using Homebrew, execute `fix_compinit.sh` to restore secure settings.
#
# Version History:
# v1.8 2025-12-15
# Resolve Homebrew prefix dynamically and process existing targets only.
# v1.7 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v1.6 2025-04-28
# Add error handling to ownership and permission operations.
# v1.5 2025-04-13
# Unify log level formatting using [INFO], [WARN], and [ERROR] tags.
# v1.4 2025-03-22
# Unify usage information by extracting help text from header comments.
# v1.3 2025-03-16
# Encapsulated all logic in functions and introduced main function.
# v1.2 2025-03-13
# Redirected error messages to stderr for better logging and debugging.
# v1.1 2025-03-05
# Added sudo privilege check when --sudo option is specified.
# v1.0 2025-01-17
# Initial release. Sets Homebrew-recommended ownership and permissions.
#
########################################################################
# Display full script header information extracted from the top comment block
usage() {
awk '
BEGIN { in_header = 0 }
/^#{10,}$/ { if (!in_header) { in_header = 1; next } else exit }
in_header && /^# ?/ { print substr($0, 3) }
' "$0"
exit 0
}
# Check if the system is macOS
check_system() {
if [ "$(uname -s 2>/dev/null)" != "Darwin" ]; then
echo "[ERROR] This script is intended for macOS only." >&2
exit 1
fi
}
# Check if required commands are available and executable
check_commands() {
for cmd in "$@"; do
cmd_path=$(command -v "$cmd" 2>/dev/null)
if [ -z "$cmd_path" ]; then
echo "[ERROR] Command '$cmd' is not installed. Please install $cmd and try again." >&2
exit 127
elif [ ! -x "$cmd_path" ]; then
echo "[ERROR] Command '$cmd' is not executable. Please check the permissions." >&2
exit 126
fi
done
}
# Check if the user has sudo privileges (password may be required)
check_sudo() {
if ! sudo -v 2>/dev/null; then
echo "[ERROR] This script requires sudo privileges. Please run as a user with sudo access." >&2
exit 1
fi
}
# Adjust ownership and permissions for Homebrew directories
adjust_homebrew_permissions() {
echo "[INFO] Setting ownership and permissions for Homebrew directories on macOS..."
# Get the current user and their primary group
current_user=$(whoami)
current_group=$(id -gn "$current_user")
# Detect Homebrew prefix dynamically; fallback to /usr/local
prefix=$(brew --prefix 2>/dev/null || echo /usr/local)
targets="
${prefix}/Homebrew
${prefix}/share/zsh/
${prefix}/share/zsh/site-functions
"
changed=0
for d in $targets; do
if [ -d "$d" ]; then
echo "[INFO] Processing: $d"
if ! sudo chown -R "$current_user":"$current_group" "$d"; then
echo "[ERROR] Failed to change ownership of $d." >&2
exit 1
fi
if ! sudo chmod u+w "$d" 2>/dev/null; then
echo "[WARN] Could not set write permission on $d (may be fine for protected paths)." >&2
fi
ls -ld "$d"
changed=1
else
echo "[WARN] Directory not found: $d" >&2
fi
done
if [ "$changed" -eq 0 ]; then
echo "[INFO] No target directories under prefix: $prefix; nothing to do."
return 0
fi
}
# Main entry point of the script
main() {
case "$1" in
-h|--help|-v|--version) usage ;;
esac
check_system
check_commands uname brew chown chmod ls
check_sudo
adjust_homebrew_permissions
return 0
}
# Execute main function
main "$@"