Cuckoo/run

106 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
SCRIPT_PATH="$(dirname "$0")"
FILE_NAME="cuckoo_generator"
FILE_PATH="$SCRIPT_PATH/$FILE_NAME"
GREP_OPTIONS=""
function perform_curl {
if [[ ! -z "$GITHUB_ACCESS_TOKEN" ]]; then
REQUEST_HEADER="-H 'Authorization: token ${GITHUB_ACCESS_TOKEN}'"
echo "$REQUEST_HEADER"
fi
echo "$1"
DOWNLOAD_URL=$(curl "$REQUEST_HEADER" "$1" | grep -oe '"browser_download_url":\s*"[^" ]*"' | grep -oe 'http[^" ]*' | grep "$FILE_NAME" | head -1)
echo "$DOWNLOAD_URL"
curl "$REQUEST_HEADER" -Lo "$FILE_NAME" "$DOWNLOAD_URL"
}
function download_generator {
if [[ "$1" = "latest" ]]; then
echo "Downloading latest version..."
perform_curl "https://api.github.com/repos/Brightify/Cuckoo/releases/latest"
else
echo "Downloading version $1..."
perform_curl "https://api.github.com/repos/Brightify/Cuckoo/releases/tags/$1"
fi
chmod +x "$FILE_NAME"
}
function build_generator {
pushd "$SCRIPT_PATH"
if [[ ! -z "$1" ]]; then
download_generator "$1"
else
if [[ -d "$SCRIPT_PATH/Generator" ]]; then
echo "Building..."
./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/.build/release/$FILE_NAME" "$FILE_PATH"
else
echo "Couldn't build. Generator source code not found. (expected in the `Generator` directory)"
download_generator "$2"
fi
fi
popd
}
# parse arguments
POSITIONAL=()
while [[ $# -gt 0 ]]
do
case $1 in
-d|--download)
if [[ "$2" = --* ]]; then
DOWNLOAD="latest"
else
DOWNLOAD=$(echo "$2" | grep -e "\d\d*\.\d\d*\.\d\d*")
if [[ -z $DOWNLOAD ]]; then
echo "Invalid Cuckoo Generator version: $2; Please use the semantic versioning. (e.g. 0.12.0)"
exit 1
fi
shift
fi
shift
;;
-c|--clean)
CLEAN=1
shift
;;
*)
POSITIONAL+=("$1") # save it in an array for later
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ ! -e "$FILE_PATH" ]] || [[ ! -z "$CLEAN" ]]; then
echo "No Cuckoo Generator found."
build_generator "$DOWNLOAD"
fi
INPUT_FILES=""
if [[ "$#" > 0 ]]; then
INPUT_FILES=$(printf '%q ' "$@")
fi
if [[ -z "$SCRIPT_INPUT_FILE_COUNT" ]]; then
SCRIPT_INPUT_FILE_COUNT=0
fi
for (( i=0; i<"$SCRIPT_INPUT_FILE_COUNT"; i++ ))
do
INPUT_FILE="SCRIPT_INPUT_FILE_$i"
INPUT_FILES+=" $(printf '%q' "${!INPUT_FILE}")"
done
echo $INPUT_FILES | xargs "$FILE_PATH"