Skip to content

Commit

Permalink
Merge pull request #417 from iAGorynT/iAGorynT-Code-Snippet-Changes
Browse files Browse the repository at this point in the history
Update Brewautom2.sh
  • Loading branch information
iAGorynT authored Oct 21, 2024
2 parents ee2c467 + 187edca commit 60446ff
Showing 1 changed file with 124 additions and 110 deletions.
234 changes: 124 additions & 110 deletions ShellScripts/Brewautom2.sh
Original file line number Diff line number Diff line change
@@ -1,128 +1,142 @@
#!/bin/zsh

# Trap Ctl-C and Require a Menu Selection to Exit Script
trap 'echo -e "\nCtrl-C will not terminate $0."' INT
# Trap Ctrl-C and prevent it from terminating the script.
trap 'echo -e "\nCtrl-C will not terminate $0."' INT

# Function to check the status of the Brew autoupdate service
function autoupdatestatus {
clear
echo "Brew Autoupdate Status..."
echo
# Define variables for file paths
userplistname="com.bin.${USERNAME}.brewit.plist"
grepname="com.bin.${USERNAME}.brewit"
launchagentfolder="$HOME/Library/LaunchAgents"
launchagentplist="${launchagentfolder}/${userplistname}"
# Check Status of launchd agent
if launchctl list | grep -q "$grepname"; then
echo "Launchd Agent is active."
else
echo "Warning: Launchd Agent may not be active."
fi
# echo
# Autoupdate Log Listing
# If Brew Autoupdate Logfile Exist, Show Contents
if test -f "$launchagentplist"; then
# Extract Keyvalue From Plist File Using PlistBuddy
autolog=$(/usr/libexec/PlistBuddy -c "Print :StandardOutPath" $launchagentplist)
fi
# Check if the Autoupdate Log file exists
if [ -f "$autolog" ]; then
echo -en "\n\n\t\t\tHit any key to view Autoupdate Log Listing"
read -k 1 line
clear
echo "Brew Autoupdate Log Listing..."
echo
# Temporary file to store the modified content
temp_file=$(mktemp)
# Loop through each line of the file
while IFS= read -r line; do
# Check if the line starts with the specified string
# if [[ "$line" =~ ^(Brew Update,) ]]; then
# Add a separator line before the line
# echo "==>" >> "$temp_file"
# fi
# Append the original line to the temporary file
echo "$line" >> "$temp_file"
done < "$autolog"
# Display Reformatted Autoupdate Logfile
cat "$temp_file" | more
fi
clear
echo "Brew Autoupdate Status..."
echo

# Define variables for file paths
userplistname="com.bin.${USERNAME}.brewit.plist"
grepname="com.bin.${USERNAME}.brewit"
launchagentfolder="$HOME/Library/LaunchAgents"
launchagentplist="${launchagentfolder}/${userplistname}"

# Check if the launchd agent is active
if launchctl list | grep -q "$grepname"; then
echo "Launchd Agent is active."
else
echo "Warning: Launchd Agent may not be active."
fi

# Check if the Brew autoupdate log file exists
if [[ -f "$launchagentplist" ]]; then
# Extract the log file path from the plist file using PlistBuddy
autolog=$(/usr/libexec/PlistBuddy -c "Print :StandardOutPath" "$launchagentplist")
fi

# If the log file exists, display its contents
if [[ -f "$autolog" ]]; then
echo -en "\n\n\t\t\tPress any key to view the Autoupdate Log"
read -k 1
clear
echo "Brew Autoupdate Log Listing..."
echo

# Temporary file for storing formatted log output
temp_file=$(mktemp)

# Boolean to indicate the start of a new log entry
newlog=true

# Process each line of the log file
while IFS= read -r line; do
# Detect new log entries based on the pattern
if [[ "$line" =~ ^(Brew Update,) ]]; then
if $newlog; then
# Add a separator line before new entries
echo "====>" >> "$temp_file"
newlog=false
else
newlog=true
fi
fi
# Append the current line to the temporary file
echo "$line" >> "$temp_file"
done < "$autolog"

# Display the formatted log using 'more' for paging
cat "$temp_file" | more

# Clean up the temporary file
rm "$temp_file"
fi
}

# Function to load the Brew autoupdate service
function autoupdateload {
clear
echo "Load Brew Autoupdate..."
echo
while true; do
read yn\?"Yy / Nn: "
case $yn in
[Yy]* ) echo; brewitload.sh; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
clear
echo "Load Brew Autoupdate..."
echo

while true; do
read yn\?"Confirm (Y/N): "
case $yn in
[Yy]* ) echo; brewitload.sh; break;;
[Nn]* ) break;;
* ) echo "Please answer yes (Y) or no (N).";;
esac
done
}

# Function to unload the Brew autoupdate service
function autoupdateunload {
clear
echo "Unload Brew Autoupdate..."
echo

while true; do
read yn\?"Yy / Nn: "
case $yn in
[Yy]* ) echo; brewitunload.sh; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
}
clear
echo "Unload Brew Autoupdate..."
echo

function menu {
clear
echo
echo -e "\t\t\tBrew Autoupdate Menu\n"
echo -e "\t1. Autoupdate Status"
echo -e "\t2. Load Autoupdate"
echo -e "\t3. Unload Autoupdate"
echo -e "\t0. Exit Menu\n\n"
echo -en "\t\tEnter an Option: "
read -k 1 option
# Test If Return / Enter Key Pressed; Replace Linefeed Character With Empty Character
if [[ "$option" == *$'\n'* ]]; then
option=""
fi
while true; do
read yn\?"Confirm (Y/N): "
case $yn in
[Yy]* ) echo; brewitunload.sh; break;;
[Nn]* ) break;;
* ) echo "Please answer yes (Y) or no (N).";;
esac
done
}

while [ 1 ]
do
menu
case $option in
0)
break ;;

1)
autoupdatestatus ;;

2)
autoupdateload ;;

3)
autoupdateunload ;;
# Function to display the main menu
function menu {
clear
echo
echo -e "\t\t\tBrew Autoupdate Menu\n"
echo -e "\t1. Check Autoupdate Status"
echo -e "\t2. Load Autoupdate"
echo -e "\t3. Unload Autoupdate"
echo -e "\t0. Exit Menu\n\n"
echo -en "\tEnter your choice: "
read -k 1 option

# Return / Enter Key Pressed
"")
break ;;
# Handle Return/Enter key as an empty input
[[ "$option" == $'\n' ]] && option=""
}

*)
clear
echo "Sorry, wrong selection";;
esac
echo -en "\n\n\t\t\tHit any key to continue"
read -k 1 line
# Main loop for the script
while true; do
menu
case $option in
0) # Exit the script
break ;;
1) # Display autoupdate status
autoupdatestatus ;;
2) # Load the autoupdate service
autoupdateload ;;
3) # Unload the autoupdate service
autoupdateunload ;;
"") # Exit when Enter is pressed
break ;;
*) # Invalid option handling
clear
echo "Invalid selection. Please try again." ;;
esac
echo -en "\n\n\tPress any key to continue"
read -k 1
done

# Reset Ctrl-C behavior to default
trap - INT
clear

# Reset Trap Ctl-C
trap INT

0 comments on commit 60446ff

Please sign in to comment.