Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/intentions-queue
blob: e9b79de5740e4a4b071e9fec11589c785c85bb45 (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
#!/bin/sh

# contains functions related to the intentions-queue

# shellcheck disable=SC2039

if [ -z "${base_dir}" ]; then
  # just to make shellcheck happy
  . '../lib/load-configuration'
fi

# intent_something [-n]
# create an intention, provided on stdin - or just show the intention (-n)
# note, that this function is not thread-safe - use the lock
# $package_database_lock_file externally
intent_something() {
  local next_number
  if [ "x$1" = 'x-n' ]; then
    cat
    return
  fi
  next_number=$((
    $(
      find "${intentions_directory}" \
        -maxdepth 1 \
        -type f \
        -name 'intention.*' \
        -printf '%f\n' \
      | sed '
        s/^intention\.//
        t
        d
      ' \
      | sort -nr \
      | grep -xm1 '[0-9]\+' \
      || echo 0
    )+1
  ))
  {
    # shellcheck disable=SC2016
    printf '%s\n' \
      '#!/bin/sh' \
      '[ -n "${base_dir}" ] || . '"${base_dir}"'/lib/load-configuration'
    cat
    printf 'rm "%s"\n' "${intentions_directory}/intention.${next_number}"
  } > "${intentions_directory}/intention.${next_number}"
  chmod +x "${intentions_directory}/intention.${next_number}"
}

# execute_intention
# executes the next intention
execute_intention() {
  local next_number
  next_number=$(
    find "${intentions_directory}" \
      -maxdepth 1 \
      -type f \
      -name 'intention.*' \
      -printf '%f\n' \
    | sed '
      s/^intention\.//
      t
      d
    ' \
    | sort -n \
    | grep -xm1 '[0-9]\+'
  ) || return 0
  "${intentions_directory}/intention.${next_number}"
}

# intentions_left
# check if there are undone intentions left
# return 0 if there is something left to do
# return 1 if nothing is queued
intentions_left() {
  find "${intentions_directory}" \
    -maxdepth 1 \
    -type f \
    -name 'intention.*' \
    -printf '%f\n' \
  | sed '
    s/^intention\.//
    t
    d
  ' \
  | sort -n \
  | grep -qxm1 '[0-9]\+'
}

# execute_all_intentions
# executes all intentions
execute_all_intentions() {
  while intentions_left; do
    execute_intention
  done
}