summaryrefslogtreecommitdiffstats
path: root/util/bin/magic
blob: 218c8d43fe9fcdaec44a051de6e6412b29180181 (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
#! /bin/sh
#
# magic name command file < content
#

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

case $1 in
  (print)
    echo "$bs"
    cat
    echo "$es"
    ;;
  (create)
    $0 $1 has $3 || $0 $1 print $3 >> $3 < /dev/null
    $0 $1 replace $3
    ;;
  (destroy)
    if $0 $1 has $3; then
      cache"`cat $3`"
      echo "$cache" | sed "/^$bs$/,/^$es$/d" > $3
    fi
    ;;
  (has)
    grep -q "^$bs\$" $3 && grep -q "^$es\$" $3
    ;;
  (replace)
    $0 $1 destroy $3 && $0 $1 create $3
    ;;
  (clear)
    $0 $1 replace $3 < /dev/null
    ;;
  (*)
    echo 'Error 1: You are made of stupid!' >&2
    exit 23
esac