updater.sh 3.8 KB

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