Update updater.sh

This commit is contained in:
earthlng 2018-05-02 15:10:39 +02:00 committed by GitHub
parent 7dabc6b920
commit 696ec12b02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,8 @@
## author: @overdodactyl ## author: @overdodactyl
## version: 1.3 ## version: 1.3
## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in check_for_update() )
ghacksjs="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js" ghacksjs="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/user.js"
updater="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh" updater="https://raw.githubusercontent.com/ghacksuserjs/ghacks-user.js/master/updater.sh"
update_pref=${1:--ask} update_pref=${1:--ask}
@ -19,33 +21,28 @@ if [ -z "$sfp" ]; then sfp=${BASH_SOURCE[0]}; fi
## change directory to the Firefox profile directory ## change directory to the Firefox profile directory
cd "$(dirname "${sfp}")" cd "$(dirname "${sfp}")"
script_filename="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
## Used to check if a new version of updater.sh is available ## Used to check if a new version of updater.sh is available
update_available="no" update_available="no"
check_for_update () { check_for_update () {
online_version="$(curl -s ${updater} | sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p')" online_version="$(curl -s ${updater} | sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p')"
path_to_script="$(dirname "${sfp}")/"${script_filename}"" path_to_script="$(dirname "${sfp}")/updater.sh"
current_version="$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$path_to_script")" current_version="$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$path_to_script")"
if (( $(echo "$online_version > $current_version" | bc -l) )); then if [[ "$current_version" < "$online_version" ]]; then
update_available="yes" update_available="yes"
fi fi
} }
## Used to backup the current script, and download and execute the latest version of updater.sh ## Used to backup the current script, and download and execute the latest version of updater.sh
update_script () { update_script () {
echo -e "This script will be backed up and the latest version of will be executed.\n" echo -e "This script will be backed up and the latest version of updater.sh will be executed.\n"
bakfile=""${script_filename}".backup.$(date +"%Y-%m-%d_%H%M")" mv updater.sh "updater.sh.backup.$(date +"%Y-%m-%d_%H%M")"
mv "${script_filename}" "userjs_backups/${bakfile}"
curl -O ${updater} && echo -e "\nThe latest updater script has been downloaded\n" curl -O ${updater} && echo -e "\nThe latest updater script has been downloaded\n"
mv updater.sh "${script_filename}"
# make new file executable # make new file executable
chmod +x "${script_filename}" chmod +x updater.sh
# execute new updater script # execute new updater script
./"${script_filename}" -donotupdate ./updater.sh -donotupdate
# exit script # exit script
exit 1 exit 1
@ -77,8 +74,8 @@ main () {
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ -e user.js ]; then if [ -e user.js ]; then
# backup current user.js # backup current user.js
bakfile="user.js.backup.$(date +"%Y-%m-%d_%H%M")" bakfile="userjs_backups/user.js.backup.$(date +"%Y-%m-%d_%H%M")"
mv user.js "userjs_backups/${bakfile}" && echo "Your previous user.js file was backed up: userjs_backups/${bakfile}" mv user.js "${bakfile}" && echo "Your previous user.js file was backed up: ${bakfile}"
fi fi
# download latest ghacks user.js # download latest ghacks user.js
@ -97,8 +94,8 @@ main () {
cd "${currdir}" cd "${currdir}"
} }
update_pref="$(echo $update_pref | tr '[A-Z]' '[a-z]')"
if [ "$(echo $update_pref | tr '[A-Z]' '[a-z]')" = "-donotupdate" ]; then if [ $update_pref = "-donotupdate" ]; then
main main
else else
check_for_update check_for_update
@ -106,11 +103,11 @@ else
main main
else else
## there is an update available ## there is an update available
if [ "$(echo $update_pref | tr '[A-Z]' '[a-z]')" = "-update" ]; then if [ $update_pref = "-update" ]; then
## update without asking ## update without asking
update_script update_script
else else
read -p "There is a newer version of this updater available. Download and execute? Y/N? " -n 1 -r read -p "There is a newer version of updater.sh available. Download and execute? Y/N? " -n 1 -r
echo -e "\n\n" echo -e "\n\n"
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
update_script update_script
@ -119,4 +116,4 @@ else
fi fi
fi fi
fi fi
fi fi