From 94a192b65130614db9a8aeb18b0f90454c0e501a Mon Sep 17 00:00:00 2001 From: daniel Date: Sun, 19 Mar 2023 14:18:49 +0100 Subject: [PATCH] auto update --- auto-update.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++ install.sh | 57 ++++++++++++++++++++++-------------------- profiles/base/.zshrc | 2 ++ 3 files changed, 91 insertions(+), 27 deletions(-) create mode 100755 auto-update.sh diff --git a/auto-update.sh b/auto-update.sh new file mode 100755 index 0000000..60f9c41 --- /dev/null +++ b/auto-update.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# adapted from: https://github.com/jez/dotfiles/blob/master/util/auto-update.sh + +# Number of seconds to wait before printing a reminder +UPDATE_THRESHOLD="604800" # 1 week + +check_for_updates() { + [ ! -e $HOME/.dotfiles_update ] && touch $HOME/.dotfiles_update + # Initialize for when we have no GNU date available + last_check=0 + time_now=0 + + # Darwin uses BSD, check for gdate, else use date + if [[ $(uname) = "Darwin" && -n $(which gdate) ]]; then + last_login=$(gdate -r ~/.dotfiles_update +%s) + time_now=$(gdate +%s) + else + # Ensure this is GNU grep + if [ -n "$(date --version 2> /dev/null | grep GNU)" ]; then + last_login=$(date -r ~/.dotfiles_update +%s) + time_now=$(date +%s) + fi + fi + + time_since_check=$((time_now - last_login)) + + if [ "$time_since_check" -ge "$UPDATE_THRESHOLD" ]; then + echo $time_since_check + else + echo 0 + fi +} + +update() { + # Record that we've update + touch $HOME/.dotfiles_update + + # Update dotfiles repo + cd ~/.dotfiles/ + + echo "Checking for updates..." + git fetch --quiet origin + if [ "$(git rev-parse HEAD)" != "$(git rev-parse origin/master)" ]; then + echo "--> outdated." + git pull + ./install.sh + else + echo "--> nothing to do." + fi + + cd - &> /dev/null +} + +check=$(check_for_updates) +if [[ $check -gt 0 ]]; then + echo "Dotfiles might be out of date." + update +fi + diff --git a/install.sh b/install.sh index fe830c5..2636613 100755 --- a/install.sh +++ b/install.sh @@ -1,30 +1,5 @@ #!/bin/bash -if ! type zsh > /dev/null; then - echo "Cannnot change default shell to zsh: not installed" -else - # Make ZSH the default shell environment - if [ $(cat /etc/passwd | grep $USER | awk -F ':' '{print substr($7, length($7)-3)}') != "/zsh" ]; then - echo "Changing shell to zsh" - chsh -s $(which zsh) - fi - - # Antidote Installation - if [ ! -d "$HOME/.antidote" ]; then - echo "Installing antidote" - - # make sure git is installed - if ! type git > /dev/null; then - echo "Git not installed. Cannot install antidote" - else - git clone --depth=1 https://github.com/mattmc3/antidote.git $HOME/.antidote - fi - - else - echo "antidote already exists." - fi -fi - # fzf if [ ! -d "$HOME/.fzf" ]; then git clone --depth 1 https://github.com/junegunn/fzf.git $HOME/.fzf @@ -51,5 +26,33 @@ fi # linking dotfiles $HOME/.dotfiles/dotfiles.sh link base -# reload shell -zsh +if ! type zsh > /dev/null; then + echo "Cannnot change default shell to zsh: not installed" +else + # Make ZSH the default shell environment + if [ $(cat /etc/passwd | grep $USER | awk -F ':' '{print substr($7, length($7)-3)}') != "/zsh" ]; then + echo "Changing shell to zsh" + chsh -s $(which zsh) + fi + + # Antidote Installation + if [ ! -d "$HOME/.antidote" ]; then + echo "Installing antidote" + + # make sure git is installed + if ! type git > /dev/null; then + echo "Git not installed. Cannot install antidote" + else + git clone --depth=1 https://github.com/mattmc3/antidote.git $HOME/.antidote + fi + + else + echo "antidote already exists." + fi + + # reload shell + source ~/.zshrc +fi + + + diff --git a/profiles/base/.zshrc b/profiles/base/.zshrc index bff16e1..d98aa46 100644 --- a/profiles/base/.zshrc +++ b/profiles/base/.zshrc @@ -36,3 +36,5 @@ bindkey -v [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh bindkey '^f' fzf-file-widget + +$HOME/.dotfiles/auto-update.sh