-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_dotzsh.sh
executable file
·136 lines (119 loc) · 4.19 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
#!/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/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.0 2025-01-17
# Initial release. Updated documentation and refactored for readability.
#
# Usage:
# ./install_dotzsh.sh [target_path] [nosudo]
#
# Notes:
# - [target_path]: Path to the installation directory (default: /usr/local/etc/zsh).
# - [nosudo]: If specified, the script runs without sudo.
# - Ensure that the DOT_ZSH_SOURCE 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.
#
########################################################################
# Check if zsh is installed
if ! command -v zsh > /dev/null 2>&1; then
echo "Error: zsh is not installed. Please install zsh before running this script." >&2
exit 1
fi
# Set the source directory dynamically based on the script's location
export DOT_ZSH_SOURCE=$(dirname "$(realpath "$0" 2>/dev/null || readlink -f "$0")")
# Ensure the source directory exists
if [ ! -d "$DOT_ZSH_SOURCE/dot_zsh/plugins" ]; then
echo "Error: $DOT_ZSH_SOURCE/dot_zsh/plugins directory does not exist." >&2
exit 1
fi
# Set up the environment and initialize variables
setup_environment() {
# Set the installation target
test -n "$1" && export TARGET=$1 || export TARGET=/usr/local/etc/zsh
echo "Installation target: $TARGET"
# Determine whether to use sudo
test -n "$2" && SUDO= || SUDO=sudo
echo "Using sudo: ${SUDO:-no}"
# Set options and owner based on the operating system
case $(uname) in
Darwin)
OPTIONS=-Rv
OWNER=root:wheel
;;
*)
OPTIONS=-Rvd
OWNER=root:root
;;
esac
echo "Copy options: $OPTIONS, Owner: $OWNER"
}
# Set file permissions and ownership
set_permission() {
if [ -n "$2" ]; then
# If nosudo is specified, set ownership to the current user and group
echo "Setting ownership to current user and group..."
chown -R "$(id -un):$(id -gn)" "$TARGET"
else
# Use sudo to set ownership to the appropriate system user and group
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 "$DOT_ZSH_SOURCE/dot_zsh/lib/"*.zsh; do
echo "Compiling $file"
zsh -c "zcompile $file"
done
for plugin in "$DOT_ZSH_SOURCE/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 "$DOT_ZSH_SOURCE/dot_zsh/lib/"*.zwc
rm -f "$DOT_ZSH_SOURCE/dot_zsh/plugins/"*.zwc
}
# Install dot_zsh configuration
install_dotzsh() {
echo "Starting installation..."
setup_environment "$@"
# Remove existing target directory if it exists
if [ -d "$TARGET" ]; then
echo "Removing existing directory: $TARGET"
$SUDO rm -rf "$TARGET"
fi
# Create the target directory
echo "Creating target directory: $TARGET"
$SUDO mkdir -p "$TARGET"
# Compile zsh scripts and copy files
zsh_compile
echo "Copying files from $DOT_ZSH_SOURCE/dot_zsh/ to $TARGET"
$SUDO cp -R "$DOT_ZSH_SOURCE/dot_zsh/lib" "$TARGET/"
$SUDO cp -R "$DOT_ZSH_SOURCE/dot_zsh/plugins" "$TARGET/"
$SUDO cp "$DOT_ZSH_SOURCE/dot_zshrc" "$HOME/.zshrc"
zsh -c 'zcompile $HOME/.zshrc'
# Clean up temporary .zwc files
zwc_cleanup
# Set permissions appropriately
set_permission "$@"
echo "Installation complete!"
}
# Execute the installation
install_dotzsh "$@"