installer.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. i386) arch="i686";;
  48. i686) arch="i686";;
  49. *) die "Unknown CPU architecture $(command uname -m)";;
  50. esac
  51. ;;
  52. *) die "kitty binaries are not available for $(command uname)"
  53. esac
  54. }
  55. expand_tilde() {
  56. tilde_less="${1#\~/}"
  57. [ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less"
  58. printf '%s' "$tilde_less"
  59. }
  60. parse_args() {
  61. dest='~/.local'
  62. [ "$OS" = "macos" ] && dest="/Applications"
  63. launch='y'
  64. installer=''
  65. while :; do
  66. case "$1" in
  67. dest=*) dest="${1#*=}";;
  68. launch=*) launch="${1#*=}";;
  69. installer=*) installer="${1#*=}";;
  70. "") break;;
  71. *) die "Unrecognized command line option: $1";;
  72. esac
  73. shift
  74. done
  75. dest=$(expand_tilde "${dest}")
  76. [ "$launch" != "y" -a "$launch" != "n" ] && die "Unrecognized command line option: launch=$launch"
  77. dest="$dest/kitty.app"
  78. }
  79. get_file_url() {
  80. url="https://github.com/kovidgoyal/kitty/releases/download/$1/kitty-$2"
  81. if [ "$OS" = "macos" ]; then
  82. url="$url.dmg"
  83. else
  84. url="$url-$arch.txz"
  85. fi
  86. }
  87. get_release_url() {
  88. release_version=$(fetch_quiet "https://sw.kovidgoyal.net/kitty/current-version.txt")
  89. [ $? -ne 0 -o -z "$release_version" ] && die "Could not get kitty latest release version"
  90. get_file_url "v$release_version" "$release_version"
  91. }
  92. get_nightly_url() {
  93. get_file_url "nightly" "nightly"
  94. }
  95. get_download_url() {
  96. installer_is_file="n"
  97. case "$installer" in
  98. "nightly") get_nightly_url ;;
  99. "") get_release_url ;;
  100. *) installer_is_file="y" ;;
  101. esac
  102. }
  103. download_installer() {
  104. tdir=$(command mktemp -d "/tmp/kitty-install-XXXXXXXXXXXX")
  105. [ "$installer_is_file" != "y" ] && {
  106. printf '%s\n\n' "Downloading from: $url"
  107. if [ "$OS" = "macos" ]; then
  108. installer="$tdir/kitty.dmg"
  109. else
  110. installer="$tdir/kitty.txz"
  111. fi
  112. fetch "$url" > "$installer" || die "Failed to download: $url"
  113. installer_is_file="y"
  114. }
  115. }
  116. linux_install() {
  117. command mkdir "$tdir/mp"
  118. command tar -C "$tdir/mp" "-xJof" "$installer" || die "Failed to extract kitty tarball"
  119. printf "%s\n" "Installing to $dest"
  120. command rm -rf "$dest" || die "Failed to delete $dest"
  121. command mv "$tdir/mp" "$dest" || die "Failed to move kitty.app to $dest"
  122. }
  123. macos_install() {
  124. command mkdir "$tdir/mp"
  125. command hdiutil attach "$installer" "-mountpoint" "$tdir/mp" || die "Failed to mount kitty.dmg"
  126. printf "%s\n" "Installing to $dest"
  127. command rm -rf "$dest"
  128. command mkdir -p "$dest" || die "Failed to create the directory: $dest"
  129. command ditto -v "$tdir/mp/kitty.app" "$dest"
  130. rc="$?"
  131. command hdiutil detach "$tdir/mp"
  132. [ "$rc" != "0" ] && die "Failed to copy kitty.app from mounted dmg"
  133. }
  134. exec_kitty() {
  135. if [ "$OS" = "macos" ]; then
  136. exec "open" "$dest"
  137. else
  138. exec "$dest/bin/kitty" "--detach"
  139. fi
  140. die "Failed to launch kitty"
  141. }
  142. main() {
  143. detect_os
  144. parse_args "$@"
  145. detect_network_tool
  146. get_download_url
  147. download_installer
  148. if [ "$OS" = "macos" ]; then
  149. macos_install
  150. else
  151. linux_install
  152. fi
  153. cleanup
  154. [ "$launch" = "y" ] && exec_kitty
  155. exit 0
  156. }
  157. main "$@"