#!/usr/bin/env bash set -eo pipefail DOWNLOAD_DOMAIN="https://release.infinilabs.com" print_usage() { echo "Usage: curl -sSL https://get.infini.cloud | bash -s -- [-p program_name] [-v program_version] [-d install_dir] [-s]" echo "Options:" echo " -p, --program-name Name of the program to install which default is console" echo " -v, --version Version of the program to install which default is latest" echo " -d, --install-dir Directory of the program install which default is /opt/program" echo " -s, --snapshot Force to install from snapshot repository" exit 1 } print_header() { echo " " echo " @@@@@@@@@@@" echo " @@@@@@@@@@@@" echo " @@@@@@@@@@@@" echo " @@@@@@@@@&@@@" echo " #@@@@@@@@@@@@@" echo " @@@ @@@@@@@@@@@@@ " echo " &@@@@@@@ &@@@@@@@@@@@@@ " echo " @&@@@@@@@&@ @@@&@@@@@@@&@ " echo " @@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@ " echo " @@@@@@@@@@@@@@@@@@& @@@@@@@@@@@@@ " echo " %@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " echo " @@@@@@@@@@@@&@@@@@@@@@@@@@@@ " echo " @@ ,@@@@@@@@@@@@@@@@@@@@@@@& " echo " @@@@@. @@@@@&@@@@@@@@@@@@@@ " echo " @@@@@@@@@@ @@@@@@@@@@@@@@@# " echo " @&@@@&@@@&@@@ &@&@@@&@@@&@ " echo " @@@@@@@@@@@@@. @@@@@@@* " echo " @@@@@@@@@@@@@ %@@@ " echo " @@@@@@@@@@@@@ " echo "/@@@@@@@&@@@@@ " echo "@@@@@@@@@@@@@ " echo "@@@@@@@@@@@@@ " echo "@@@@@@@@@@@@ Welcome to INFINI Labs!" echo "" echo "" echo "Now attempting the installation... " echo "" } print_footprint() { echo " __ _ __ ____ __ _ __ __ " echo " / // |/ // __// // |/ // / " echo " / // || // _/ / // || // / " echo "/_//_/|_//_/ /_//_/|_//_/ " echo "" echo "©INFINI.LTD, All Rights Reserved." echo "" } __try() { if [[ $try_status -eq 0 ]]; then ! exception=$( $@ 2>&1 >/dev/null ) try_status=${PIPESTATUS[0]} fi } __catch() { _old_try=$try_status try_status=0 [[ $_old_try -ne 0 ]] } get_latest_version() { echo $(curl -m30 -sk "${DOWNLOAD_DOMAIN}/.latest" |sed 's/",/"/;s/"//g;s/://1' |grep -Ev '^[{}]' |grep "$program_name" |awk '{print $NF}') } check_dir() { if [[ "${install_dir}" != /* ]]; then install_dir="$(pwd)/${install_dir}" fi if [[ ! -d "${install_dir}" ]]; then __try mkdir -p "${install_dir}" if __catch e; then echo -e "Error: Unable to create installation directory, please manually create and reinstall.\nsudo mkdir -p ${install_dir} && sudo chown -R \$(whoami) ${install_dir}" >&2; exit 1; fi fi install_dir=$(realpath "${install_dir}") owner=$(ls -ld "${install_dir}" |awk '{print $3}') if [[ "${owner}" != "$(whoami)" ]]; then echo -e "Error: The installation directory ${install_dir} should be owner by current user.\nsudo chown -R \$(whoami) ${install_dir}" >&2; exit 1; fi if [[ "$(ls -A ${install_dir})" ]]; then echo "Error: The installation directory ${install_dir} should be clean." >&2; exit 1; fi } compare_versions() { local v1_base=$(echo "$1" | cut -d'-' -f1) local v2_base=$(echo "$2" | cut -d'-' -f1) [[ "$v1_base" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] && [[ "$v2_base" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "Error: Invalid version format. Expected major.minor.patch" >&2 exit 1 } IFS=. read -r v1_major v1_minor v1_patch <<< "$v1_base" IFS=. read -r v2_major v2_minor v2_patch <<< "$v2_base" (( v1_major > v2_major )) && echo "greater" && return (( v1_major < v2_major )) && echo "less" && return (( v1_minor > v2_minor )) && echo "greater" && return (( v1_minor < v2_minor )) && echo "less" && return (( v1_patch > v2_patch )) && echo "greater" && return (( v1_patch < v2_patch )) && echo "less" && return echo "equal" } check_platform() { local platform=$(uname) local arch=$(uname -m) case $platform in "Linux") case $arch in "i386"|"i686"|"x86") file_ext="linux-386.tar.gz" ;; "x86_64"|"amd64") file_ext="linux-amd64.tar.gz" ;; "aarch64"|"arm64") file_ext="linux-arm64.tar.gz" ;; "armv5tel") file_ext="linux-armv5.tar.gz" ;; "armv6l") file_ext="linux-armv6.tar.gz" ;; "armv7"|"armv7l") file_ext="linux-armv7.tar.gz" ;; "mips"|"mipsel") file_ext="linux-mips.tar.gz" ;; "mips64") file_ext="linux-mips64.tar.gz" ;; "mips64el") file_ext="linux-mips64le.tar.gz" ;; "loong64"|"loongarch64") file_ext="linux-loong64.tar.gz" ;; "sw_64") file_ext="linux-sw64.tar.gz" ;; "riscv64") file_ext="linux-riscv64.tar.gz" ;; *) echo "Unsupported architecture: ${arch}" >&2 exit 1 ;; esac ;; "Darwin") case $arch in "x86_64"|"amd64") file_ext="mac-amd64.zip" ;; "arm64") file_ext="mac-arm64.zip" ;; *) echo "Unsupported architecture: ${arch}" >&2 exit 1 ;; esac ;; "MINGW"*|"WSL"*|"Cygwin") case $arch in "i386"|"i686") file_ext="windows-386.zip" ;; "x86_64"|"amd64") file_ext="windows-amd64.zip" ;; *) echo "Unsupported architecture: ${arch}" >&2 exit 1 ;; esac ;; *) echo "Unsupported platform: ${platform}" >&2 exit 1 ;; esac } install_binary() { type="stable" if [[ "$force_snapshot" == "true" ]]; then type="snapshot" elif [[ "$version" =~ NIGHTLY ]] || [[ "$version" =~ SNAPSHOT ]]; then type="snapshot" elif [[ -n "$version" ]] && [[ -n "$latest_version" ]]; then local result=$(compare_versions "$version" "$latest_version") if [[ "$result" == "greater" ]]; then type="snapshot" fi fi tmp_dir="$(mktemp -d)" cd "$tmp_dir" local filename="${program_name}-${version}-${file_ext}" perform_download() { local current_type=$1 local repo_path="${program_name}/${current_type}" if [[ "$program_name" == "cloud" ]]; then repo_path=".${program_name}/${current_type}" fi local url="${DOWNLOAD_DOMAIN}/${repo_path}/${program_name}-${version}-${file_ext}" echo "File: [$url]" if command -v curl >/dev/null 2>&1; then # -f: fail silently on server errors (404), important for fallback logic curl -f -S -# -kLO "$url" elif command -v wget >/dev/null 2>&1; then wget -q -nc --show-progress --progress=bar:force:noscroll "$url" else echo "Error: Could not find curl or wget, Please install wget or curl in advance." >&2; return 1; fi } if ! perform_download "$type"; then if [[ "$type" == "snapshot" ]]; then fallback_type="stable" else fallback_type="snapshot" fi echo "" echo "Download from [${type}] repository failed. Trying [${fallback_type}] repository..." if ! perform_download "$fallback_type"; then echo "" echo "Error: Download failed from both snapshot and stable repositories." >&2 echo "Please check if the version [${version}] exists or network connection." >&2 exit 1 fi fi if [[ ! -s "${filename}" ]]; then echo "Error: Downloaded file is empty or does not exist." >&2 exit 1 fi if [[ "${file_ext}" == *".tar.gz" ]]; then if ! tar -xzf "${filename}" -C "$install_dir" >/dev/null 2>&1; then echo "Error: Failed to extract tar.gz archive. The file might be corrupted." >&2 exit 1 fi else if ! unzip -q "${filename}" -d "$install_dir" >/dev/null 2>&1; then echo "Error: Failed to extract zip archive. The file might be corrupted." >&2 exit 1 fi fi cd "${install_dir}" && rm -rf "${tmp_dir}" } main() { force_snapshot="false" while [[ $# -gt 0 ]]; do case "$1" in -p|--program-name) program_name="$2"; shift 2 ;; -v|--version) version="$2"; shift 2 ;; -d|--install-dir) install_dir="$2"; shift 2 ;; -s|--snapshot) force_snapshot="true"; shift 1 ;; *) print_usage ;; esac done program_name=${program_name:-console} install_dir=${install_dir:-/opt/$program_name} latest_version=$(get_latest_version) version=${version:-$latest_version} if [[ -n "$latest_version" && ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+-[0-9]+$ ]]; then version="$latest_version" fi file_ext="" if [[ -z "${version}" ]]; then echo "Error: Could not obtain the latest version number. Please check the network and try again.">&2; exit 1; else echo "Name: [${program_name}], Version: [${version}], Path: [${install_dir}]" fi check_dir check_platform install_binary [[ "$program_name" == "easysearch" ]] && exe_name="bin/initialize.sh && bin/${program_name}" || exe_name=./${program_name}-${file_ext%%.*} echo "" echo "Installation complete. [${program_name}] is ready to use!" echo "" echo "" echo "----------------------------------------------------------------" echo "cd ${install_dir} && ${exe_name}" echo "----------------------------------------------------------------" echo "" echo "" print_footprint } print_header main "$@"