add Teckids utility suite, by request from Natureshadow
[shellsnippets/shellsnippets.git] / mksh / teckids / rplanung
1 # -*- mode: sh -*-
2 #-
3 # Copyright © 2014
4 #       Dominik George <dominik.george@teckids.org>
5 #       mirabilos <thorsten.glaser@teckids.org>
6 #
7 # Provided that these terms and disclaimer and all copyright notices
8 # are retained or reproduced in an accompanying document, permission
9 # is granted to deal in this work without restriction, including un‐
10 # limited rights to use, publicly perform, distribute, sell, modify,
11 # merge, give away, or sublicence.
12 #
13 # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
14 # the utmost extent permitted by applicable law, neither express nor
15 # implied; without malicious intent or gross negligence. In no event
16 # may a licensor, author or contributor be held liable for indirect,
17 # direct, other damage, loss, or other issues arising in any way out
18 # of dealing in the work, even if advised of the possibility of such
19 # damage or existence of a defect, except proven that it results out
20 # of said person’s immediate fault when using the work as intended.
21
22 # Teckids utility subcommand that gets a field from planung.txt by key and field name
23
24 if [[ $(basename "$0") = rplanung ]]; then
25         teckids_sourcing_wrapper=1
26         offline=1
27         . "$(dirname "$0")/teckids"
28 elif [[ -z $TECKIDS_WRAPPER_LOADED ]]; then
29         print -ru2 E: may only be sourced from teckids scripts
30         exit 1
31 fi
32
33 asso_setasso planung
34
35 [[ -e $ptxt ]] || return
36
37 s=0
38 while IFS= read -r line; do
39         case $s in
40         0)
41                 if [[ $line = "| Message-ID "* ]]; then
42                         line=${line#\| }
43                         line=${line% \|}
44                         sIFS=$IFS; IFS="|"; set -A fields -- $line; IFS=$sIFS
45
46                         i=-1
47                         while (( ++i < ${#fields[@]} )); do
48                                 fields[i]=${fields[i]##*( )}
49                                 fields[i]=${fields[i]%%*( )}
50                         done
51
52                         s=1
53                 fi
54                 ;;
55         1)
56                 s=2
57                 ;;
58         2)
59                 if [[ $line == "+-"* ]]; then
60                         break
61                 else
62                         line=${line#\| }
63                         line=${line% \|}
64                         sIFS=$IFS; IFS="|"; set -A linea -- $line; IFS=$sIFS
65
66                         i=-1
67                         while (( ++i < ${#linea[@]} )); do
68                                 linea[i]=${linea[i]##*( )}
69                                 linea[i]=${linea[i]%%*( )}
70                         done
71
72                         key=${linea[0]}
73
74                         i=-1
75                         while (( ++i < ${#fields[@]} )); do
76                                 asso_sets "${linea[i]}" planung "$key" "${fields[i]}"
77                         done
78                 fi
79                 ;;
80         esac
81 done <"$ptxt"
82
83 if [[ $(basename "$0") = rplanung ]]; then
84         asso_getv planung "$1" "$2"
85         exit 0
86 fi