Update updater.sh

This commit is contained in:
Tyler Page 2019-07-03 16:32:55 +00:00 committed by GitHub
parent d40d7dbabd
commit 52bb5f7658
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,12 +2,14 @@
## ghacks-user.js updater for macOS and Linux ## ghacks-user.js updater for macOS and Linux
## version: 2.5 ## version: 2.5.1
## Author: Pat Johnson (@overdodactyl) ## Author: Pat Johnson (@overdodactyl)
## Additional contributors: @earthlng, @ema-pe, @claustromaniac ## Additional contributors: @earthlng, @ema-pe, @claustromaniac, @iamtpage
## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in update_updater() ) ## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in update_updater() )
set -e
readonly CURRDIR=$(pwd) readonly CURRDIR=$(pwd)
sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null) sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)
@ -113,23 +115,27 @@ legacy_argument () {
# Download files # Download files
download_file () { download_file () {
declare -r url=$1 URL="$1"
declare -r tf=$(mktemp) TF="$(mktemp)"
local dlcmd='' export URL
export TF
local DLCMD=''
if [ $DOWNLOAD_METHOD = 'curl' ]; then if [ $DOWNLOAD_METHOD = 'curl' ]; then
dlcmd="curl -o $tf" DLCMD="curl -o $TF $URL"
else else
dlcmd="wget -O $tf" DLCMD="wget -O $TF $URL"
fi fi
export DLCMD
$dlcmd "${url}" &>/dev/null && echo "$tf" || echo '' # return the temp-filename (or empty string on error) $DLCMD &>/dev/null && echo "$TF" || echo '' # return the temp-filename (or empty string on error)
} }
open_file () { #expects one argument: file_path open_file () { #expects one argument: file_path
KERNEL="$(uname -s)"
if [ "$(uname)" == 'Darwin' ]; then if [ "$(uname)" == 'Darwin' ]; then
open "$1" open "$1"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then elif [ "${KERNEL:0:5}" == "Linux" ]; then
xdg-open "$1" xdg-open "$1"
else else
echo -e "${RED}Error: Sorry, opening files is not supported for your OS.${NC}" echo -e "${RED}Error: Sorry, opening files is not supported for your OS.${NC}"
@ -137,19 +143,20 @@ open_file () { #expects one argument: file_path
} }
readIniFile () { # expects one argument: absolute path of profiles.ini readIniFile () { # expects one argument: absolute path of profiles.ini
declare -r inifile="$1" inifile="$1"
declare -r tfile=$(mktemp) tfile="$(mktemp)"
declare -r inifile
declare -r tfile
if [ $(grep '^\[Profile' "$inifile" | wc -l) == "1" ]; then ### only 1 profile found if [ "$(grep -c '^\[Profile' "$inifile")" == "1" ]; then ### only 1 profile found
grep '^\[Profile' -A 4 "$inifile" | grep -v '^\[Profile' > $tfile grep '^\[Profile' -A 4 "$inifile" | grep -v '^\[Profile' > "$tfile"
else else
grep -E -v '^\[General\]|^StartWithLastProfile=|^IsRelative=' "$inifile" grep -E -v '^\[General\]|^StartWithLastProfile=|^IsRelative=' "$inifile"
echo '' echo ''
read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r
echo -e "\n" echo -e "\n"
if [[ $REPLY =~ ^(0|[1-9][0-9]*)$ ]]; then if [[ $REPLY =~ ^(0|[1-9][0-9]*)$ ]]; then
grep '^\[Profile'${REPLY} -A 4 "$inifile" | grep -v '^\[Profile'${REPLY} > $tfile if grep "^\[Profile'${REPLY}" -A 4 "$inifile" | grep -v "^\[Profile'${REPLY}" > "$tfile"; then
if [[ "$?" != "0" ]]; then
echo "Profile${REPLY} does not exist!" && exit 1 echo "Profile${REPLY} does not exist!" && exit 1
fi fi
else else
@ -157,8 +164,10 @@ readIniFile () { # expects one argument: absolute path of profiles.ini
fi fi
fi fi
declare -r profpath=$(grep '^Path=' $tfile) profpath="$(grep '^Path=' "$tfile")"
declare -r pathisrel=$(grep '^IsRelative=' $tfile) pathisrel="$(grep '^IsRelative=' "$tfile")"
declare -r profpath
declare -r pathisrel
rm "$tfile" rm "$tfile"
@ -198,7 +207,7 @@ getProfilePath () {
# Returns the version number of a updater.sh file # Returns the version number of a updater.sh file
get_updater_version () { get_updater_version () {
echo $(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$1") sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$1"
} }
# Update updater.sh # Update updater.sh
@ -211,7 +220,8 @@ update_updater () {
return 0 # User signified not to check for updates return 0 # User signified not to check for updates
fi fi
declare -r tmpfile=$(download_file 'https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh') tmpfile="$(download_file 'https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh')"
declare -r tmpfile
if [[ $(get_updater_version "${SCRIPT_DIR}/updater.sh") < $(get_updater_version "${tmpfile}") ]]; then if [[ $(get_updater_version "${SCRIPT_DIR}/updater.sh") < $(get_updater_version "${tmpfile}") ]]; then
if [ $UPDATE = 'check' ]; then if [ $UPDATE = 'check' ]; then
@ -238,15 +248,15 @@ update_updater () {
# Returns version number of a user.js file # Returns version number of a user.js file
get_userjs_version () { get_userjs_version () {
if [ -e $1 ]; then if [ -e "$1" ]; then
echo "$(sed -n '4p' "$1")" sed -n '4p' "$1"
else else
echo "Not detected." echo "Not detected."
fi fi
} }
add_override () { add_override () {
input=$1 input="$1"
if [ -f "$input" ]; then if [ -f "$input" ]; then
echo "" >> user.js echo "" >> user.js
cat "$input" >> user.js cat "$input" >> user.js
@ -254,12 +264,12 @@ add_override () {
elif [ -d "$input" ]; then elif [ -d "$input" ]; then
FSAVEIFS=$IFS FSAVEIFS=$IFS
IFS=$'\n\b' # Set IFS IFS=$'\n\b' # Set IFS
FILES="${input}"/*.js FILES="( ${input}/*.js )"
for f in $FILES for f in $FILES
do do
add_override "$f" add_override "$f"
done done
IFS=$SAVEIFS # restore $IFS IFS=$FSAVEIFS # restore $IFS
else else
echo -e "${ORANGE}Warning: Could not find override file:${NC} ${input}" echo -e "${ORANGE}Warning: Could not find override file:${NC} ${input}"
fi fi
@ -271,11 +281,12 @@ remove_comments () { # expects 2 arguments: from-file and to-file
# Applies latest version of user.js and any custom overrides # Applies latest version of user.js and any custom overrides
update_userjs () { update_userjs () {
declare -r newfile=$(download_file 'https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js') NEWFILE="$(download_file 'https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js')"
export NEWFILE
echo 'Please observe the following information:' echo 'Please observe the following information:'
echo -e "\tFirefox profile: ${ORANGE}$(pwd)${NC}" echo -e "\tFirefox profile: ${ORANGE}$(pwd)${NC}"
echo -e "\tAvailable online: ${ORANGE}$(get_userjs_version $newfile)${NC}" echo -e "\tAvailable online: ${ORANGE}$(get_userjs_version "$NEWFILE")${NC}"
echo -e "\tCurrently using: ${ORANGE}$(get_userjs_version user.js)\n${NC}\n" echo -e "\tCurrently using: ${ORANGE}$(get_userjs_version user.js)\n${NC}\n"
if [ $CONFIRM = 'yes' ]; then if [ $CONFIRM = 'yes' ]; then
@ -284,7 +295,7 @@ update_userjs () {
echo -e "\n" echo -e "\n"
if [[ $REPLY =~ ^[Nn]$ ]]; then if [[ $REPLY =~ ^[Nn]$ ]]; then
echo -e "${RED}Process aborted${NC}" echo -e "${RED}Process aborted${NC}"
rm $newfile rm "$NEWFILE"
return 1 return 1
fi fi
fi fi
@ -297,13 +308,15 @@ update_userjs () {
# backup user.js # backup user.js
mkdir -p userjs_backups mkdir -p userjs_backups
local bakname="userjs_backups/user.js.backup.$(date +"%Y-%m-%d_%H%M")" local bakname
bakname="userjs_backups/user.js.backup.$(date +"%Y-%m-%d_%H%M")"
if [ $BACKUP = 'single' ]; then if [ $BACKUP = 'single' ]; then
bakname='userjs_backups/user.js.backup' bakname='userjs_backups/user.js.backup'
fi fi
cp user.js "$bakname" &>/dev/null cp user.js "$bakname" &>/dev/null
mv "${newfile}" user.js echo "mv ${NEWFILE} user.js" #DEBUG
mv "$NEWFILE" user.js
echo -e "Status: ${GREEN}user.js has been backed up and replaced with the latest version!${NC}" echo -e "Status: ${GREEN}user.js has been backed up and replaced with the latest version!${NC}"
if [ "$ESR" = true ]; then if [ "$ESR" = true ]; then
@ -349,16 +362,16 @@ update_userjs () {
######################### #########################
if [ $# != 0 ]; then if [ $# != 0 ]; then
readonly legacy_lc=$(echo $1 | tr '[A-Z]' '[a-z]') readonly legacy_lc="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
# Display usage if first argument is -help or --help # Display usage if first argument is -help or --help
if [ $1 = '--help' ] || [ $1 = '-help' ]; then if [ "$1" = "--help" ] || [ "$1" = "-help" ]; then
usage usage
elif [ $legacy_lc = '-donotupdate' ]; then elif [ "$legacy_lc" = "-donotupdate" ]; then
UPDATE='no' UPDATE='no'
legacy_argument $1 legacy_argument "$1"
elif [ $legacy_lc = '-update' ]; then elif [ "$legacy_lc" = "-update" ]; then
UPDATE='yes' UPDATE='yes'
legacy_argument $1 legacy_argument "$1"
else else
while getopts ":hp:ludsno:bcvre" opt; do while getopts ":hp:ludsno:bcvre" opt; do
case $opt in case $opt in
@ -400,7 +413,7 @@ if [ $# != 0 ]; then
;; ;;
r) r)
tfile=$(download_file 'https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js') tfile=$(download_file 'https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js')
mv $tfile "${tfile}.js" mv "$tfile" "${tfile}.js"
echo -e "${ORANGE}Warning: user.js was saved to temporary file ${tfile}.js${NC}" echo -e "${ORANGE}Warning: user.js was saved to temporary file ${tfile}.js${NC}"
open_file "${tfile}.js" open_file "${tfile}.js"
exit 1 exit 1
@ -419,7 +432,7 @@ if [ $# != 0 ]; then
fi fi
show_banner show_banner
update_updater $@ update_updater "$@"
getProfilePath # updates PROFILE_PATH or exits on error getProfilePath # updates PROFILE_PATH or exits on error
cd "$PROFILE_PATH" && update_userjs cd "$PROFILE_PATH" && update_userjs