updater.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env bash
  2. ### ghacks-user.js updater for Mac/Linux
  3. ## author: @overdodactyl, @ema-pe
  4. ## version: 1.4
  5. ## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in check_for_update() )
  6. ghacksjs="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js"
  7. updater="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh"
  8. update_pref=${1:--ask}
  9. currdir=$(pwd)
  10. DOWNLOAD_TO_STDOUT="curl -s"
  11. DOWNLOAD_TO_FILE="curl -O"
  12. # Use wget if curl is not available.
  13. if [[ -z $(command -v "curl") ]]; then
  14. DOWNLOAD_TO_STDOUT="wget --quiet --output-document=-"
  15. DOWNLOAD_TO_FILE="wget"
  16. fi
  17. ## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed)
  18. sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)
  19. ## fallback for Macs without coreutils
  20. if [ -z "$sfp" ]; then sfp=${BASH_SOURCE[0]}; fi
  21. ## change directory to the Firefox profile directory
  22. cd "$(dirname "${sfp}")"
  23. ## Used to check if a new version of updater.sh is available
  24. update_available="no"
  25. check_for_update () {
  26. online_version="$($DOWNLOAD_TO_STDOUT ${updater} | sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p')"
  27. path_to_script="$(dirname "${sfp}")/updater.sh"
  28. current_version="$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$path_to_script")"
  29. if [[ "$current_version" < "$online_version" ]]; then
  30. update_available="yes"
  31. fi
  32. }
  33. ## Used to backup the current script, and download and execute the latest version of updater.sh
  34. update_script () {
  35. echo -e "This script will be backed up and the latest version of updater.sh will be executed.\n"
  36. mv updater.sh "updater.sh.backup.$(date +"%Y-%m-%d_%H%M")"
  37. $DOWNLOAD_TO_FILE ${updater} && echo -e "\nThe latest updater script has been downloaded\n"
  38. # make new file executable
  39. chmod +x updater.sh
  40. # execute new updater script
  41. ./updater.sh -donotupdate
  42. # exit script
  43. exit 1
  44. }
  45. main () {
  46. ## create backup folder if it doesn't exist
  47. mkdir -p userjs_backups;
  48. echo -e "\nThis script should be run from your Firefox profile directory.\n"
  49. echo -e "Updating the user.js for Firefox profile:\n$(pwd)\n"
  50. if [ -e user.js ]; then
  51. echo "Your current user.js file for this profile will be backed up and the latest ghacks version from github will take its place."
  52. echo -e "\nIf currently using the ghacks user.js, please compare versions:"
  53. echo " Available online: $($DOWNLOAD_TO_STDOUT ${ghacksjs} | sed -n '4p')"
  54. echo " Currently using: $(sed -n '4p' user.js)"
  55. else
  56. echo "A user.js file does not exist in this profile. If you continue, the latest ghacks version from github will be downloaded."
  57. fi
  58. echo -e "\nIf a user-overrides.js file exists in this profile, it will be appended to the user.js.\n"
  59. read -p "Continue Y/N? " -n 1 -r
  60. echo -e "\n\n"
  61. if [[ $REPLY =~ ^[Yy]$ ]]; then
  62. if [ -e user.js ]; then
  63. # backup current user.js
  64. bakfile="userjs_backups/user.js.backup.$(date +"%Y-%m-%d_%H%M")"
  65. mv user.js "${bakfile}" && echo "Your previous user.js file was backed up: ${bakfile}"
  66. fi
  67. # download latest ghacks user.js
  68. echo "downloading latest ghacks user.js file"
  69. $DOWNLOAD_TO_FILE ${ghacksjs} && echo "ghacks user.js has been downloaded"
  70. if [ -e user-overrides.js ]; then
  71. echo "user-overrides.js file found"
  72. cat user-overrides.js >> user.js && echo "user-overrides.js has been appended to user.js"
  73. fi
  74. else
  75. echo "Process aborted"
  76. fi
  77. ## change directory back to the original working directory
  78. cd "${currdir}"
  79. }
  80. update_pref="$(echo $update_pref | tr '[A-Z]' '[a-z]')"
  81. if [ $update_pref = "-donotupdate" ]; then
  82. main
  83. else
  84. check_for_update
  85. if [ $update_available = "no" ]; then
  86. main
  87. else
  88. ## there is an update available
  89. if [ $update_pref = "-update" ]; then
  90. ## update without asking
  91. update_script
  92. else
  93. read -p "There is a newer version of updater.sh available. Download and execute? Y/N? " -n 1 -r
  94. echo -e "\n\n"
  95. if [[ $REPLY =~ ^[Yy]$ ]]; then
  96. update_script
  97. else
  98. main
  99. fi
  100. fi
  101. fi
  102. fi