diff --git a/battery.sh b/battery.sh index 1185790..d3657ea 100644 --- a/battery.sh +++ b/battery.sh @@ -3,7 +3,6 @@ ## ############### ## Variables ## ############### -tempfolder=tmp binfolder=/usr/local/bin visudo_path=/private/etc/sudoers.d/battery configfolder=$HOME/.battery @@ -55,7 +54,10 @@ Usage: instructions on how to make which utility exempt from sudo, highly recommended battery update - update the battery utility to the latest version (reruns the installation script) + update the battery utility to the latest version + + battery reinstall + reinstall the battery utility to the latest version (reruns the installation script) battery uninstall enable charging and remove the smc tool and the battery script @@ -75,7 +77,6 @@ $( whoami ) ALL = NOPASSWD: BATTERYON # Get parameters action=$1 setting=$2 -platypus=$3 ## ############### ## Helpers @@ -83,11 +84,7 @@ platypus=$3 function log() { - if [ -z "$platypus" ]; then - echo -e "$(date +%T) - $1" - else - echo -e "$1" - fi + echo -e "$(date +%T) - $1" } @@ -144,12 +141,26 @@ if [[ "$action" == "visudo" ]]; then exit 0 fi +# Reinstall helper +if [[ "$action" == "reinstall" ]]; then + echo "This will run curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash" + if [[ ! "$setting" == "silent" ]]; then + echo "Press any key to continue" + read + fi + curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash + battery + exit 0 +fi + # Update helper if [[ "$action" == "update" ]]; then - echo "This will run curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash" - echo "Press any key to continue" - read - curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash + echo "This will run curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/update.sh | bash" + if [[ ! "$setting" == "silent" ]]; then + echo "Press any key to continue" + read + fi + curl -sS https://raw.githubusercontent.com/actuallymentor/battery/main/update.sh | bash battery exit 0 fi diff --git a/platypus.sh b/platypus.sh deleted file mode 100644 index fa2a1c2..0000000 --- a/platypus.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -binfolder=/usr/local/bin - -# Ask the user what we are doing -response=$( osascript -e \ - 'tell app "System Events" to display dialog "šŸ”‹ Do you want to disable or enable the battery utility?\n\nEnable: maintain a battery percentage of 80%.\nDisable: charge your macbook as usual (to max 100%)." buttons {"Enable", "Disable"} default button 1' -) - -# Install/update on application open -bash ./setup.sh "true" - -# Run battery maintain at default percentage - -if [[ "$response" == *"Enable"* ]]; then - $binfolder/battery maintain 80 "true" - echo "āœ… Maintaining battery at 80%, you may quit this window safely" -else - $binfolder/battery maintain stop "true" - $binfolder/battery charging on - echo "āœ… Disabled battery utility, you may quit this window safely" -fi diff --git a/setup.sh b/setup.sh index a6e22d2..3762ed7 100644 --- a/setup.sh +++ b/setup.sh @@ -1,14 +1,15 @@ #!/bin/bash -platypus=$1 - # User welcome message -if [ -z "$platypus" ]; then - echo -e "\n####################################################################" - echo '# šŸ‘‹ Welcome, this is the setup script for the battery CLI tool.' - echo -e "# Note: this script will ask for your password once or multiple times." - echo -e "####################################################################\n\n" -fi +echo -e "\n####################################################################" +echo '# šŸ‘‹ Welcome, this is the setup script for the battery CLI tool.' +echo -e "# Note: this script will ask for your password once or multiple times." +echo -e "####################################################################\n\n" + +# Set environment variables +tempfolder=~/.battery-tmp +binfolder=/usr/local/bin +mkdir -p $tempfolder # Ask for sudo once, in most systems this will cache the permissions for a bit sudo echo "šŸ”‹ Starting battery installation" @@ -16,12 +17,9 @@ sudo echo "šŸ”‹ Starting battery installation" echo -e "\n[ 1/9 ] Superuser permissions acquired." # Get smc source and build it -tempfolder=~/.battery-tmp -binfolder=/usr/local/bin -mkdir -p $tempfolder - smcfolder="$tempfolder/smc" echo "[ 2/9 ] Cloning fan control version of smc" +rm -rf $smcfolder git clone --depth 1 https://github.com/hholtmann/smcFanControl.git $smcfolder &> /dev/null cd $smcfolder/smc-command echo "[ 3/9 ] Building smc from source" @@ -39,6 +37,7 @@ echo "[ 5/9 ] Cloning battery repository" git clone --depth 1 https://github.com/actuallymentor/battery.git $bateryfolder &> /dev/null echo "[ 6/9 ] Writing script to $binfolder/battery" sudo cp $bateryfolder/battery.sh $binfolder/battery +sudo chown $USER $binfolder/battery sudo chmod 755 $binfolder/battery sudo chmod u+x $binfolder/battery @@ -51,8 +50,4 @@ echo "[ 8/9 ] Removing temp folder $tempfolder" rm -rf $tempfolder echo "[ 9/9 ] Removed temporary build files" - - -if [ -z "$platypus" ]; then - echo -e "\nšŸŽ‰ Battery tool installed. Type \"battery\" for instructions.\n" -fi \ No newline at end of file +echo -e "\nšŸŽ‰ Battery tool installed. Type \"battery help\" for instructions.\n" \ No newline at end of file diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..588e1db --- /dev/null +++ b/update.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Set environment variables +tempfolder=~/.battery-tmp +binfolder=/usr/local/bin +bateryfolder="$tempfolder/battery" +mkdir -p $bateryfolder + +echo "šŸ”‹ Starting battery update" + +# Write battery function as executable + +echo "[ 1/3 ] Cloning battery repository" +rm -rf $bateryfolder +git clone --depth 1 https://github.com/actuallymentor/battery.git $bateryfolder &> /dev/null +echo "[ 2/3 ] Writing script to $binfolder/battery" +cp $bateryfolder/battery.sh $binfolder/battery +chown $USER $binfolder/battery +chmod 755 $binfolder/battery +chmod u+x $binfolder/battery + +# Remove tempfiles +cd +rm -rf $tempfolder +echo "[ 3/3 ] Removed temporary folder" + + + +echo -e "\nšŸŽ‰ Battery tool updated.\n"