summaryrefslogtreecommitdiffstats
path: root/util/bin/magic
blob: 8f3c7e048d243b31e181a82e1512ffd6346ed571 (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
#! /bin/sh
#
# magic NAME create FILE < CONTENT
# magic NAME update FILE < CONTENT
# magic NAME destroy FILE
#

bs="# begin krebs magic <$1>"
es="# end krebs magic <$1>"

has() {
  grep -q "^$bs$" $3 && grep -q "^$es$" $3
}

create() {
  destroy "$@"
  cache="`cat $3`"
  {
    echo "$cache"
    echo "$bs"
    cat
    echo "$es"
  } >$3
}

#retrieve() {
#}

update() {
  ! has "$@" || create "$@"
}

destroy() {
  if has "$@"; then
    cache="`cat $3`"
    echo "$cache" | sed "/^$bs$/,/^$es$/d" >$3
  fi
}

case $2 in
  (create) create "$@" ;;
  (update) update "$@" ;;
  (destroy) destroy "$@" ;;
  (*)
    echo 'Error 1: You are made of stupid!' >&2
    exit 23
esac