123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #!/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."
|