diff --git a/bin/update b/bin/update index 067f5a7..7248817 100755 --- a/bin/update +++ b/bin/update @@ -1,5 +1,28 @@ #!/usr/bin/env bash +_lock() { + if [ -f ~/.config/dotfiles/.data/update_lock ]; then + return 1 + fi + + touch ~/.config/dotfiles/.data/update_lock +} + +_lock_or_error_message() { + _lock + local value=$? + + if [ $value -ne 0 ]; then + echo -e "\033[31;1merror:\033[0m update seems to be already running" + echo -e "\033[1minfo:\033[0m if you're sure it's not running, run \`update force-unlock\`" + return $value + fi +} + +_unlock() { + rm -rf ~/.config/dotfiles/.data/update_lock +} + _check_date_if_format() { local format=`_date_format` @@ -286,6 +309,22 @@ partial-test() { "check") return 0;; "startup") return 0;; "postpone") return 0;; + "force-unlock") return 0;; + *) return 1;; + esac +} + +partial-requires-lock() { + case $1 in + "system") return 0;; + "rust") return 0;; + "npm") return 0;; + "dotfiles") return 0;; + "neovim") return 0;; + "check") return 1;; + "startup") return 1;; + "postpone") return 1;; + "force-unlock") return 1;; *) return 1;; esac } @@ -300,6 +339,7 @@ partial-update() { "check") _check_date_if_format force;; "startup") _check_date_if_format;; "postpone") update-postpone;; + "force-unlock") _unlock;; *) return 1 esac } @@ -307,6 +347,10 @@ partial-update() { main() { if [ $# -eq 0 ]; then + _lock_or_error_message + if [ $? -ne 0 ]; then + return $? + fi start=`date +%s` echo -e "\033[32;1m=== Starting full update ===\033[0m" @@ -334,22 +378,38 @@ main() { _check_date_if_format force + _unlock + else + local lock_required=1 + for part in $@; do partial-test $part if [ $? -ne 0 ]; then echo -e "\033[1;31merror:\033[0m unrocognized update command \"$part\"" return 1 fi + partial-requires-lock $part + lock_required=$(($lock_required * $?)) done + if [ $lock_required -eq 0 ]; then + _lock_or_error_message + if [ $? -ne 0 ]; then + return $? + fi + fi + for part in $@; do partial-update $part done + if [ $lock_required -eq 0 ]; then + _unlock + fi fi - } main $@ +