install.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/sh
  2. # Lyrebird installer script. If running as root then will install at /usr/local/{bin,share},
  3. # otherwise will install at ~/.local/{bin,share}.
  4. VERSION="1.1"
  5. VERBOSE=${VERBOSE:-1}
  6. DRYRUN=${DRYRUN:-0}
  7. # Initial setup
  8. if [ $DRYRUN = 1 ]; then DRYRUN_INFO=" (dryrun)"; fi
  9. ECHO_PREFIX="[lyrebird]$DRYRUN_INFO"
  10. info_echo() {
  11. echo "$ECHO_PREFIX $1"
  12. }
  13. warning_echo() {
  14. echo "[warning]$DRYRUN_INFO $1"
  15. }
  16. verbose_echo() {
  17. if [ $VERBOSE = 1 ]; then
  18. echo "$ECHO_PREFIX $1"
  19. fi
  20. }
  21. if [ "$(id -u)" -eq 0 ]; then
  22. INSTALL_PREFIX="${INSTALL_PREFIX:-/usr/local}"
  23. else
  24. INSTALL_PREFIX="${INSTALL_PREFIX:-$HOME/.local}"
  25. fi
  26. verbose_echo "Installing Lyrebird to prefix: ${INSTALL_PREFIX}"
  27. BIN_PATH="$INSTALL_PREFIX/bin"
  28. SHARE_PATH="$INSTALL_PREFIX/share/lyrebird"
  29. DESKTOP_PATH="$INSTALL_PREFIX/share/applications"
  30. python_version_check() {
  31. PYTHON_VERSION=$(python3 --version | grep -Po '3\.\d')
  32. PYTHON_MIN_MAJOR=3
  33. PYTHON_MIN_MINOR=7
  34. PYTHON_MAJOR=${PYTHON_VERSION%.*}
  35. PYTHON_MINOR=${PYTHON_VERSION#*.}
  36. invalid_python() {
  37. info_echo "Lyrebird requires Python version 3.7 or higher"
  38. }
  39. if [ "$PYTHON_MAJOR" -lt "$PYTHON_MIN_MAJOR" ]; then
  40. invalid_python
  41. exit 1
  42. elif [ "$PYTHON_MAJOR" -eq "$PYTHON_MIN_MAJOR" ] && [ "$PYTHON_MINOR" -lt "$PYTHON_MIN_MINOR" ]; then
  43. invalid_python
  44. exit 1
  45. fi
  46. }
  47. python_version_check
  48. # Removing previous versions
  49. remove_deprecated_install() {
  50. if [ -d "$1" ] || [ -f "$1" ] ; then
  51. if [ $2 -eq 1 ] && [ "$(id -u)" -ne 0 ]; then
  52. warning_echo "Deprecated install location found, cannot remove without root access: $1"
  53. return
  54. fi
  55. info_echo "Removing old install location: $1"
  56. if [ $DRYRUN != 1 ]; then rm -rf $1; fi
  57. fi
  58. }
  59. remove_deprecated_install "/usr/local/bin/lyrebird/" 1
  60. if [ "$(id -u)" -ne 0 ]; then
  61. remove_deprecated_install "/usr/local/share/applications/Lyrebird.desktop" 1
  62. remove_deprecated_install "/usr/local/share/applications/lyrebird.desktop" 1
  63. fi
  64. if [ -d "/etc/lyrebird/" ]; then
  65. warning_echo "/etc/lyrebird/ is now deprecated, please relocate contents to ~/.config/lyrebird/ and delete"
  66. fi
  67. # Required pip3 modules space separated
  68. REQUIRED_PIP_MODULES="toml"
  69. # Create all of the directories if they don't exist
  70. if [ ! -d "$BIN_PATH" ]; then
  71. verbose_echo "Creating binary path: $BIN_PATH"
  72. if [ $DRYRUN != 1 ]; then
  73. mkdir -p "$BIN_PATH"
  74. chmod -R 755 "$BIN_PATH"
  75. fi
  76. fi
  77. if [ ! -d "$SHARE_PATH" ]; then
  78. verbose_echo "Creating share path: $SHARE_PATH"
  79. if [ $DRYRUN != 1 ]; then
  80. mkdir -p "$SHARE_PATH"
  81. chmod -R 755 "$SHARE_PATH"
  82. fi
  83. fi
  84. if [ ! -d "$DESKTOP_PATH" ]; then
  85. verbose_echo "Creating desktop path: $DESKTOP_PATH"
  86. if [ $DRYRUN != 1 ]; then
  87. mkdir -p "$DESKTOP_PATH"
  88. fi
  89. fi
  90. install_python_modules() {
  91. verbose_echo "Installing Python modules: $REQUIRED_PIP_MODULES"
  92. if [ $DRYRUN != 1 ]; then
  93. # Var not included in quotes so it installs each module
  94. pip3 install --prefix $INSTALL_PREFIX $REQUIRED_PIP_MODULES
  95. fi
  96. }
  97. install_binary_source() {
  98. verbose_echo "Copying app/ to: $SHARE_PATH"
  99. if [ $DRYRUN != 1 ]; then cp -rf app "$SHARE_PATH"; fi
  100. verbose_echo "Copying icon.png to: $SHARE_PATH"
  101. if [ $DRYRUN != 1 ]; then cp icon.png "$SHARE_PATH"; fi
  102. verbose_echo "Copying app.py to: $SHARE_PATH"
  103. if [ $DRYRUN != 1 ]; then cp app.py "$SHARE_PATH"; fi
  104. verbose_echo "Copying lyrebird to: $BIN_PATH"
  105. if [ $DRYRUN != 1 ]; then cp lyrebird "$BIN_PATH/lyrebird"; fi
  106. verbose_echo "Setting permissions 755 recursively for: $SHARE_PATH"
  107. if [ $DRYRUN != 1 ]; then chmod -R 755 "$SHARE_PATH"; fi
  108. }
  109. install_desktop() {
  110. verbose_echo "Copying lyrebird.desktop to: $DESKTOP_PATH"
  111. if [ $DRYRUN != 1 ]; then BIN_PATH=$BIN_PATH SHARE_PATH=$SHARE_PATH envsubst < lyrebird.desktop > $DESKTOP_PATH/lyrebird.desktop; fi
  112. verbose_echo "Setting permission 644 for: $DESKTOP_PATH/lyrebird.desktop"
  113. if [ $DRYRUN != 1 ]; then chmod -R 644 "$DESKTOP_PATH/lyrebird.desktop"; fi
  114. }
  115. verbose_space() { if [ $VERBOSE = 1 ]; then echo; fi }
  116. install_python_modules
  117. verbose_space
  118. install_binary_source
  119. verbose_space
  120. install_desktop
  121. verbose_space
  122. info_echo "Installed Lyrebird v$VERSION"