user confirmation to update

This commit is contained in:
overdodactyl 2018-04-21 21:04:09 -06:00
parent f7c727112d
commit e28e95ed90

View file

@ -2,11 +2,11 @@
### ghacks-user.js updater for Mac/Linux ### ghacks-user.js updater for Mac/Linux
## author: @overdodactyl ## author: @overdodactyl
## version: 1.3 ## version: 1.1
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}
currdir=$(pwd) currdir=$(pwd)
@ -19,65 +19,100 @@ 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}")"
## create backup folder if it doesn't exist ## Used to check if a new version of updater.sh is available
mkdir -p userjs_backups; update_available="no"
check_for_update () {
online_version="$(curl -s ${updater} | sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p')"
path_to_script="$(dirname "${sfp}")/updater.sh"
current_version="$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$path_to_script")"
if (( $(echo "$online_version > $current_version" | bc -l) )); then
update_available="yes"
fi
}
## Check if there's a newer version of the updater script available ## Used to backup the current script, and download and execute the latest version of updater.sh
online_version="$(curl -s ${updater} | sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p')" update_script () {
path_to_script="$(dirname "${sfp}")/updater.sh" echo -e "This script will be backed up and the latest version of updater.sh will be executed.\n"
current_version="$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$path_to_script")"
if (( $(echo "$online_version > $current_version" | bc -l) )); then
echo -e "There is a new updater script available online. This updater will be backed up and the latest will be executed.\n"
bakfile="updater.sh.backup.$(date +"%Y-%m-%d_%H%M")" bakfile="updater.sh.backup.$(date +"%Y-%m-%d_%H%M")"
mv updater.sh "userjs_backups/${bakfile}" mv updater.sh "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"
# make new file executable # make new file executable
chmod +x updater.sh chmod +x updater.sh
# execute new updater script # execute new updater script
./updater.sh ./updater.sh -donotupdate
# exit script # exit script
exit 1 exit 1
fi }
echo -e "\nThis script should be run from your Firefox profile directory.\n"
echo -e "Updating the user.js for Firefox profile:\n$(pwd)\n" main () {
## create backup folder if it doesn't exist
mkdir -p userjs_backups;
if [ -e user.js ]; then echo -e "\nThis script should be run from your Firefox profile directory.\n"
echo "Your current user.js file for this profile will be backed up and the latest ghacks version from github will take its place."
echo -e "\nIf currently using the ghacks user.js, please compare versions:"
echo " Available online: $(curl -s ${ghacksjs} | sed -n '4p')"
echo " Currently using: $(sed -n '4p' user.js)"
else
echo "A user.js file does not exist in this profile. If you continue, the latest ghacks version from github will be downloaded."
fi
echo -e "\nIf a user-overrides.js file exists in this profile, it will be appended to the user.js.\n" echo -e "Updating the user.js for Firefox profile:\n$(pwd)\n"
read -p "Continue Y/N? " -n 1 -r
echo -e "\n\n"
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ -e user.js ]; then if [ -e user.js ]; then
# backup current user.js echo "Your current user.js file for this profile will be backed up and the latest ghacks version from github will take its place."
bakfile="user.js.backup.$(date +"%Y-%m-%d_%H%M")" echo -e "\nIf currently using the ghacks user.js, please compare versions:"
mv user.js "userjs_backups/${bakfile}" && echo "Your previous user.js file was backed up: userjs_backups/${bakfile}" echo " Available online: $(curl -s ${ghacksjs} | sed -n '4p')"
echo " Currently using: $(sed -n '4p' user.js)"
else
echo "A user.js file does not exist in this profile. If you continue, the latest ghacks version from github will be downloaded."
fi fi
# download latest ghacks user.js echo -e "\nIf a user-overrides.js file exists in this profile, it will be appended to the user.js.\n"
echo "downloading latest ghacks user.js file"
curl -O ${ghacksjs} && echo "ghacks user.js has been downloaded"
if [ -e user-overrides.js ]; then read -p "Continue Y/N? " -n 1 -r
echo "user-overrides.js file found" echo -e "\n\n"
cat user-overrides.js >> user.js && echo "user-overrides.js has been appended to user.js"
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ -e user.js ]; then
# backup current user.js
bakfile="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}"
fi
# download latest ghacks user.js
echo "downloading latest ghacks user.js file"
curl -O ${ghacksjs} && echo "ghacks user.js has been downloaded"
if [ -e user-overrides.js ]; then
echo "user-overrides.js file found"
cat user-overrides.js >> user.js && echo "user-overrides.js has been appended to user.js"
fi
else
echo "Process aborted"
fi fi
## change directory back to the original working directory
cd "${currdir}"
}
if [ $update_pref = "-donotupdate" ]; then
main
else else
echo "Process aborted" check_for_update
fi if [ $update_available = "no" ]; then
main
## change directory back to the original working directory else
cd "${currdir}" ## there is an update available
if [ $update_pref = "-update" ]; then
## update without asking
update_script
else
read -p "There is a newer version of updater.sh available. Download and execute? Y/N? " -n 1 -r
echo -e "\n\n"
if [[ $REPLY =~ ^[Yy]$ ]]; then
update_script
else
main
fi
fi
fi
fi