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

getmicro.sh (4443B)


      1 #!/bin/sh
      2 
      3 # This script installs micro.
      4 # Modified from https://getmic.ro by gearsix
      5 
      6 printError() {
      7 	>&2 echo $1
      8 }
      9 
     10 checkPermissions() {
     11 	if [ ! -r $GETMICRO_INSTALLDIR ] || [ ! -w $GETMICRO_INSTALLDIR ]; then
     12 		printError "'$GETMICRO_INSTALLDIR': Permission denied"
     13 		exit
     14 	fi
     15 }
     16 
     17 githubLatestTag() {
     18   latestJSON="$( eval "$http 'https://api.github.com/repos/$1/releases/latest'" 2>/dev/null )" || true
     19   
     20   versionNumber=''
     21   if ! echo "$latestJSON" | grep 'API rate limit exceeded' >/dev/null 2>&1 ; then
     22     if ! versionNumber="$( echo "$latestJSON" | grep -oEm1 '[0-9]+[.][0-9]+[.][0-9]+' - 2>/dev/null )" ; then
     23       versionNumber=''
     24     fi
     25   fi
     26   
     27   if [ "${versionNumber:-x}" = "x" ] ; then
     28     # Try to fallback to previous latest version detection method if curl is available
     29     if command -v curl >/dev/null 2>&1 ; then
     30       if finalUrl="$( curl "https://github.com/$1/releases/latest" -s -L -I -o /dev/null -w '%{url_effective}' 2>/dev/null )" ; then
     31         trimmedVers="${finalUrl##*v}"
     32         if [ "${trimmedVers:-x}" != "x" ] ; then
     33           echo "$trimmedVers"
     34           exit 0
     35         fi
     36       fi
     37     fi
     38     
     39     printError "HTTP File download failed."
     40     exit 1
     41   else
     42     echo "$versionNumber"
     43   fi
     44 }
     45 
     46 
     47 # get HTTP fetch command
     48 if command -v curl >/dev/null 2>&1 ; then
     49   http="curl -L"
     50 elif command -v wget >/dev/null 2>&1 ; then
     51   http="wget -O-"
     52 else
     53   printError "Couldn't find commands 'curl' or 'wget' on your system."
     54   exit 1
     55 fi
     56 
     57 # get platform
     58 platform=''
     59 machine=$(uname -m)
     60 
     61 if [ "${GETMICRO_PLATFORM:-x}" != "x" ]; then
     62   platform="$GETMICRO_PLATFORM"
     63 else
     64   case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
     65     "linux")
     66       case "$machine" in
     67         "arm64"* | "aarch64"* ) platform='linux-arm64' ;;
     68         "arm"* | "aarch"*) platform='linux-arm' ;;
     69         *"86") platform='linux32' ;;
     70         *"64") platform='linux64' ;;
     71       esac
     72       ;;
     73     "darwin") platform='osx' ;;
     74     *"freebsd"*)
     75       case "$machine" in
     76         *"86") platform='freebsd32' ;;
     77         *"64") platform='freebsd64' ;;
     78       esac
     79       ;;
     80     "openbsd")
     81       case "$machine" in
     82         *"86") platform='openbsd32' ;;
     83         *"64") platform='openbsd64' ;;
     84       esac
     85       ;;
     86     "netbsd")
     87       case "$machine" in
     88         *"86") platform='netbsd32' ;;
     89         *"64") platform='netbsd64' ;;
     90       esac
     91       ;;
     92     "msys"*|"cygwin"*|"mingw"*|*"_nt"*|"win"*)
     93       case "$machine" in
     94         *"86") platform='win32' ;;
     95         *"64") platform='win64' ;;
     96       esac
     97       ;;
     98   esac
     99 fi
    100 
    101 if [ "${platform:-x}" = "x" ]; then
    102   printError "Couldn't automatically detect operating system.
    103 Set GETMICRO_PLATFORM to one of the following values:
    104 
    105 - freebsd32
    106 - freebsd64
    107 - linux-arm
    108 - linux-arm64
    109 - linux32
    110 - linux64
    111 - netbsd32
    112 - netbsd64
    113 - openbsd32
    114 - openbsd64
    115 - osx
    116 - win32
    117 - win64
    118 "
    119   exit 1
    120 fi
    121 
    122 # get latest tag version
    123 TAG=$(githubLatestTag zyedidia/micro)
    124 
    125 if command -v grep >/dev/null 2>&1 ; then
    126   if ! echo "v$TAG" | grep -E '^v[0-9]+[.][0-9]+[.][0-9]+$' >/dev/null 2>&1 ; then
    127     printError "Recieved an invalid tag and cannot be sure that the tag will not break this script."
    128     echo "> $TAG" 1>&2
    129     exit 1
    130   fi
    131 fi
    132 
    133 # check if latestTag already installed
    134 if [ $(command -v micro) ] && [ "$(micro -version | grep $TAG)" != "" ]; then exit; fi
    135 
    136 # get file extension
    137 if [ "${platform:-x}" = "win64" ] || [ "${platform:-x}" = "win32" ]; then
    138   extension='zip'
    139 else
    140   extension='tar.gz'
    141 fi
    142 
    143 # if Musl libc; use the staticly-compiled versioon
    144 if [ "${platform:-x}" = "linux64" ]; then
    145   # Detect musl libc (source: https://stackoverflow.com/a/60471114)
    146   libc=$(ldd /bin/ls | grep 'musl' | head -1 | cut -d ' ' -f1)
    147   if [ -n "$libc" ]; then
    148     platform='linux64-static'
    149   fi
    150 fi
    151 
    152 # set GETMICRO_INSTALLDIR
    153 GETMICRO_INSTALLDIR=/usr/local/bin
    154 
    155 if [ -d "$1" ]; then GETMICRO_INSTALLDIR="$1"; fi
    156 
    157 checkPermissions
    158 
    159 echo "Detected platform: $platform"
    160 echo "Latest Version: $TAG"
    161 echo "Downloading https://github.com/zyedidia/micro/releases/download/v$TAG/micro-$TAG-$platform.$extension"
    162 
    163 eval "$http 'https://github.com/zyedidia/micro/releases/download/v$TAG/micro-$TAG-$platform.$extension'" > "micro.$extension"
    164 
    165 case "$extension" in
    166   "zip") unzip -j "micro.$extension" -d "micro-$TAG" ;;
    167   "tar.gz") tar -xvzf "micro.$extension" "micro-$TAG/micro" ;;
    168 esac
    169 
    170 mv "micro-$TAG/micro" ./micro
    171 
    172 rm "micro.$extension"
    173 rm -rf "micro-$TAG"
    174 
    175 mv ./micro $GETMICRO_INSTALLDIR/micro
    176 
    177 echo "Micro has been installed successfully."