]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.description
batman-adv: Use new function to remove device
[people/stevee/network.git] / src / functions / functions.description
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 description_format_filename() {
23 # Format the filename of a description file for a given zone or port
24 local type=${1}
25 local name=${2}
26
27 case ${type} in
28 zone)
29 echo "$(zone_dir ${name})/description"
30 ;;
31 port)
32 echo "$(port_dir ${name})/description"
33 ;;
34 esac
35 }
36
37 description_touch_file() {
38 # If the description file does not exist
39 # and we have no directory with the given name this function creates the file
40 local type=${1}
41 local name=${2}
42
43 local file=$(description_format_filename ${type} ${name})
44
45 # We use the -e switch here because we want to touch
46 # when also no directory with this name exist
47 if ! [ -e ${file} ]; then
48 touch ${file}
49 fi
50
51 }
52 description_title_read() {
53 # This function reads the title out of a given description file
54 local file=${1}
55 assert isset file
56
57 [ -r "${file}" ] || return ${EXIT_ERROR}
58
59 local title
60 read -r title < ${file}
61 echo ${title}
62 }
63
64 description_edit() {
65 # This function provides a higher level interface special for description files of the editor function.
66 local type=${1}
67 local name=${2}
68
69 description_touch_file ${type} ${name}
70
71 local file=$(description_format_filename ${type} ${name})
72 editor ${file} "description_check"
73 }
74
75 description_print() {
76 # This function prints a given description file.
77 local type=${1}
78 local name=${2}
79
80 local file=$(description_format_filename ${type} ${name})
81
82 if [ ! -r "${file}" ] || [ ! -f "${file}" ]; then
83 warning "${file} is not readable"
84 return ${EXIT_ERROR}
85 fi
86
87 local title=$(description_title_read ${file})
88
89 cli_headline 1 "Description"
90 cli_space
91 cli_print 2 "${title}"
92 cli_space
93
94 # True if we are in the first line
95 local first_line=true
96
97 # True if we get from the second line on, only whitespace or empty lines
98 local front_white=true
99
100 # How many blank lines did we get
101 local white_counter=0
102
103 while read line; do
104 if ${first_line}; then
105 # We are in the first line and pass they so first_line is now false
106 first_line=false
107 continue
108 fi
109 # Check if the line is blank or contain only whitespace
110 if ${front_white} && [[ "${line}" =~ ^(|[[:space:]]+)$ ]]; then
111 # The we do not print them
112 continue
113 else
114 # we have found after the second line which is not blank or contain only white space so
115 # front_white is false. Now ew print empyt line but only if they are follewd by a non empty line.
116 front_white=false
117 if [[ "${line}" == "" ]] || [[ "${line}" =~ ^[[:space:]]$ ]]; then
118 # If the line is blank or contain only white space we increase the counter.
119 (( white_counter++ ))
120 else
121 # The line is not blank so we print all blank lines till now and print the current line after.
122 if [ ${white_counter} -gt 0 ]; then
123 for (( i = 1; i <= ${white_counter}; i += 1 )); do
124 cli_space
125 done
126
127 # The counter is now zero, because the lines were printed.
128 white_counter=0
129 fi
130 cli_print 2 "${line}"
131 fi
132 fi
133 done < ${file}
134
135 cli_space
136 }
137
138 description_cli() {
139 # Function for the command line interface
140 local type=${1}
141 local name=${2}
142 local action=${3}
143 shift 3
144
145 case ${action} in
146 show)
147 description_print ${type} ${name} ${@}
148 ;;
149 edit)
150 description_edit ${type} ${name} ${@}
151 ;;
152 *)
153 error "Invalid argument: ${action}"
154 ;;
155 esac
156
157 }
158
159 description_check_title() {
160 # Checks if the title is too long and if so prints a warning
161 assert [ $# -eq 1 ]
162
163 local title=${1}
164 local title_length=40
165
166 # Have to be shorter then ${title_length}
167 if [ ${#title} -gt ${title_length} ]; then
168 warning "Title '${title}' is to long. Only titles with ${title_length} or less chracters are allowed"
169 return ${EXIT_ERROR}
170 fi
171
172 return ${EXIT_OK}
173 }
174
175 description_check() {
176 # Check if a description file satisfy our needs
177 assert [ $# -eq 1 ]
178
179 local file=${1}
180 local title=$(description_title_read ${file})
181
182 description_check_title "${title}"
183
184 return ${EXIT_OK}
185 }