-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·166 lines (144 loc) · 3.85 KB
/
install.sh
File metadata and controls
executable file
·166 lines (144 loc) · 3.85 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env sh
set -eu
REPO="barnphp/cnkill"
INSTALL_MODE="local"
INSTALL_DIR=""
usage() {
cat <<'EOF'
Install cnkill from the latest GitHub release.
Usage:
install.sh [--system] [--dir PATH]
Options:
--system Install to /usr/local/bin (uses sudo if needed)
--dir PATH Install to a custom directory
-h, --help Show this help message
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
--system)
INSTALL_MODE="system"
;;
--dir)
shift
if [ "$#" -eq 0 ]; then
echo "Error: --dir requires a path." >&2
exit 1
fi
INSTALL_DIR="$1"
;;
-h|--help)
usage
exit 0
;;
*)
echo "Error: unknown option '$1'" >&2
usage >&2
exit 1
;;
esac
shift
done
if [ -z "$INSTALL_DIR" ]; then
if [ "$INSTALL_MODE" = "system" ]; then
INSTALL_DIR="/usr/local/bin"
else
INSTALL_DIR="$HOME/.local/bin"
fi
fi
if [ -z "${HOME:-}" ]; then
echo "Error: HOME is not set." >&2
exit 1
fi
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required for installation." >&2
exit 1
fi
UNAME_S=$(uname -s)
UNAME_M=$(uname -m)
case "$UNAME_S" in
Linux)
OS="linux"
;;
Darwin)
OS="macos"
;;
*)
echo "Error: unsupported OS '$UNAME_S'. Supported: Linux, macOS." >&2
exit 1
;;
esac
case "$UNAME_M" in
x86_64|amd64)
ARCH="x86_64"
;;
aarch64|arm64)
ARCH="aarch64"
;;
*)
echo "Error: unsupported architecture '$UNAME_M'. Supported: x86_64, aarch64." >&2
exit 1
;;
esac
ASSET_NAME="cnkill-${OS}-${ARCH}"
LATEST_RELEASE_API="https://api.github.com/repos/${REPO}/releases/latest"
TAG_NAME=$(
curl -fsSL "$LATEST_RELEASE_API" \
| sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' \
| sed -n '1p'
)
if [ -z "$TAG_NAME" ]; then
echo "Error: failed to determine latest release tag." >&2
exit 1
fi
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG_NAME}/${ASSET_NAME}"
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
echo "Downloading ${ASSET_NAME} (${TAG_NAME})..."
if ! curl -fsSL "$DOWNLOAD_URL" -o "$TMP_DIR/cnkill"; then
echo "Error: failed to download ${ASSET_NAME} from release ${TAG_NAME}." >&2
exit 1
fi
mkdir -p "$INSTALL_DIR"
if [ "$INSTALL_MODE" = "system" ]; then
if [ "$(id -u)" -eq 0 ]; then
install -m 0755 "$TMP_DIR/cnkill" "$INSTALL_DIR/cnkill"
elif command -v sudo >/dev/null 2>&1; then
sudo install -m 0755 "$TMP_DIR/cnkill" "$INSTALL_DIR/cnkill"
else
echo "Error: system install requires root or sudo." >&2
exit 1
fi
else
install -m 0755 "$TMP_DIR/cnkill" "$INSTALL_DIR/cnkill"
fi
echo "cnkill installed at: $INSTALL_DIR/cnkill"
if [ "$INSTALL_MODE" = "local" ]; then
case ":$PATH:" in
*":$INSTALL_DIR:"*)
echo "You're all set. Run: cnkill"
;;
*)
RC_FILE="$HOME/.profile"
case "${SHELL:-}" in
*/zsh) RC_FILE="$HOME/.zshrc" ;;
*/bash) RC_FILE="$HOME/.bashrc" ;;
esac
echo ""
echo "Add this line to $RC_FILE so cnkill is available everywhere:"
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
echo "Then restart your shell or run:"
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
;;
esac
else
case ":$PATH:" in
*":$INSTALL_DIR:"*)
echo "You're all set. Run: cnkill"
;;
*)
echo "Note: '$INSTALL_DIR' is not currently in PATH for this shell session."
echo "Open a new terminal, or run cnkill via: $INSTALL_DIR/cnkill"
;;
esac
fi