home/.zsh/functions.d/home.sh

81 lines
2.2 KiB
Bash
Raw Normal View History

2025-09-10 00:54:07 +02:00
function _home_date() {
os_name=$(uname)
if [ "$os_name" = "FreeBSD" ]; then
hdate=$(date -r $1)
else
hdate=$(date -d @$1)
fi
return $hdate
}
function _home_help() {
2025-09-10 00:14:02 +02:00
echo "Available commands:"
echo ""
2025-09-10 00:14:02 +02:00
echo "help: Show this help text."
2025-09-10 00:57:25 +02:00
echo "diff Show the file differences."
2025-09-10 00:14:02 +02:00
echo "pull: Execute git pull in $HOME."
echo "sub: Update all submodules of $HOME."
2025-09-10 00:57:25 +02:00
echo "status Show Git status."
2025-09-10 00:14:02 +02:00
echo "up: Pull and update submodules in $HOME."
}
function home() {
case $1 in
diff)
cd ~
git diff
2025-09-10 00:14:02 +02:00
cd $OLDPWD
;;
help)
2025-09-10 00:54:07 +02:00
_home_help
;;
pull)
cd ~
git pull
2025-09-10 00:14:02 +02:00
cd $OLDPWD
;;
status)
cd ~
git status
2025-09-10 00:14:02 +02:00
cd $OLDPWD
;;
sub)
cd ~
git submodule update --init --recursive
2025-09-10 00:14:02 +02:00
cd $OLDPWD
;;
up)
cd ~
2025-09-10 00:32:22 +02:00
if test -f .lastupdate; then
2025-09-10 01:28:44 +02:00
last_update=$(date -r .lastupdate +%s)
2025-09-10 00:32:22 +02:00
echo "Last configuration update: $last_update"
current_date=$(date +%s)
2025-09-10 01:19:09 +02:00
echo "Current date: $current_date"
2025-09-10 00:32:22 +02:00
time_diff=$(($current_date-$last_update))
2025-09-10 00:54:07 +02:00
echo "Time difference: $time_diff seconds."
2025-09-10 00:32:22 +02:00
if (( time_diff > 86400 )) || [ "$2" = "f" ]; then
echo "Updating home configuration..."
if git pull; then
if git submodule update --init --recursive; then
touch .lastupdate
fi
fi
else
2025-10-03 00:46:16 +02:00
echo "$HOME was already updated in the last 24 hours."
2025-09-10 00:32:22 +02:00
echo "Run this to force updating: home up f"
fi
2025-09-10 00:14:02 +02:00
else
2025-09-10 00:32:22 +02:00
echo "~/.lastupdate does not exist!"
2025-09-10 00:35:45 +02:00
echo "You need to run the following command to update your"
echo "home configuration: home up f"
2025-09-10 00:32:22 +02:00
touch .lastupdate
2025-09-09 23:30:15 +02:00
fi
2025-09-10 00:14:02 +02:00
cd $OLDPWD
;;
*)
2025-09-10 00:14:02 +02:00
echo "Command not found!"
echo ""
_home_help
;;
esac
}