-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_dotzsh.sh
executable file
·184 lines (163 loc) · 5.04 KB
/
install_dotzsh.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/sh
########################################################################
# install_dotzsh.sh: Install dot_zsh Configuration
#
# Description:
# This script installs the dot_zsh configuration files to the specified
# target directory. It compiles zsh scripts into .zwc files, sets the
# appropriate permissions, and optionally removes existing configurations.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/dot_zsh
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: [email protected]
#
# Version History:
# v2.1 2025-03-22
# Unify usage information by extracting help text from header comments.
# v2.0 2025-03-17
# Standardized documentation format and added system checks.
# [Further version history truncated for brevity]
# v1.0 2025-01-17
# Initial stable release.
# v0.1 2011-05-20
# First release.
#
# Usage:
# ./install_dotzsh.sh [target_path] [nosudo]
#
# Options:
# -h, --help Show this help message and exit.
#
# Notes:
# - [target_path]: Path to the installation directory (default: /usr/local/etc/zsh).
# - [nosudo]: If specified, the script runs without sudo.
# - Ensure that the SCRIPT_HOME environment variable points to the directory
# containing the dot_zsh files before running the script.
# - This script is not POSIX compliant and is designed specifically for zsh environments.
#
########################################################################
# Display script usage information
usage() {
awk '
BEGIN { in_usage = 0 }
/^# Usage:/ { in_usage = 1; print substr($0, 4); next }
/^#{10}/ { if (in_usage) exit }
in_usage && /^#/ { print substr($0, 4) }
' "$0"
exit 0
}
# Function to check required commands
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
}
# Set up the environment and initialize variables
setup_environment() {
export SCRIPT_HOME=$(dirname "$(realpath "$0" 2>/dev/null || readlink -f "$0")")
if [ ! -d "$SCRIPT_HOME/dot_zsh/plugins" ]; then
echo "Error: $SCRIPT_HOME/dot_zsh/plugins directory does not exist." >&2
exit 1
fi
TARGET=${1:-/usr/local/etc/zsh}
echo "Installation target: $TARGET"
if [ -n "$2" ]; then
SUDO=""
else
SUDO="sudo"
fi
echo "Using sudo: ${SUDO:-no}"
case "$(uname)" in
Darwin)
OPTIONS=-Rv
OWNER=root:wheel
;;
*)
OPTIONS=-Rvd
OWNER=root:root
;;
esac
if [ "$SUDO" = "sudo" ]; then
check_sudo
else
OWNER="$(id -un):$(id -gn)"
fi
echo "Copy options: $OPTIONS, Owner: $OWNER"
}
# Set file permissions and ownership
set_permission() {
if [ -n "$2" ]; then
echo "Setting ownership to current user and group..."
chown -R "$OWNER" "$TARGET"
else
echo "Setting ownership to $OWNER..."
$SUDO chown -R "$OWNER" "$TARGET"
fi
}
# Compile zsh scripts into .zwc files
zsh_compile() {
echo "Compiling zsh scripts..."
for file in "$SCRIPT_HOME/dot_zsh/lib/"*.zsh; do
echo "Compiling $file"
zsh -c "zcompile $file"
done
for plugin in "$SCRIPT_HOME/dot_zsh/plugins/"*.zsh; do
echo "Compiling $plugin"
zsh -c "zcompile $plugin"
done
}
# Clean up compiled .zwc files
zwc_cleanup() {
echo "Cleaning up .zwc files..."
rm -f "$SCRIPT_HOME/dot_zsh/lib/"*.zwc
rm -f "$SCRIPT_HOME/dot_zsh/plugins/"*.zwc
}
# Install configuration files to the target directory
install_files() {
echo "Installing files from $SCRIPT_HOME/dot_zsh/ to $TARGET"
if [ -d "$TARGET" ]; then
echo "Removing existing directory: $TARGET"
$SUDO rm -rf "$TARGET"
fi
echo "Creating target directory: $TARGET"
$SUDO mkdir -p "$TARGET"
$SUDO cp $OPTIONS "$SCRIPT_HOME/dot_zsh/lib" "$TARGET/"
$SUDO cp $OPTIONS "$SCRIPT_HOME/dot_zsh/plugins" "$TARGET/"
$SUDO cp $OPTIONS "$SCRIPT_HOME/dot_zshrc" "$HOME/.zshrc"
zsh -c 'zcompile $HOME/.zshrc'
}
# Install dot_zsh configuration
install_dotzsh() {
echo "Starting installation..."
setup_environment "$@"
zsh_compile
install_files
zwc_cleanup
set_permission "$@"
echo "Installation complete!"
}
# Main function to execute the script
main() {
case "$1" in
-h|--help) usage ;;
esac
check_commands zsh cp mkdir chmod chown rm id dirname
install_dotzsh "$@"
}
# Execute main function
main "$@"