-
Notifications
You must be signed in to change notification settings - Fork 2
/
install
142 lines (116 loc) · 3.3 KB
/
install
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
#!/usr/bin/env bash
# shellcheck disable=SC2145
set -euo pipefail
IFS=$'\n\t'
normal=$'\e[0m'
bold=$'\e[1m'
dimmed=$'\e[2m'
red=$'\e[31m'
green=$'\e[92m'
cyan=$'\e[96m'
log() {
echo "$@" 1>&2
}
info() {
log "${dimmed}==> $@${normal}"
}
success() {
log "${cyan}✓ $@${normal}"
}
throw() {
log " ${red}Error: $@${normal}"
exit 1
}
yn() {
# shellcheck disable=SC2064
trap "echo $normal" SIGINT
read -r -n 1 -p "${green}${bold}?${normal} $1 [y/n]: ${cyan}${bold}" yn
echo "${normal}"
trap - SIGINT
if [[ "$yn" != [Yy]* ]]; then
return 1
fi
}
# ==========================
# Install icsp
# ==========================
REPO_ZIP_DOWNLOAD_URL="https://github.com/loteoo/icsp/archive/refs/heads/main.zip"
BIN_HOME="${XDG_BIN_HOME:-$HOME/.local/bin}"
SCRIPT_NAME="icsp"
INSTALL_PATH="$BIN_HOME/$SCRIPT_NAME-main"
SCRIPT_FILE_PATH="$INSTALL_PATH/$SCRIPT_NAME"
SYMLINK_PATH="$BIN_HOME/$SCRIPT_NAME"
## Validation steps
## ==================
# Validate dependencies
for dependency in curl unzip awk date sed tr; do
if ! command -v $dependency &> /dev/null; then
throw "\"$dependency\" command is not available. Installation aborted."
fi
done
# Warn reinstall if already installed
if [[ -f "$SYMLINK_PATH" || -d "$INSTALL_PATH" || -n "$(which $SCRIPT_NAME)" ]]; then
if ! yn "$SCRIPT_NAME already exists. Uninstall before reinstall?"; then
throw "Installation aborted."
fi
if [[ -f "$SYMLINK_PATH" ]]; then
info "Remove symlink..."
rm "$SYMLINK_PATH"
fi
if [[ -d "$INSTALL_PATH" ]]; then
info "Remove script files..."
rm -r "$INSTALL_PATH"
fi
if [[ -n "$(which $SCRIPT_NAME)" ]]; then
info "Remove previous installation..."
rm "$(which $SCRIPT_NAME)"
fi
success "Uninstalled $SCRIPT_NAME."
fi
# Confirm install
if ! yn "Install $SCRIPT_NAME?"; then
throw "Installation aborted."
fi
# Create bin directory if needed.
if [[ ! -d "$BIN_HOME" ]]; then
if ! yn "Create \"$BIN_HOME\" directory?"; then
throw "Installation aborted."
fi
info "Create \"$BIN_HOME\"..."
mkdir -p "$BIN_HOME"
# Add to bin directory to $PATH if needed.
if ! echo "$PATH" | tr ':' $'\n' | grep -q "$BIN_HOME"; then
profile_file=".bashrc"
if [[ -f "$HOME/.zshrc" ]]; then
profile_file=".zshrc"
fi
if ! yn "\"$BIN_HOME\" is not in \$PATH. Add to path via $profile_file?"; then
throw "Installation aborted."
fi
bash -c "echo 'export PATH=\$PATH:$BIN_HOME' >> $HOME/$profile_file"
info "\"$BIN_HOME\" was added to \$PATH. Shell session restart required."
fi
success "Created \"$BIN_HOME\" directory."
fi
## Install steps
## ==================
info "Downloading $REPO_ZIP_DOWNLOAD_URL..."
curl -fsSL "$REPO_ZIP_DOWNLOAD_URL" -o "$INSTALL_PATH.zip"
info "Unpacking to $INSTALL_PATH..."
unzip -q "$INSTALL_PATH.zip" -d "$BIN_HOME"
info "Delete archive..."
rm "$INSTALL_PATH.zip"
info "Make script executable..."
chmod +x "$SCRIPT_FILE_PATH"
info "Create symbolic link at $SYMLINK_PATH..."
ln -s "$SCRIPT_FILE_PATH" "$SYMLINK_PATH"
## Post-install
## ==================
success "$SCRIPT_NAME installed! 🎉"
log "Give $SCRIPT_NAME it a star on Github if you like it! 🙏"
if [[ -z "$(which "$SCRIPT_NAME")" ]]; then
log "Please restart your terminal to make $SCRIPT_NAME available."
exit
fi
info "Running \"$SCRIPT_NAME -h\"..."
eval "$SCRIPT_NAME -h"