getmicro

An install script for the micro text editor. - forked from github.com/benweissmann/getmic.ro
git clone git://src.gearsix.net/getmicro
Log | Files | Refs | Atom

commit c35d7b3b7ee65f3afc432dd72bad4d679af73318
Author: gearsix <gearsix@tuta.io>
Date:   Sun, 13 Mar 2022 18:34:22 +0000

made quite a few changes, see below.

- removed a lot of the verbose error printing
- added printError
- a lot of tidyup & comments at each step
- install to /usr/local/bin by default (requires sudo)
  - added checkPermissions to handle this
- install is only performed if micro is not installed or if installed version < githubLatestTag result
- removed all the stuff about setting alternative-default, etc

Diffstat:
Agetmicro.sh | 178+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 178 insertions(+), 0 deletions(-)

diff --git a/getmicro.sh b/getmicro.sh @@ -0,0 +1,177 @@ +#!/bin/sh + +# This script installs micro. +# Modified from https://getmic.ro by gearsix + +printError() { + >&2 echo $1 +} + +checkPermissions() { + if [ ! -r $GETMICRO_INSTALLDIR ] || [ ! -w $GETMICRO_INSTALLDIR ]; then + printError "'$GETMICRO_INSTALLDIR': Permission denied" + exit + fi +} + +githubLatestTag() { + latestJSON="$( eval "$http 'https://api.github.com/repos/$1/releases/latest'" 2>/dev/null )" || true + + versionNumber='' + if ! echo "$latestJSON" | grep 'API rate limit exceeded' >/dev/null 2>&1 ; then + if ! versionNumber="$( echo "$latestJSON" | grep -oEm1 '[0-9]+[.][0-9]+[.][0-9]+' - 2>/dev/null )" ; then + versionNumber='' + fi + fi + + if [ "${versionNumber:-x}" = "x" ] ; then + # Try to fallback to previous latest version detection method if curl is available + if command -v curl >/dev/null 2>&1 ; then + if finalUrl="$( curl "https://github.com/$1/releases/latest" -s -L -I -o /dev/null -w '%{url_effective}' 2>/dev/null )" ; then + trimmedVers="${finalUrl##*v}" + if [ "${trimmedVers:-x}" != "x" ] ; then + echo "$trimmedVers" + exit 0 + fi + fi + fi + + printError "HTTP File download failed." + exit 1 + else + echo "$versionNumber" + fi +} + + +# get HTTP fetch command +if command -v curl >/dev/null 2>&1 ; then + http="curl -L" +elif command -v wget >/dev/null 2>&1 ; then + http="wget -O-" +else + printError "Couldn't find commands 'curl' or 'wget' on your system." + exit 1 +fi + +# get platform +platform='' +machine=$(uname -m) + +if [ "${GETMICRO_PLATFORM:-x}" != "x" ]; then + platform="$GETMICRO_PLATFORM" +else + case "$(uname -s | tr '[:upper:]' '[:lower:]')" in + "linux") + case "$machine" in + "arm64"* | "aarch64"* ) platform='linux-arm64' ;; + "arm"* | "aarch"*) platform='linux-arm' ;; + *"86") platform='linux32' ;; + *"64") platform='linux64' ;; + esac + ;; + "darwin") platform='osx' ;; + *"freebsd"*) + case "$machine" in + *"86") platform='freebsd32' ;; + *"64") platform='freebsd64' ;; + esac + ;; + "openbsd") + case "$machine" in + *"86") platform='openbsd32' ;; + *"64") platform='openbsd64' ;; + esac + ;; + "netbsd") + case "$machine" in + *"86") platform='netbsd32' ;; + *"64") platform='netbsd64' ;; + esac + ;; + "msys"*|"cygwin"*|"mingw"*|*"_nt"*|"win"*) + case "$machine" in + *"86") platform='win32' ;; + *"64") platform='win64' ;; + esac + ;; + esac +fi + +if [ "${platform:-x}" = "x" ]; then + printError "Couldn't automatically detect operating system. +Set GETMICRO_PLATFORM to one of the following values: + +- freebsd32 +- freebsd64 +- linux-arm +- linux-arm64 +- linux32 +- linux64 +- netbsd32 +- netbsd64 +- openbsd32 +- openbsd64 +- osx +- win32 +- win64 +" + exit 1 +fi + +# get latest tag version +TAG=$(githubLatestTag zyedidia/micro) + +if command -v grep >/dev/null 2>&1 ; then + if ! echo "v$TAG" | grep -E '^v[0-9]+[.][0-9]+[.][0-9]+$' >/dev/null 2>&1 ; then + printError "Recieved an invalid tag and cannot be sure that the tag will not break this script." + echo "> $TAG" 1>&2 + exit 1 + fi +fi + +# check if latestTag already installed +if [ $(command -v micro) ] && [ "$(micro -version | grep $TAG)" != "" ]; then exit; fi + +# get file extension +if [ "${platform:-x}" = "win64" ] || [ "${platform:-x}" = "win32" ]; then + extension='zip' +else + extension='tar.gz' +fi + +# if Musl libc; use the staticly-compiled versioon +if [ "${platform:-x}" = "linux64" ]; then + # Detect musl libc (source: https://stackoverflow.com/a/60471114) + libc=$(ldd /bin/ls | grep 'musl' | head -1 | cut -d ' ' -f1) + if [ -n "$libc" ]; then + platform='linux64-static' + fi +fi + +# set GETMICRO_INSTALLDIR +GETMICRO_INSTALLDIR=/usr/local/bin + +if [ -d "$1" ]; then GETMICRO_INSTALLDIR="$1"; fi + +checkPermissions + +echo "Detected platform: $platform" +echo "Latest Version: $TAG" +echo "Downloading https://github.com/zyedidia/micro/releases/download/v$TAG/micro-$TAG-$platform.$extension" + +eval "$http 'https://github.com/zyedidia/micro/releases/download/v$TAG/micro-$TAG-$platform.$extension'" > "micro.$extension" + +case "$extension" in + "zip") unzip -j "micro.$extension" -d "micro-$TAG" ;; + "tar.gz") tar -xvzf "micro.$extension" "micro-$TAG/micro" ;; +esac + +mv "micro-$TAG/micro" ./micro + +rm "micro.$extension" +rm -rf "micro-$TAG" + +mv ./micro $GETMICRO_INSTALLDIR/micro + +echo "Micro has been installed successfully." +\ No newline at end of file