File check-for-expired-apt-keys-and-rename-them of Package kimi-utils-ubuntu
#!/bin/bash
# ANSI color codes
if tput setaf 1 &> /dev/null; then
RED=$(tput setaf 1) # Red for "expired"
GREEN=$(tput setaf 2) # Green for paths
NC=$(tput sgr0) # Reset to default color
else
RED=""
GREEN=""
NC=""
fi
# Check for expired APT keys
expired_keys=$(apt-key list | grep expired)
# Check if any expired keys were found
if [[ -z "$expired_keys" ]]; then
echo "No expired APT keys found."
exit 0
fi
echo "Expired APT keys found:"
# Check if running as root
is_root=0
print_root_message=0
if [[ "$(id -u)" -ne 0 ]]; then
print_root_message=1
else
is_root=1
fi
# Process output
while IFS= read -r line; do
# Print colored line for trusted.gpg.d paths
if [[ $line == "/etc/apt/trusted.gpg.d/"* ]]; then
echo -e "${GREEN}${line}${NC}"
if (( is_root )); then
# Rename the expired key by appending "~"
mv "$line" "${line}~"
echo "Renamed: $line to ${line}~"
fi
elif [[ $line == pub* || $line == uid* ]]; then
# Color "expired" in pub and uid lines
echo -e "${line//expired/$RED&$NC}"
else
# Print other lines without color
echo "$line"
fi
done <<< "$expired_keys"
if (( print_root_message )); then
echo
echo "You need to be root to rename the expired keys"
echo "so that they do not interefere with updates."
fi