-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
155 lines (133 loc) · 3.79 KB
/
Copy pathinstall.sh
File metadata and controls
155 lines (133 loc) · 3.79 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
#!/bin/sh
set -e
fmt() {
FMT_TYPE="$1"
case "$FMT_TYPE" in
TIT) shift && printf "\033[34m--- %s ---\033[0m\n" "$*" && return ;;
SUC) shift && printf "\033[32m%s\033[0m " "$FMT_TYPE" ;;
ERR) shift && printf "\033[31m%s\033[0m " "$FMT_TYPE" ;;
WRN | TIP) shift && printf "\033[33m%s\033[0m " "$FMT_TYPE" ;;
INF) shift && printf "\033[37m%s\033[0m " "$FMT_TYPE" ;;
esac
printf "%s\n" "$*"
}
yes() {
case "$1" in
[yY] | [yY][eE][sS]) return 0 ;;
*) return 1 ;;
esac
}
pkg_install() {
if type apt-get >/dev/null 2>&1; then
apt-get update || true
apt-get install -q -y "$@"
elif type dnf >/dev/null 2>&1; then
dnf install -y "$@"
elif type yum >/dev/null 2>&1; then
yum install -y "$@"
elif type apk >/dev/null 2>&1; then
apk add -q "$@"
elif type zypper >/dev/null 2>&1; then
zypper -q install -y "$@"
elif type pacman >/dev/null 2>&1; then
pacman -Syu --noconfirm "$@"
else
return 1
fi
}
usage() {
cat <<EOF
Usage: $0 [options]
Options:
-f, --force Force install dependencies
--cdn Use CDN for script downloads
-h, --help Show this help
EOF
}
check_missing_commands() {
MISSING=0
for ITEM in "$@"; do
CMD_PATH=$(command -v "$ITEM" 2>/dev/null || true)
if [ -n "$CMD_PATH" ]; then
CMD_PATH=$(readlink -f "$CMD_PATH" 2>/dev/null || true)
fi
case "$CMD_PATH" in
"")
fmt TIP "Command missing: $ITEM"
;;
*/busybox)
fmt TIP "Command provided by busybox: $ITEM"
;;
*) continue ;;
esac
MISSING=1
done
return $MISSING
}
install_dependencies() {
fmt TIT "Install Dependencies"
set -- "wget" "unshare"
if [ "$FLAG_FORCE" -eq 1 ]; then
fmt INF "Force install dependencies..."
elif ! check_missing_commands "$@"; then
fmt INF "Missing some dependencies, trying to install..."
else
fmt INF "Founded all dependencies, skip"
return 0
fi
set -- "wget" "util-linux"
if pkg_install "$@"; then
fmt SUC "Installed:" "$@"
else
fmt ERR "Failed to install dependencies" >&2
fmt TIP "Please install by yourself:" "$@"
exit 1
fi
}
install_scripts() {
fmt TIT "Download & Install"
PREFIX="/opt/wsl-init" LINK_PREFIX="/usr/local/bin"
if [ "$FLAG_CDN" -eq 1 ]; then
fmt INF "Use CDN"
BASE_URL="https://cdn.jsdelivr.net/gh/pierreown/wsl-init@main"
else
BASE_URL="https://raw.githubusercontent.com/pierreown/wsl-init/main"
fi
# Cleanup old files
rm -rf "$PREFIX" && mkdir -p "$PREFIX"
# Download Scripts
set -- wsl-init.sh wsl-init-boot.sh wsl-init-enter.sh wsl-init-enter.profile.sh
for ITEM in "$@"; do
if wget -q -t 3 -w 1 -T 5 -O "$PREFIX/$ITEM" "$BASE_URL/$ITEM"; then
chmod 755 "$PREFIX/$ITEM"
fmt SUC "Downloaded $PREFIX/$ITEM"
else
fmt ERR "Failed to download $PREFIX/$ITEM" >&2 && exit 1
fi
done
# Create Symlink
set -- wsl-init.sh wsl-init-boot.sh wsl-init-enter.sh
for ITEM in "$@"; do
ln -sf "$PREFIX/$ITEM" "$LINK_PREFIX/${ITEM%.sh}"
fmt SUC "Linked $PREFIX/$ITEM => $LINK_PREFIX/${ITEM%.sh}"
done
}
main() {
# Parse Options
eval set -- "$(getopt -o ':fh' --long 'force,cdn,help' -- "$@" 2>/dev/null)"
FLAG_FORCE=0 FLAG_CDN=0
while true; do
case "$1" in
--) shift && break ;;
-f | --force) FLAG_FORCE="1" ;;
--cdn) FLAG_CDN="1" ;;
-h | --help) usage && exit ;;
esac
shift
done
install_dependencies
install_scripts
}
# Check User
[ "$(id -u)" -ne 0 ] && echo "Please run as root" >&2 && exit 1
main "$@"