.dots/scripts/update/alpine.sh
2024-09-26 11:05:50 +02:00

61 lines
2 KiB
Bash
Executable file

#!/usr/bin/env ash
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
# exit on ctrl-c
trap 'printf ""$font_colored_bold"\n>> stopping system update after "$SECONDS" seconds..."$font_default"\n" && exit 1' SIGINT
# font styles & colors
font_default="\e[0m"
font_colored="\e[32;1m"
font_colored_bold="\e[31;1m"
[[ "$(id -u)" != "0" ]] && printf ""$font_colored_bold">> error: permission denied!"$font_default"\n" && exit 1
for arg in "$@"; do
case "$arg" in
-n|--noconfirm)
noconfirm="true"
;;
-r|--reboot)
reboot="true"G
;;
esac
done
printf ""$font_colored_bold">> system update started..."$font_default"\n"
printf ""$font_colored"> updating packages..."$font_default"\n"
[[ "$noconfirm" ]] && /sbin/apk update --progress || /sbin/apk update --progress --interactive
[[ "$noconfirm" ]] && /sbin/apk upgrade --progress || /sbin/apk update --progress --interactive
printf ""$font_colored"> cleaning cache..."$font_default"\n"
/sbin/apk cache clean &> /dev/null
rm -rf "/var/cache//sbin/apk/*"
orphans="$(/sbin/apk list --orphaned | awk '{print $1}' 2> /dev/null)"
if [[ "$orphans" ]]; then
orphans="$(printf "$orphans" | tr '\n' ' ')"
printf ""$font_colored"> removing orphaned packages..."$font_default"\n"
[[ "$noconfirm" ]] && /sbin/apk del --purge --progress $orphans || /sbin/apk del --purge --progress --interactive $orphans
fi
[[ -f "/etc/motd" ]] && printf ""$font_colored"> removing '/etc/motd'..."$font_default"\n" && rm -f "/etc/motd"
script="$(realpath $(dirname $0))/update-$(cat /etc/hostname).sh"
[[ -x "$script" ]] && printf ""$font_colored"> executing script "$script"..."$font_default"\n" && (. "$script")
printf ""$font_colored_bold">> system updated finished!"$font_default"\n"
[[ ! "$reboot" ]] && exit 0
timeout="5"
printf ""$font_colored"> rebooting system in "
while [[ "$timeout" -gt "0" ]]; do
printf "$timeout... "
timeout="$(( timeout - 1 ))"
sleep 1
done
printf "now!"$font_default"\n"
reboot