-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·126 lines (97 loc) · 3.04 KB
/
install.sh
File metadata and controls
executable file
·126 lines (97 loc) · 3.04 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
#!/usr/bin/env bash
PKG="m-cli"
GIT_URL="https://github.com/rgcr/m-cli.git"
INSTALL_DIR="${INSTALL_DIR:-${HOME}/.local/opt/m-cli}"
BIN_DIR="${BIN_DIR:-${HOME}/.local/bin}"
LINK_NAME="m"
has(){
command -v "$1" &>/dev/null
}
die() {
echo >&2 "ERROR: ${@}"
exit 1
}
infomsg() {
echo "INFO: ${@}"
}
ensure_dirs() {
mkdir -p ${HOME}/.local/opt
mkdir -p ${HOME}/.local/bin
}
prechecks(){
if ! has "git"; then
die "git is not installed. Please install git before running this script."
fi
ensure_dirs
}
install_or_update_from_git(){
if [ -e "${INSTALL_DIR}" ]; then
# package is installed, we just update it
infomsg "${PKG} is already installed at ${INSTALL_DIR}"
infomsg "Updating ${PKG} from git"
command git --git-dir="${INSTALL_DIR}/.git" --work-tree="${INSTALL_DIR}" fetch --depth=1 || \
die "Failed to fetch changes => ${GIT_URL}"
command git --git-dir="${INSTALL_DIR}/.git" --work-tree="${INSTALL_DIR}" reset --hard origin/main || \
die "Failed to reset changes => ${GIT_URL}"
else
# package is not installed, we clone it
infomsg "Downloading ${PKG} from git to ${INSTALL_DIR}"
command git clone --depth 1 ${GIT_URL} ${INSTALL_DIR} || \
die "Failed to clone => ${GIT_URL}"
fi
}
create_symlink() {
if [ ! -f "${INSTALL_DIR}/m" ]; then
die "Executable not found at ${INSTALL_DIR}/m"
fi
infomsg "Creating symlink ${BIN_DIR}/${LINK_NAME} -> ${INSTALL_DIR}/m"
ln -sf "${INSTALL_DIR}/m" "${BIN_DIR}/${LINK_NAME}" || die "Failed to create symlink"
}
caveats() {
cat <<_EOF_
NOTE:
Please make sure that ${BIN_DIR} is in your PATH.
You can add it by running:
export PATH="${BIN_DIR}:\$PATH"
Or by adding the above line to your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc).
Some commands are executed with 'sudo' internally because they require root privileges to work correctly.
Therefore, make sure you have sudo privileges to use this tool effectively.
_EOF_
}
__uninstall_mcli() {
if [ ! -e "${INSTALL_DIR}" ]; then
infomsg "${PKG} is not installed, nothing to do."
return 0
fi
rm -rf "${INSTALL_DIR}" 2>/dev/null || die "Failed to remove ${INSTALL_DIR}"
rm -f "${BIN_DIR}/${LINK_NAME}" || die "Failed to remove symlink ${BIN_DIR}/${LINK_NAME}"
}
usage(){
cat <<_EOF_
Usage: install.sh [--reinstall|--help]
Installs or updates ${PKG} in ${INSTALL_DIR} and creates a symlink in ${BIN_DIR}.
Options:
--help Show this help message
--reinstall Force reinstallation of ${PKG} even if it is already installed.
_EOF_
}
main(){
prechecks
local force_install=0
case "$1" in
--help|-h)
usage && exit 0
;;
--reinstall)
force_install=1
;;
esac
if [ ${force_install} -ne 0 ]; then
__uninstall_mcli
fi
install_or_update_from_git
create_symlink
infomsg "${PKG} has been successfully installed in ${INSTALL_DIR}"
caveats
}
main $@