getmicro.sh 4.3 KB

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