Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·132 lines (109 loc) · 2.97 KB
/
Copy pathinstall
File metadata and controls
executable file
·132 lines (109 loc) · 2.97 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
#!/bin/env bash
cat <<EOF
-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
BY URIID1
Source https://github.com/uriid1/bash-tools
-=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
EOF
readonly BIN_PATH="${HOME}/.local/bin/uriid1/bin"
readonly EXIT_SUCCESS=0
readonly EXIT_FAILURE=1
log() {
echo -e "\033[1;32m-->\033[0m $1"
}
warn() {
echo -e "\033[1;33m[!]\033[0m $1"
}
#
# Checking that base tools installed
#
base_tools=(adb dig iptables wc sysctl curl openssl python bc readlink lsblk column jq)
for ((i = 0; i < ${#base_tools[*]}; ++i)); do
tool="${base_tools[$i]}"
if ! [ "$(which -a $tool . 2>/dev/null)" ]; then
warn "Not found: ${tool}"
fi
done
#
# Check / setup PATH
#
mkdir -p "${BIN_PATH}"
setup_path() {
# Already available in the current session
case ":${PATH}:" in
*":${BIN_PATH}:"*) return 0 ;;
esac
local shell_name rc cmd reload answer
shell_name="$(basename "${SHELL:-bash}")"
case "${shell_name}" in
fish)
rc="${HOME}/.config/fish/config.fish"
cmd="fish_add_path ${BIN_PATH}"
reload="exec fish (or open a new terminal)"
;;
zsh)
rc="${HOME}/.zshrc"
cmd="export PATH=\"\$PATH:${BIN_PATH}\""
reload="source ${rc} (or open a new terminal)"
;;
*)
shell_name="bash"
rc="${HOME}/.bashrc"
cmd="export PATH=\"\$PATH:${BIN_PATH}\""
reload="source ${rc} (or open a new terminal)"
;;
esac
# Already declared in the config — just needs a reload
if [ -f "${rc}" ] && grep -qF "${BIN_PATH}" "${rc}"; then
warn "${BIN_PATH} is set in ${rc} but not in the current shell."
warn "Run: ${reload}"
return 0
fi
warn "${BIN_PATH} is not in your \$PATH (shell: ${shell_name})."
# Non-interactive run: only print manual instructions
if [ ! -t 0 ]; then
warn "Add it to ${rc}: ${cmd}"
return 0
fi
printf '\033[1;33m[?]\033[0m Configure PATH for %s in %s? [y/N] ' "${shell_name}" "${rc}"
read -r answer
case "${answer}" in
y | Y | yes | YES)
mkdir -p "$(dirname "${rc}")"
printf '\n# Added by uriid1/bash-tools\n%s\n' "${cmd}" >> "${rc}"
log "Appended PATH config to ${rc}"
log "Run: ${reload} to apply"
;;
*)
warn "Skipped. Add to ${rc}: ${cmd}"
;;
esac
}
setup_path
#
# Ensure bash-libs submodule is present (scripts source it)
#
readonly LIB_SRC="vendor/bash-libs/lib"
readonly LIB_DST="${HOME}/.local/bin/uriid1/lib"
if [ ! -f "${LIB_SRC}/const.sh" ]; then
warn "bash-libs submodule is not initialized; fetching..."
if ! git submodule update --init --recursive; then
warn "Failed to initialize bash-libs. Run: git submodule update --init --recursive"
exit $EXIT_FAILURE
fi
fi
#
# Installing
#
chmod +x bin/*
for script in bin/*; do
name=$(basename $script)
log "Copy ${name} to ${BIN_PATH}"
cp $script ${BIN_PATH}
done
#
# Install bash-libs alongside scripts so that ../lib/bash-libs/lib resolves
#
log "Copy bash-libs to ${LIB_DST}"
mkdir -p "${LIB_DST}"
cp -a "${LIB_SRC}/." "${LIB_DST}/"