-
Notifications
You must be signed in to change notification settings - Fork 174
/
run
executable file
·97 lines (83 loc) · 2.56 KB
/
run
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
#!/usr/bin/env bash
SCRIPT_PATH="$(cd "$(dirname "$0")" && pwd)"
GENERATOR_NAME="cuckoonator"
GENERATOR_PATH="$SCRIPT_PATH/$GENERATOR_NAME"
GREP_OPTIONS=""
echo "Script path: $SCRIPT_PATH"
echo "Generator path: $GENERATOR_PATH"
function download_generator {
echo "Downloading generator version $1..."
if [[ ! -z "$GITHUB_ACCESS_TOKEN" ]]; then
CURL_OPTIONS=(-H "Authorization: token $GITHUB_ACCESS_TOKEN")
fi
URL="https://api.github.com/repos/Brightify/Cuckoo/releases/tags/$1"
DOWNLOAD_URL=$(curl "${CURL_OPTIONS[@]}" "$URL" | grep -oe '"browser_download_url":\s*"[^" ]*"' | grep -oe 'http[^" ]*' | grep "$GENERATOR_NAME" | head -1)
if [[ -z "$DOWNLOAD_URL" ]]; then
echo "Error: Failed to fetch download URL for the Cuckoo Generator."
exit 1
else
echo "Downloading Cuckoo Generator from URL: $DOWNLOAD_URL"
curl "${CURL_OPTIONS[@]}" -Lo "$GENERATOR_PATH" "$DOWNLOAD_URL"
fi
chmod +x "$GENERATOR_NAME"
}
function get_generator {
pushd "$SCRIPT_PATH"
if [[ ! -z "$DOWNLOAD" ]]; then
download_generator "$LIB_VERSION"
else
if [[ -d "$SCRIPT_PATH/Generator" ]]; then
echo "Building generator..."
./build_generator
if [[ "$?" -ne 0 ]]; then
echo "Build seems to have failed for some reason. Please file an issue on GitHub."
exit 1
fi
mv "$SCRIPT_PATH/Generator/bin/$GENERATOR_NAME" "$GENERATOR_PATH"
else
echo "Couldn't build generator – source code not found. (expected in the 'Generator' directory)"
echo "Pass the --download option to fetch the binary from GitHub."
exit 1
fi
fi
popd
}
# Source: https://stackoverflow.com/a/58333404/11558478
function ver_cmp {
local IFS=.
local V1=($1) V2=($2) I
for ((I=0 ; I<${#V1[*]} || I<${#V2[*]} ; I++)) ; do
[[ ${V1[$I]:-0} -lt ${V2[$I]:-0} ]] && echo -1 && return
[[ ${V1[$I]:-0} -gt ${V2[$I]:-0} ]] && echo 1 && return
done
echo 0
}
function ver_lt {
[[ $(ver_cmp "$1" "$2") -eq -1 ]]
}
# parse arguments
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case $1 in
-d|--download)
DOWNLOAD=1
shift
;;
-c|--clean)
CLEAN=1
echo 'Performing clean generator build.'
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac; done
set -- "${POSITIONAL[@]}"
LIB_VERSION=$(<version)
GEN_VERSION=$("$GENERATOR_PATH" --version)
# Procure a generator if it's not available, if clean build is requested, or if the generator version is older than library version.
if [[ ! -e "$GENERATOR_PATH" ]] || [[ ! -z "$CLEAN" ]] || ver_lt "$GEN_VERSION" "$LIB_VERSION"; then
get_generator
fi
"$GENERATOR_PATH" $@