aboutsummaryrefslogtreecommitdiff
path: root/status
blob: 51f9874bda6906d27a5f757e919d9bb4463f3f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
#
# Check the overall status of your work.
#

# Parameters
BASENAME="`basename $0`"

# Usage
function status_usage {
  echo "usage: $BASENAME [-l|--long] [-d|--delay N] [project]"

  if [ -z "$1" ]; then
    exit 1
  else
    exit $1
  fi
}

# See https://stackoverflow.com/questions/2721946/cross-platform-getopt-for-a-shell-script#4300224
function status_options {
  getopt -T > /dev/null
  if [ $? -eq 4 ]; then
    # GNU enhanced getopt is available
    ARGS=`getopt --name "$BASENAME" --long loop,delay: --options ld: -- "$@"`
  else
    # Original getopt is available (no long option names, no whitespace, no sorting)
    ARGS=`getopt ld: "$@"`
  fi

  if [ $? -ne 0 ]; then
    echo "$BASENAME: usage error" >&2
    status_usage 2
  fi

  eval set -- $ARGS

  while [ $# -gt 0 ]; do
    case "$1" in
      l|--loop)
        LOOP="yes";;
      d|--delay)
        DELAY="$2"
        shift;;
      --)
        shift
        break;;
      *)
        status_usage
        ;;
    esac
    shift
  done

  if [ $# -gt 0 ]; then
    PROJECT="$1"
    shift
  fi
}

# Calculate timestamp delta from a file
function status_file_changed_delta {
  local file="$1"

  if [ -z "$file" ] || [ ! -e "$file" ]; then
    echo 0
  fi

  local file_timestamp="`stat -c '%Z' $file`"
  local timestamp="`date +%s`"

  echo $(( timestamp - file_timestamp ))
}

# Run command if a file is older than a given number of seconds
function status_run_if_file_older_than {
  local file="$1"
  local interval="$2"
  local run="0"

  shift 2

  # Run command if there's still no file to test
  if [ ! -e "$file" ]; then
    run="1"
  else
    local age="`status_file_changed_delta $file`"
    local interval="3600"

    # Run command only when file is older or equal the interval
    if (( $age >= $interval )); then
      run="1"
    fi
  fi

  # Run if criteria matches
  if [ "$run" == "1" ]; then
    $*
  fi
}

# Run status
function status_run {
  if [ ! -z "$PROJECT" ]; then
    # Check the status of the given project
    cd $PROJECT &> /dev/null && git status
  elif git status &> /dev/null; then
    # Check the status of the current project
    mr status
  else
    # Ensure we are in the home folder
    cd

    # Check your reminders
    if which remind &> /dev/null && [ -e "$HOME/.reminders" ]; then
      remind ~/.reminders | grep -iv '^no reminders.$' # | tr '[:upper:]' '[:lower:]'
    fi

    # Update your mrconfig and check all registered repositories
    if which mr &> /dev/null; then
      local mrconfig="$HOME/.custom/mrconfig-automatic"

      status_run_if_file_older_than $mrconfig 3600 mrconfig-updater

      # Pipe through cat so we don't have to deal with mr's dynamic output weirdness
      #mr -m status | cat

      # Replace eventual "/mnt/crypt/home/$USER" with "~/" from output
      # Piping through sed also handles mr's dynamic output weirdness
      mr -m status | sed -e "s|/mnt/crypt/home/$USER/|~/|" -e "s|$HOME|~|"
    fi

    # Use this as an opportunity to save our configs
    if which git-config-save &> /dev/null; then
      git config-save --sometimes &> /dev/null
    fi

    # Check your TODO lists, filtering only important tasks
    # Convention here is to use uppercase "IMPORTANT", to be explicit and avoid false positives
    todo list IMPORTANT

    # Collected items that need to be moved somewhere else
    #collector count
    collector list

    # Check if you have mails to send
    postponed

    # Check unread mails
    unread-mails

    # Alarms set
    #xalarm list

    # Check for spool files
    if [ -e "$HOME/temp/log/spool.md" ] && [ "`wc -l $HOME/temp/log/spool.md | cut -d ' ' -f 1`" != "0" ]; then
      echo "Spool file not empty: $HOME/temp/log/spool.md"
    fi

    # Check if are dangling downloaded files
    if [ -e "$HOME/load" ] && [ ! -z "`ls -1 ~/load/`" ]; then
      # Print a newline if needed depending of the output of the previous commands
      if [ ! -z "`postponed`" ] || [ ! -z "`unread-mails`" ] || [ ! -z "`collector list`" ]; then
        echo ""
      fi

      echo "Dangling files at ~/load:"
      echo ""
      ls -lh ~/load/
    fi
  fi
}

# Options
status_options $@

# Dispatch
if [ "$LOOP" == "yes" ]; then
  PROJECT=""

  if [ -z "$DELAY" ]; then
    DELAY="60"
  fi

  while true; do
    clear
    status_run
    sleep $DELAY
  done
else
  status_run
fi