Last active
May 24, 2025 09:37
-
-
Save pmeulen/d77f083ccd5b56c002e13919b31a569f to your computer and use it in GitHub Desktop.
MacPorts update reminder. This is a script run from your shell profile (e.g. ~/.bash_profile). It prompts you to update MacPorts after 7 days every time you open a new terminal window and offers to run the update for you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Copyright 2025 SURF bv | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# Usage: bash macports_update_reminder.sh | |
# | |
# The idea is that you add this script to your shell profile (e.g. ~/.bash_profile) so that | |
# you are reminded to update MacPorts after 7 days every time you open a new terminal window. | |
# This script alerts the user to run an update if it was more than 7 days since the last update | |
# and prompts the user to run the update. | |
# The user must successfully run the update through this script for the reminder to be reset so | |
# the check does not need root privileges or network access. The script uses sudo to run the macports | |
# update. | |
# Timestamp file location | |
TIMESTAMP_FILE="$HOME/.last_macports_update" | |
# The number of days after which to remind the user to update MacPorts | |
REMIND_AFTER_DAYS=7 | |
WHITE_BOLD="\033[1;37m" | |
YELLOW_BOLD="\033[1;33m" | |
GREEN_BOLD="\033[1;32m" | |
RED_BOLD="\033[1;31m" | |
BLUE_BOLD="\033[1;34m" | |
RESET="\033[0m" | |
# Icons for messages | |
INFO="ℹ️\t" | |
ALERT="⚠️\t" | |
SUCCESS="✅\t" | |
FAILURE="❌\t" | |
QUESTION="❓\t" | |
# Get current date in seconds since epoch | |
CURRENT_DATE=$(date +%s) | |
# Check if the timestamp file exists | |
if [[ -f "$TIMESTAMP_FILE" ]]; then | |
# Read the last update timestamp | |
LAST_UPDATE=$(cat "$TIMESTAMP_FILE") | |
# Calculate days since last update | |
SECONDS_SINCE_UPDATE=$((CURRENT_DATE - LAST_UPDATE)) | |
DAYS_SINCE_UPDATE=$((SECONDS_SINCE_UPDATE / 86400)) | |
if ((DAYS_SINCE_UPDATE > REMIND_AFTER_DAYS)); then | |
echo -e "${YELLOW_BOLD}${ALERT}It has been $DAYS_SINCE_UPDATE days since you last updated MacPorts${RESET}" | |
echo -n -e "${WHITE_BOLD}${QUESTION}" | |
read -r -p "Do you want to update macports now? (y/N) <enter> " RESPONSE | |
echo -n -e "${RESET}" | |
if [[ "$RESPONSE" =~ ^[Yy] ]]; then | |
echo "Updating MacPorts..." | |
echo "sudo port selfupdate && sudo port upgrade outdated" | |
if sudo port selfupdate && sudo port upgrade outdated; then | |
echo "$CURRENT_DATE" > "$TIMESTAMP_FILE" | |
echo -e "${GREEN_BOLD}${SUCCESS}MacPorts update completed. Next reminder in $REMIND_AFTER_DAYS days.${RESET}" | |
else | |
echo -e "${RED_BOLD}${FAILURE}MacPorts update failed.${RESET}" | |
fi | |
else | |
echo -e "${BLUE_BOLD}${INFO}You chose not to update MacPorts.${RESET}" | |
fi | |
fi | |
else | |
# This is the first time the script is run | |
echo "$CURRENT_DATE" > "$TIMESTAMP_FILE" | |
echo -e "${GREEN_BOLD}${INFO}MacPorts update tracking initialized. First reminder in $REMIND_AFTER_DAYS days.${RESET}" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment