-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·144 lines (123 loc) · 3.58 KB
/
install.sh
File metadata and controls
executable file
·144 lines (123 loc) · 3.58 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
#!/usr/bin/env bash
# Simple, effective mactime installer
# ----------------------------------
set -euo pipefail
IFS=$'\n\t'
# Output functions
log_color() {
local color="$1"
local message="$2"
printf "\033[0;${color}m%s\033[0m\n" "$message"
}
green() { log_color "32" "$1"; }
blue() { if $VERBOSE; then log_color "34" "$1"; fi }
yellow() { if $VERBOSE; then log_color "33" "$1"; fi }
red() { log_color "31" "$1"; }
# Check dependencies
check_dependencies() {
for cmd in git ln; do
if ! command -v "$cmd" &> /dev/null; then
red "Error: $cmd is required but not found. Please install it first."
exit 1
fi
done
}
# Get source location and prepare main script path
setup_source_location() {
# Get script location
SCRIPT_PATH="$(realpath "$0")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
# Determine source location
if [ -f "$SCRIPT_DIR/main.py" ]; then
# Local installation
blue "Installing from local directory: $SCRIPT_DIR"
MAIN_SCRIPT="$SCRIPT_DIR/main.py"
else
# Remote installation
blue "Installing from GitHub..."
mkdir -p ~/.local/src
REPO_DIR="$HOME/.local/src/mactime"
MAIN_SCRIPT="$REPO_DIR/main.py"
if [ -d "$REPO_DIR" ]; then
blue "Updating existing repository..."
git -C "$REPO_DIR" fetch --quiet
git -C "$REPO_DIR" reset --hard origin/main --quiet
else
blue "Cloning repository..."
git clone --quiet https://github.com/Bobronium/mactime.git "$REPO_DIR"
fi
fi
}
# Create a symlink with backup protection
create_symlink() {
local src="$1"
local dest="$2"
if [ ! -e "$src" ]; then
red "Error: Source file $src does not exist!"
return 1
fi
if [ -L "$dest" ]; then
rm "$dest"
elif [ -e "$dest" ]; then
yellow "Note: Creating backup of existing file: ${dest}.bak"
mv "$dest" "${dest}.bak"
fi
ln -s "$src" "$dest"
blue "Created symlink: $dest -> $src"
}
# Setup symlinks
setup_symlinks() {
blue "Creating symlinks..."
create_symlink "$MAIN_SCRIPT" "$BIN_DIR/mactime"
create_symlink "$MAIN_SCRIPT" "$BIN_DIR/whenwhat"
create_symlink "$MAIN_SCRIPT" "$BIN_DIR/ww"
}
# Ensure PATH contains ~/.local/bin
setup_path() {
if ! echo "$PATH" | tr ':' '\n' | grep -q "^$HOME/.local/bin$"; then
yellow "Adding ~/.local/bin to your PATH..."
# Determine which shell configs exist
shell_configs=()
[ -f "$HOME/.zshrc" ] && shell_configs+=("$HOME/.zshrc")
[ -f "$HOME/.bashrc" ] && shell_configs+=("$HOME/.bashrc")
if [ ${#shell_configs[@]} -gt 0 ]; then
for config in "${shell_configs[@]}"; do
if ! grep -q 'export PATH="$HOME/.local/bin:$PATH"' "$config"; then
yellow "Updating $config..."
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$config"
fi
done
export PATH="$HOME/.local/bin:$PATH" # Update current session
else
yellow "No shell config files found. You'll need to add ~/.local/bin to your PATH manually."
fi
fi
}
# Verify installation
verify_installation() {
if command -v mactime &> /dev/null || [[ -x "$BIN_DIR/mactime" && "$PATH" == *"$BIN_DIR"* ]]; then
green "Installed 3 executables: mactime, whenwhat, ww"
else
yellow "Note: Start a new terminal session to use mactime."
fi
}
# Main function
main() {
# Create bin directory
mkdir -p ~/.local/bin
BIN_DIR="$HOME/.local/bin"
VERBOSE=false
while getopts ":v" opt; do
case ${opt} in
v ) VERBOSE=true ;;
\? ) echo "Usage: $0 [-v]" >&2; exit 1 ;;
esac
done
check_dependencies
setup_source_location
setup_symlinks
setup_path
verify_installation
}
# Execute main function
main "$@"