diff --git a/installer.sh b/installer.sh index f4b89e7d4e97e472f8a1152fcfa39afd4167a5ae..973a939429630891d51f0590847f42bf489d194a 100644 --- a/installer.sh +++ b/installer.sh @@ -3,193 +3,199 @@ # # Distributed under terms of the GPLv3 license. -{ \unalias command; \unset -f command; } >/dev/null 2>&1 +{ + \unalias command + \unset -f command +} >/dev/null 2>&1 tdir='' gitedu_release_url="https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest" gitedu_prealpha_url="https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest" gitedu_url=$gitedu_release_url cleanup_success() { - printf "The dojo is installed in %s\nMake sure %s is in your path\n" "$dest" "$dest" - cleanup + printf "The dojo is installed in %s\nMake sure %s is in your path\n" "$dest" "$destdir" + printf "If you are using bash you may want to append:\n" + printf "export PATH=\"\$PATH:%s\" to your .bashrc file.\n" "$destdir" + cleanup } cleanup() { - [ -n "$tdir" ] && { - printf "Cleaning temporary files in %s\n" "$tdir" - command rm -rf "$tdir" - tdir='' - } + [ -n "$tdir" ] && { + printf "Cleaning temporary files in %s\n" "$tdir" + command rm -rf "$tdir" + tdir='' + } } die() { - cleanup - printf "\033[31m%s\033[m\n\r" "$*" > /dev/stderr; - exit 1; + cleanup + printf "\033[31m%s\033[m\n\r" "$*" >/dev/stderr + exit 1 } detect_jq_tool() { - if command -v jq 2> /dev/null > /dev/null; then - get_version() { - command jq -r '."assets"."links"[0].url' | command awk -F '/' '{print $(NF-1)}' - } - else - die "jq not available on your system cannot get dojo version" - fi + if command -v jq 2>/dev/null >/dev/null; then + get_version() { + command jq -r '."assets"."links"[0].url' | command awk -F '/' '{print $(NF-1)}' + } + else + die "jq not available on your system cannot get dojo version" + fi } - detect_network_tool() { - if command -v curl 2> /dev/null > /dev/null; then - fetch() { - command curl -fL "$1" - } - fetch_and_get_version() { - command curl -fL "$1" | get_version - } - - fetch_quiet() { - command curl -fsSL "$1" - } - fetch_and_get_version_quiet() { - command curl -fsSL "$1" | get_version - } - elif command -v wget 2> /dev/null > /dev/null; then - fetch() { - command wget -O- "$1" - } - fetch_and_get_version() { - command wget -O- "$1" | get_version - } - - fetch_quiet() { - command wget --quiet -O- "$1" - } - fetch_and_get_version_quiet() { - command wget --quiet -O- "$1" | get_version - } - else - die "Neither curl nor wget available. Cannot download dojo. Install any of the or see $gitedu_url to download manually" - fi + if command -v curl 2>/dev/null >/dev/null; then + fetch() { + command curl -fL "$1" + } + fetch_and_get_version() { + command curl -fL "$1" | get_version + } + + fetch_quiet() { + command curl -fsSL "$1" + } + fetch_and_get_version_quiet() { + command curl -fsSL "$1" | get_version + } + elif command -v wget 2>/dev/null >/dev/null; then + fetch() { + command wget -O- "$1" + } + fetch_and_get_version() { + command wget -O- "$1" | get_version + } + + fetch_quiet() { + command wget --quiet -O- "$1" + } + fetch_and_get_version_quiet() { + command wget --quiet -O- "$1" | get_version + } + else + die "Neither curl nor wget available. Cannot download dojo. Install any of the or see $gitedu_url to download manually" + fi } - detect_os() { - arch="" - case "$(command uname)" in - 'Darwin') OS="macos" - case "$(command uname -m)" in - amd64|x86_64) arch="x64";; - aarch64*) arch="arm64";; - armv8*) arch="arm64";; - arm64) arch="arm64";; - *) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually";; - esac - ;; - 'Linux') - OS="linux" - case "$(command uname -m)" in - amd64|x86_64) arch="x64";; - aarch64*) arch="arm64";; - armv8*) arch="arm64";; - arm64) arch="arm64";; - *) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually";; - esac - ;; - *) die "dojo binaries are not available for $(command uname). See $gitedu_url to download manually" - esac + arch="" + case "$(command uname)" in + 'Darwin') + OS="macos" + case "$(command uname -m)" in + amd64 | x86_64) arch="x64" ;; + aarch64*) arch="arm64" ;; + armv8*) arch="arm64" ;; + arm64) arch="arm64" ;; + *) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually" ;; + esac + ;; + 'Linux') + OS="linux" + case "$(command uname -m)" in + amd64 | x86_64) arch="x64" ;; + aarch64*) arch="arm64" ;; + armv8*) arch="arm64" ;; + arm64) arch="arm64" ;; + *) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually" ;; + esac + ;; + *) die "dojo binaries are not available for $(command uname). See $gitedu_url to download manually" ;; + esac } expand_tilde() { - tilde_less="${1#\~/}" - [ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less" - printf '%s' "$tilde_less" + tilde_less="${1#\~/}" + [ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less" + printf '%s' "$tilde_less" } parse_args() { - dest='~/.local/bin' - [ "$OS" = "macos" ] && dest="/usr/local/bin" - installer='' - while :; do - case "$1" in - dest=*) dest="${1#*=}";; - installer=*) installer="${1#*=}";; - "") break;; - *) die "Unrecognized command line option: $1";; - esac - shift - done - dest=$(expand_tilde "${dest}") - dest="$dest/dojo" + destdir='~/.local/bin' + [ "$OS" = "macos" ] && destdir="/usr/local/bin" + installer='' + while :; do + case "$1" in + destdir=*) destdir="${1#*=}" ;; + installer=*) installer="${1#*=}" ;; + "") break ;; + *) die "Unrecognized command line option: $1" ;; + esac + shift + done + destdir=$(expand_tilde "${destdir}") + if [ ! -d "$destdir" ]; then + printf "Directory %s does not exist. Creatring %s now.\n" "$destdir" "$destdir" + mkdir -p "$destdir" + fi + dest="$destdir/dojo" } - get_file_url() { - url="https://gitedu.hesge.ch/api/v4/projects/12972/packages/generic/dojo" - if [ "$OS" = "macos" ]; then - url="$url$1_macOS-$arch/$1/dojo$1" - else - url="$url$1_Linux-$arch/$1/dojo$1" - fi + url="https://gitedu.hesge.ch/api/v4/projects/12972/packages/generic/dojo" + if [ "$OS" = "macos" ]; then + url="$url$1_macOS-$arch/$1/dojo$1" + else + url="$url$1_Linux-$arch/$1/dojo$1" + fi } get_release_url() { - release_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Latest) - [ $? -ne 0 -o -z "$release_version" ] && die "Could not get dojo latest release version" - get_file_url "$release_version" + release_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Latest) + [ $? -ne 0 -o -z "$release_version" ] && die "Could not get dojo latest release version" + get_file_url "$release_version" } - get_prealpha_url() { - gitedu_url=$gitedu_prealpha_url - prealpha_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Pre-alpha) - [ $? -ne 0 -o -z "$prealpha_version" ] && die "Could not get dojo pre-alpha release version" - get_file_url "$prealpha_version" + gitedu_url=$gitedu_prealpha_url + prealpha_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Pre-alpha) + [ $? -ne 0 -o -z "$prealpha_version" ] && die "Could not get dojo pre-alpha release version" + get_file_url "$prealpha_version" } get_download_url() { - case "$installer" in - "pre-alpha") get_prealpha_url ;; - "") get_release_url ;; - *) ;; - esac + case "$installer" in + "pre-alpha") get_prealpha_url ;; + "") get_release_url ;; + *) ;; + esac } download_installer() { - tdir=$(command mktemp -d "/tmp/dojo-install-XXXXXXXXXXXX") - printf '%s\n' "Downloading from: $url" - if [ "$OS" = "macos" ]; then - installer="$tdir/dojo" - else - installer="$tdir/dojo" - fi - fetch "$url" > "$installer" || die "Failed to download: $url" - printf '%s\n\n' "File saved in: $installer" + tdir=$(command mktemp -d "/tmp/dojo-install-XXXXXXXXXXXX") + printf '%s\n' "Downloading from: $url" + if [ "$OS" = "macos" ]; then + installer="$tdir/dojo" + else + installer="$tdir/dojo" + fi + fetch "$url" >"$installer" || die "Failed to download: $url" + printf '%s\n\n' "File saved in: $installer" } linux_install() { - command chmod +x "$installer" || die "Failed to chmod +x $installer" - command mv "$installer" "$dest" || die "Failed to move $installer" + command chmod +x "$installer" || die "Failed to chmod +x $installer" + command mv "$installer" "$dest" || die "Failed to move $installer" } macos_install() { - command chmod +x "$installer" || die "Failed to chmod +x $installer for MacOS" - command mv "$installer" "$dest" || die "Failed to move $installer to $dest for MacOS" + command chmod +x "$installer" || die "Failed to chmod +x $installer for MacOS" + command mv "$installer" "$dest" || die "Failed to move $installer to $dest for MacOS" } main() { - detect_os - parse_args "$@" - detect_jq_tool - detect_network_tool - get_download_url - download_installer - if [ "$OS" = "macos" ]; then - macos_install - else - linux_install - fi - cleanup_success - exit 0 + detect_os + parse_args "$@" + detect_jq_tool + detect_network_tool + get_download_url + download_installer + if [ "$OS" = "macos" ]; then + macos_install + else + linux_install + fi + cleanup_success + exit 0 } main "$@"