cdist/cdist/conf/type/__ini_value/files/parts/present/insert.awk

57 lines
1.5 KiB
Awk

# check if there is a comment block before the section
firstline_index = bufindex - (buflen - 1)
insertpoint = -1 # the insertpoint marks the point before the insert
lastfreespace = -1
for(i = bufindex; i >= firstline_index; i--) {
_line = trim(linebuf[i])
if(_line == "") {
lastfreespace = i
continue
}
if(comment && was_comment(_line, comment)) {
insertpoint = i + 1
no_insert_comment = 1
if(lastfreespace != insertpoint)
insert_line_after = 1
break
}
if(!is_comment(_line) || index(_line, delimiter) > 0) {
insertpoint = i + 1
# only insert a line before if we do not have a space around
if(lastfreespace == insertpoint)
insertpoint++
else
insert_line_before = 1
# check for empty line after the insert point
# use absolute boundary cause the insertpoint can be changed
if(trim(linebuf[i + 2]) != "")
insert_line_after = 1
break
}
}
# insert into the last free space
if(insertpoint == -1) {
if(lastfreespace != -1) {
insertpoint = lastfreespace
insert_line_before = 1
}
else {
insertpoint = firstline_index
insert_line_after = 1
}
}
# print lines before
flush_lines(insertpoint - firstline_index)
# print before and comment
if(insert_line_before) print ""
if(comment && !no_insert_comment) c_print()
v_print()
if(insert_line_after) print ""
flush_buffer()