check_for_upgrade.sh 1.43 KB
Newer Older
1
2
3
#!/usr/bin/env zsh

zmodload zsh/datetime
4

5
function _current_epoch() {
6
  echo $(( $EPOCHSECONDS / 60 / 60 / 24 ))
7
8
9
}

function _update_zsh_update() {
10
  echo "LAST_EPOCH=$(_current_epoch)" >! ${ZSH_CACHE_DIR}/.zsh-update
11
}
12

13
function _upgrade_zsh() {
14
  env ZSH=$ZSH sh $ZSH/tools/upgrade.sh
15
16
17
18
  # update the zsh file
  _update_zsh_update
}

Eric Danielson's avatar
Eric Danielson committed
19
20
21
22
23
24
epoch_target=$UPDATE_ZSH_DAYS
if [[ -z "$epoch_target" ]]; then
  # Default to old behavior
  epoch_target=13
fi

n.st's avatar
n.st committed
25
26
27
28
# Cancel upgrade if the current user doesn't have write permissions for the
# oh-my-zsh directory.
[[ -w "$ZSH" ]] || return 0

29
30
31
# Cancel upgrade if git is unavailable on the system
whence git >/dev/null || return 0

32
if mkdir "$ZSH/log/update.lock" 2>/dev/null; then
33
34
  if [ -f ${ZSH_CACHE_DIR}/.zsh-update ]; then
    . ${ZSH_CACHE_DIR}/.zsh-update
35

36
    if [[ -z "$LAST_EPOCH" ]]; then
37
38
39
      _update_zsh_update
      rmdir $ZSH/log/update.lock # TODO: fix later
      return 0
40
    fi
41

42
    epoch_diff=$(($(_current_epoch) - $LAST_EPOCH))
43
44
    if [ $epoch_diff -gt $epoch_target ]; then
      if [ "$DISABLE_UPDATE_PROMPT" = "true" ]; then
45
        _upgrade_zsh
46
      else
47
        echo "[Oh My Zsh] Would you like to update? [Y/n]: \c"
48
49
50
51
52
53
        read line
        if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]; then
          _upgrade_zsh
        else
          _update_zsh_update
        fi
54
      fi
55
    fi
56
57
58
  else
    # create the zsh file
    _update_zsh_update
59
  fi
60

Fabian Wolff's avatar
Fabian Wolff committed
61
  rmdir $ZSH/log/update.lock
62
fi