updater.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env bash
  2. ## ghacks-user.js updater for macOS and Linux
  3. ## version: 1.5
  4. ## Author: Pat Johnson (@overdodactyl)
  5. ## Additional contributors: @earthlng, @ema-pe
  6. ## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in check_for_update() )
  7. #########################
  8. # Base variables #
  9. #########################
  10. update_pref=${1:--ask}
  11. RED='\033[0;31m'
  12. BLUE='\033[0;34m'
  13. BBLUE='\033[1;34m'
  14. GREEN='\033[0;32m'
  15. ORANGE='\033[0;33m'
  16. CYAN='\033[0;36m'
  17. NC='\033[0m' # No Color
  18. #########################
  19. # Working directory #
  20. #########################
  21. # get current directory
  22. currdir=$(pwd)
  23. ## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed)
  24. sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)
  25. ## fallback for Macs without coreutils
  26. if [ -z "$sfp" ]; then sfp=${BASH_SOURCE[0]}; fi
  27. ## store the Firefox profile directory
  28. ff_profile="$(dirname "${sfp}")"
  29. #########################
  30. # File Handeling #
  31. #########################
  32. # Download method priority: curl -> wget -> pearl
  33. DOWNLOAD_METHOD="not_pearl"
  34. if [[ $(command -v "curl") ]] > /dev/null 2>&1; then
  35. DOWNLOAD_TO_FILE="curl -O"
  36. elif [[ $(command -v "wget") ]] > /dev/null 2>&1; then
  37. DOWNLOAD_TO_FILE="wget"
  38. elif [[ $(command -v "perl") ]]; then
  39. DOWNLOAD_METHOD="perl"
  40. else
  41. echo -e ${RED}"This script requires curl, wget or perl to be installed.\nProcess aborted"${NC}
  42. exit 0
  43. fi
  44. # Download files
  45. download_file () {
  46. mkdir -p userjs_temps
  47. cd userjs_temps
  48. url=$1
  49. if [ $DOWNLOAD_METHOD = "not_pearl" ]; then
  50. $DOWNLOAD_TO_FILE ${url}
  51. else
  52. http_url=${url/https/http}
  53. # Variables from the shell are available in Perl's %ENV hash
  54. # Need to export shell variable so it is visible to subprocesses
  55. export http_url
  56. perl -e '
  57. use File::Fetch;
  58. my $ff = File::Fetch->new(uri => $ENV{http_url});
  59. my $where = $ff->fetch() or die $ff->error;
  60. my $where = $ff->fetch( to => "." );
  61. '
  62. fi
  63. cd ..
  64. }
  65. # Backup a file into userjs_backups
  66. # Replace current version of a file with new one in userjs_temps
  67. backup_file () {
  68. filename=$1
  69. mkdir -p userjs_backups
  70. mv $filename "userjs_backups/${filename}.backup.$(date +"%Y-%m-%d_%H%M")"
  71. mv "userjs_temps/${filename}" $filename
  72. echo -e "Status: ${GREEN}${filename} has been backed up and replaced with the latest version!${NC}"
  73. }
  74. #########################
  75. # Initiation #
  76. #########################
  77. initiate () {
  78. echo -e
  79. echo -e
  80. echo -e ${BBLUE}" ############################################################################"
  81. echo -e " #### ####"
  82. echo -e " #### ghacks user.js ####"
  83. echo -e " #### Hardening the Privacy and Security Settings of Firefox ####"
  84. echo -e " #### Maintained by @Thorin-Oakenpants and @earthlng ####" ####"
  85. echo -e " #### Updater for macOS and Linux by @overdodactyl ####" ####"
  86. echo -e " #### ####"
  87. echo -e " ############################################################################"${NC}
  88. echo -e
  89. echo -e
  90. echo -e "Documentation for this script is available here: ${CYAN}https://github.com/ghacksuserjs/ghacks-user.js/wiki/3.3-Updater-Scripts${NC}\n"
  91. }
  92. confirmation () {
  93. download_file "https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js" &>/dev/null
  94. echo -e "Please observe the following information:"
  95. echo -e "\tFirefox profile: ${ORANGE}$(pwd)${NC}"
  96. echo -e "\tAvailable online: ${ORANGE}$(get_userjs_version userjs_temps/user.js)${NC}"
  97. echo -e "\tCurrently using: ${ORANGE}$(get_userjs_version user.js)\n${NC}\n"
  98. echo -e "This script will update to the latest user.js file and append any custom configurations from user-overrides.js. ${RED}Continue Y/N? ${NC}"
  99. read -p "" -n 1 -r
  100. echo -e "\n"
  101. if [[ $REPLY =~ ^[Nn]$ ]]; then
  102. echo -e ${RED}"Process aborted"${NC}
  103. return 1
  104. fi
  105. }
  106. #########################
  107. # Update updater.sh #
  108. #########################
  109. # Returns the version number of a updater.sh file
  110. get_updater_version () {
  111. filename=$1
  112. version_regex='5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p'
  113. echo "$(sed -n "$version_regex" "${ff_profile}/${filename}")"
  114. }
  115. # Update updater.sh
  116. # Default: Check for update, if available, ask user if they want to execute it
  117. # Args:
  118. # -donotupdate: New version will not be looked for and update will not occur
  119. # -update: Check for update, if available, execute without asking
  120. update_updater () {
  121. update_pref="$(echo $update_pref | tr '[A-Z]' '[a-z]')"
  122. if [ $update_pref = "-donotupdate" ]; then
  123. # User signified not to check for updates
  124. return 0
  125. fi
  126. download_file "https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh" &>/dev/null
  127. if [[ $(get_updater_version updater.sh) < $(get_updater_version userjs_temps/updater.sh) ]]; then
  128. if [ $update_pref != "-update" ]; then
  129. echo -e "There is a newer version of updater.sh available. ${RED}Download and execute Y/N?${NC}"
  130. read -p "" -n 1 -r
  131. echo -e "\n\n"
  132. if [[ $REPLY =~ ^[Nn]$ ]]; then
  133. # Update available, but user chooses not to update
  134. return 0
  135. fi
  136. fi
  137. else
  138. # No update available
  139. return 0
  140. fi
  141. # Backup current updater, execute latest version
  142. backup_file updater.sh
  143. chmod +x updater.sh
  144. ./updater.sh -donotupdate
  145. exit 1
  146. }
  147. #########################
  148. # Update user.js #
  149. #########################
  150. # Returns version number of a user.js file
  151. get_userjs_version () {
  152. filename=$1
  153. echo "$(sed -n "4p" "${ff_profile}/${filename}")"
  154. }
  155. # Applies latest version of user.js and any custom overrides
  156. update_userjs () {
  157. backup_file user.js
  158. if [ -e user-overrides.js ]; then
  159. cat user-overrides.js >> user.js
  160. echo -e "Status: ${GREEN}Your user-overrides.js customizations have been applied!${NC}"
  161. fi
  162. }
  163. #########################
  164. # Execute #
  165. #########################
  166. ## change directory to the Firefox profile directory
  167. cd "$ff_profile"
  168. initiate
  169. update_updater
  170. confirmation && update_userjs
  171. rm -rf userjs_temps
  172. cd "${currdir}"