installer.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/bin/sh
  2. # Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
  3. #
  4. # Distributed under terms of the GPLv3 license.
  5. { \unalias command; \unset -f command; } >/dev/null 2>&1
  6. tdir=''
  7. cleanup() {
  8. [ -n "$tdir" ] && {
  9. command rm -rf "$tdir"
  10. tdir=''
  11. }
  12. }
  13. die() {
  14. cleanup
  15. printf "\033[31m%s\033[m\n\r" "$*" > /dev/stderr;
  16. exit 1;
  17. }
  18. detect_network_tool() {
  19. if command -v curl 2> /dev/null > /dev/null; then
  20. fetch() {
  21. command curl -fL "$1"
  22. }
  23. fetch_quiet() {
  24. command curl -fsSL "$1"
  25. }
  26. elif command -v wget 2> /dev/null > /dev/null; then
  27. fetch() {
  28. command wget -O- "$1"
  29. }
  30. fetch_quiet() {
  31. command wget --quiet -O- "$1"
  32. }
  33. else
  34. die "Neither curl nor wget available, cannot download kitty"
  35. fi
  36. }
  37. detect_os() {
  38. arch=""
  39. case "$(command uname)" in
  40. 'Darwin') OS="macos";;
  41. 'Linux')
  42. OS="linux"
  43. case "$(command uname -m)" in
  44. amd64|x86_64) arch="x86_64";;
  45. aarch64*) arch="arm64";;
  46. armv8*) arch="arm64";;
  47. *) die "kitty binaries not available for architecture $(command uname -m)";;
  48. esac
  49. ;;
  50. *) die "kitty binaries are not available for $(command uname)"
  51. esac
  52. }
  53. expand_tilde() {
  54. tilde_less="${1#\~/}"
  55. [ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less"
  56. printf '%s' "$tilde_less"
  57. }
  58. parse_args() {
  59. dest='~/.local'
  60. [ "$OS" = "macos" ] && dest="/Applications"
  61. launch='y'
  62. installer=''
  63. while :; do
  64. case "$1" in
  65. dest=*) dest="${1#*=}";;
  66. launch=*) launch="${1#*=}";;
  67. installer=*) installer="${1#*=}";;
  68. "") break;;
  69. *) die "Unrecognized command line option: $1";;
  70. esac
  71. shift
  72. done
  73. dest=$(expand_tilde "${dest}")
  74. [ "$launch" != "y" -a "$launch" != "n" ] && die "Unrecognized command line option: launch=$launch"
  75. dest="$dest/kitty.app"
  76. }
  77. get_file_url() {
  78. url="https://github.com/kovidgoyal/kitty/releases/download/$1/kitty-$2"
  79. if [ "$OS" = "macos" ]; then
  80. url="$url.dmg"
  81. else
  82. url="$url-$arch.txz"
  83. fi
  84. }
  85. get_release_url() {
  86. release_version=$(fetch_quiet "https://sw.kovidgoyal.net/kitty/current-version.txt")
  87. [ $? -ne 0 -o -z "$release_version" ] && die "Could not get kitty latest release version"
  88. get_file_url "v$release_version" "$release_version"
  89. }
  90. get_version_url() {
  91. get_file_url "v$1" "$1"
  92. }
  93. get_nightly_url() {
  94. get_file_url "nightly" "nightly"
  95. }
  96. get_download_url() {
  97. installer_is_file="n"
  98. case "$installer" in
  99. "nightly") get_nightly_url ;;
  100. "") get_release_url ;;
  101. version-*) get_version_url "${installer#*-}";;
  102. *) installer_is_file="y" ;;
  103. esac
  104. }
  105. download_installer() {
  106. tdir=$(command mktemp -d "/tmp/kitty-install-XXXXXXXXXXXX")
  107. [ "$installer_is_file" != "y" ] && {
  108. printf '%s\n\n' "Downloading from: $url"
  109. if [ "$OS" = "macos" ]; then
  110. installer="$tdir/kitty.dmg"
  111. else
  112. installer="$tdir/kitty.txz"
  113. fi
  114. fetch "$url" > "$installer" || die "Failed to download: $url"
  115. installer_is_file="y"
  116. }
  117. }
  118. ensure_dest() {
  119. printf "%s\n" "Installing to $dest"
  120. command rm -rf "$dest" || die "Failed to delete $dest"
  121. command mkdir -p "$dest" || die "Failed to mkdir -p $dest"
  122. command rm -rf "$dest" || die "Failed to delete $dest"
  123. }
  124. linux_install() {
  125. command mkdir "$tdir/mp"
  126. command tar -C "$tdir/mp" "-xJof" "$installer" || die "Failed to extract kitty tarball"
  127. ensure_dest
  128. command mv "$tdir/mp" "$dest" || die "Failed to move kitty.app to $dest"
  129. }
  130. macos_install() {
  131. command mkdir "$tdir/mp"
  132. command hdiutil attach "$installer" "-mountpoint" "$tdir/mp" || die "Failed to mount kitty.dmg"
  133. ensure_dest
  134. command ditto -v "$tdir/mp/kitty.app" "$dest"
  135. rc="$?"
  136. command hdiutil detach "$tdir/mp"
  137. [ "$rc" != "0" ] && die "Failed to copy kitty.app from mounted dmg"
  138. }
  139. exec_kitty() {
  140. if [ "$OS" = "macos" ]; then
  141. exec "open" "$dest"
  142. else
  143. exec "$dest/bin/kitty" "--detach"
  144. fi
  145. die "Failed to launch kitty"
  146. }
  147. main() {
  148. detect_os
  149. parse_args "$@"
  150. detect_network_tool
  151. get_download_url
  152. download_installer
  153. if [ "$OS" = "macos" ]; then
  154. macos_install
  155. else
  156. linux_install
  157. fi
  158. cleanup
  159. [ "$launch" = "y" ] && exec_kitty
  160. exit 0
  161. }
  162. main "$@"