#!/usr/bin/env bash
##
##
##
##
##
: ==========================================
: Introduction
: ==========================================
# COPYRIGHT NOTICE
# This script page is strongly based on the job done by the Firebase Team.
# However, this is by no means a version of the Firebase Tools, but an
# internal CLI for the Unreal Urban Team.
#
# Note: The implementation of the underlying CLI was entirely written using
# Dart CLI instead of NodeJS as the Firebase Tools.
#
# This script allows you to install the latest version of the
# "unreal" command by running:
#
: curl -sL https://cli.levenup.com | bash
#
: ==========================================
: Advanced Usage
: ==========================================
# The behavior of this script can be modified at runtime by passing environmental
# variables to the `bash` process.
#
# For example, passing an argument called arg1 set to true and one called arg2 set
# to false would look like this.
#
: curl -sL https://cli.levenup.com | arg1=true arg2=false bash
#
# These arguments are optional, but be aware that explicitly setting them will help
# ensure consistent behavior if / when defaults are changed.
#
: -----------------------------------------
: Installing a specific version
: -----------------------------------------
# You can provide a version tag through the variable "version".
# If the script finds any previous version installed, by default, the provided
# version won't be installed.
# To force the installation of the provided version, hence removing the
# previous binary, provide and set the variable "force" to true.
#
: curl -sL https://cli.levenup.com | version=1.0.0 bash
: curl -sL https://cli.levenup.com | version=1.0.0 force=true bash
#
# Caution: Your environment and workspace might be in an unpredicted state
# by downgrading a version.
#
: -----------------------------------------
: Upgrading - default: false
: -----------------------------------------
# By default, this script will not replace an existing CLI install.
# If you'd like to upgrade an existing install, set the "upgrade" variable to true.
#
: curl -sL https://cli.levenup.com | upgrade=true bash
#
# This operation could (potentially) break an existing install, although we
# attempt to backup the previous binary, so use it with caution.
#
: -----------------------------------------
: Uninstalling - default false
: -----------------------------------------
# You can remove the binary by passing the "uninstall" flag.
#
: curl -sL https://cli.levenup.com | uninstall=true bash
#
# This will remove the binary file.
# Any workspace or environment settings will remain in your system.
#
: ==========================================
: Source Code
: ==========================================
API_ADDRESS="${api:-https://cli.levenup.com}"
echo
echo "UNREAL CLI BOOSTRAP"
echo "--------------------"
echo
echo "We will perform operations that might need you to enter your password."
echo "You can see what we exactly do in this script at https://cli.levenup.com"
echo
# This script contains a large amount of comments so you can understand
# how it interacts with your system. If you're not interested in the
# technical details, you can just run the command above.
# If the user asked for us to uninstall the CLI, then do so.
# shellcheck disable=SC2154
if [ "$uninstall" = "true" ]; then
echo "-- Removing binary file..."
sudo rm "$(which unreal)"
echo "-- Unreal CLI has been uninstalled"
echo "-- All Done!"
exit 0
fi
# For info about why we place the binary at this location, see
# https://unix.stackexchange.com/a/8658
INSTALL_DIR="/usr/local/bin"
# We need to ensure that the INSTALL_DIR exists.
# On some platforms like the Windows Subsystem for Linux it may not.
# We created it using a non-destructive mkdir command.
sudo mkdir -p "$INSTALL_DIR"
# We need to ensure that we don't mess up an existing CLI
# install, so before doing anything we check to see if this system
# has "unreal" installed and if so, we exit out.
echo "-- Checking for existing CLI on PATH..."
HAS_CLI=$(which unreal)
if [ -n "$HAS_CLI" ]; then
INSTALLED_CLI_VERSION=$(unreal --version)
# In the case of a corrupt CLI install, we wont be able to
# retrieve a version number, so to keep the logs correct, we refer to
# your existing install as either the CLI version or as a "corrupt install"
if [ -n "$INSTALLED_CLI_VERSION" ]; then
CLI_NICKNAME="unreal ($INSTALLED_CLI_VERSION)"
else
CLI_NICKNAME="a corrupted CLI binary"
fi
# We are only capable of upgrading installs of the standalone binary version of the CLI
# If the user didn't pass upgrade=true or force=true, then we print the command to do an upgrade and exit
# shellcheck disable=SC2154
if [ ! "$upgrade" = "true" ] && [ ! "$force" = "true" ]; then
echo "Your machine has $CLI_NICKNAME installed."
echo "If you would like to upgrade your install run: curl -sL https://cli.levenup.com | upgrade=true bash"
exit 0
else
# If the user did pass upgrade=true or force=true, then we allow the script to continue and overwrite the install.
echo "-- Your machine has $CLI_NICKNAME, attempting overwrite..."
fi
fi
# If the user provided a specific version, providing the previous condition check with
# force=true passed, then we set the install version to the one provided.
INSTALL_VERSION='latest'
if [ -n "$version" ]; then
echo "-- Installing version $version..."
INSTALL_VERSION="$version"
fi
echo "-- Checking your machine type..."
# Now we need to detect the platform we're running on (Linux / Mac / Other)
# so we can fetch the correct binary and place it in the correct location
# on the machine.
# We use "tr" to translate the uppercase "uname" output into lowercase
UNAME=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
# Then we map the output to the names used on the Github releases page
case "$UNAME" in
linux*) MACHINE=ubuntu;;
darwin*) MACHINE=macos;;
esac
if [ -n "$MACHINE" ]
then
case "$ARCH" in
x86*) MACHINE="$MACHINE-x86";;
arm*) MACHINE="$MACHINE-arm";;
amd*) MACHINE="$MACHINE-amd";;
esac
fi
# If we never define the $MACHINE variable (because our platform is neither Mac
# or Linux), then we can't finish our job, so just log out a helpful message
# and close.
if [ -z "$MACHINE" ]
then
echo "Your operating system $UNAME ($ARCH) is not supported."
echo "-- All done!"
exit 0
fi
# We have enough information to generate the binary's download URL.
DOWNLOAD_URL="$API_ADDRESS/bin/$MACHINE/$INSTALL_VERSION"
echo "-- Downloading binary from $DOWNLOAD_URL"
# We use "curl" to download the binary with a flag set to follow redirects
# (Github download URLs redirect to CDNs) and a flag to show a progress bar.
sudo curl -o "$INSTALL_DIR/unreal.tar.gz" -L --fail --progress-bar "$DOWNLOAD_URL" || exit 1;
# Make a backup of the current binary, if any
if [ -n "$HAS_CLI" ]; then
sudo cp "$INSTALL_DIR/unreal" "$INSTALL_DIR/unreal_backup"
fi
# Extract the compressed file and rename the binary to "unreal".
sudo tar -zxf "$INSTALL_DIR/unreal.tar.gz" -C "$INSTALL_DIR" > /dev/null || exit 1;
sudo mv "$INSTALL_DIR/unreal.exe" "$INSTALL_DIR/unreal" > /dev/null
sudo rm -f "$INSTALL_DIR/unreal.tar.gz" > /dev/null
# Once the download is complete, we mark the binary file as readable
# and executable (+rx).
echo "-- Setting permissions on binary..."
sudo chmod +rx "$INSTALL_DIR/unreal" > /dev/null
# If all went well, the "unreal" binary should be located on our PATH so
# we'll run it once, asking it to print out the version. This is helpful as
# standalone CLI binaries do a small amount of setup on the initial run
# so this not only allows us to make sure we got the right version, but it
# also does the setup so the first time the developer runs the binary, it'll
# be faster.
VERSION=$("$INSTALL_DIR/unreal" --version)
# If no version is detected then clearly the binary failed to install for
# some reason, so we'll log out an error message.
if [ -z "$VERSION" ]
then
echo "Something went wrong, CLI has not been installed."
if [ -n "$HAS_CLI" ]; then
echo "Rolling back the previous binary..."
sudo mv "$INSTALL_DIR/unreal_backup" "$INSTALL_DIR/unreal"
fi
echo "-- Installation failed!"
exit 1
fi
# If the installation succeeded, remove the backup, if any
sudo rm -f "$INSTALL_DIR/unreal_backup" > /dev/null
# In order for the user to be able to actually run the "unreal" command
# without specifying the absolute location, the INSTALL_DIR path must
# be present inside of the PATH environment variable.
echo "-- Checking your PATH variable..."
if [[ ! ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
echo ""
echo "It looks like $INSTALL_DIR isn't on your PATH."
echo "Please add the following line to either your ~/.profile or ~/.bash_profile or ~/.bashrc or ~/.zshrc, then restart your terminal."
echo ""
echo "PATH=\$PATH:$INSTALL_DIR"
echo ""
echo "For more information about modifying PATHs, see https://unix.stackexchange.com/a/26059"
echo ""
fi
# Since we've gotten this far we know everything succeeded. We'll just
# let the developer know everything is ready and take our leave.
echo "-- unreal@$VERSION is now installed"
echo "-- All done!"
echo