installer.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_nightly_url() {
  91. get_file_url "nightly" "nightly"
  92. }
  93. get_download_url() {
  94. installer_is_file="n"
  95. case "$installer" in
  96. "nightly") get_nightly_url ;;
  97. "") get_release_url ;;
  98. *) installer_is_file="y" ;;
  99. esac
  100. }
  101. download_installer() {
  102. tdir=$(command mktemp -d "/tmp/kitty-install-XXXXXXXXXXXX")
  103. [ "$installer_is_file" != "y" ] && {
  104. printf '%s\n\n' "Downloading from: $url"
  105. if [ "$OS" = "macos" ]; then
  106. installer="$tdir/kitty.dmg"
  107. else
  108. installer="$tdir/kitty.txz"
  109. fi
  110. fetch "$url" > "$installer" || die "Failed to download: $url"
  111. installer_is_file="y"
  112. }
  113. }
  114. linux_install() {
  115. command mkdir "$tdir/mp"
  116. command tar -C "$tdir/mp" "-xJof" "$installer" || die "Failed to extract kitty tarball"
  117. printf "%s\n" "Installing to $dest"
  118. command rm -rf "$dest" || die "Failed to delete $dest"
  119. command mv "$tdir/mp" "$dest" || die "Failed to move kitty.app to $dest"
  120. }
  121. macos_install() {
  122. command mkdir "$tdir/mp"
  123. command hdiutil attach "$installer" "-mountpoint" "$tdir/mp" || die "Failed to mount kitty.dmg"
  124. printf "%s\n" "Installing to $dest"
  125. command rm -rf "$dest"
  126. command mkdir -p "$dest" || die "Failed to create the directory: $dest"
  127. command ditto -v "$tdir/mp/kitty.app" "$dest"
  128. rc="$?"
  129. command hdiutil detach "$tdir/mp"
  130. [ "$rc" != "0" ] && die "Failed to copy kitty.app from mounted dmg"
  131. }
  132. exec_kitty() {
  133. if [ "$OS" = "macos" ]; then
  134. exec "open" "$dest"
  135. else
  136. exec "$dest/bin/kitty" "--detach"
  137. fi
  138. die "Failed to launch kitty"
  139. }
  140. main() {
  141. detect_os
  142. parse_args "$@"
  143. detect_network_tool
  144. get_download_url
  145. download_installer
  146. if [ "$OS" = "macos" ]; then
  147. macos_install
  148. else
  149. linux_install
  150. fi
  151. cleanup
  152. [ "$launch" = "y" ] && exec_kitty
  153. exit 0
  154. }
  155. main "$@"