add quiet flag
[cedit] / cedit
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 cedit() {
17 local help="Usage: [-h|--help ] [-v] [SECTION_NAME] FILE
18 Create/modify a section in a config file
19
20 Returns 1 if the file is modified by this command, 2 or higher
21 for other problems.
22
23 The section is #comment delimited. Reads STDIN for the contents of the
24 section. Without SECTION_NAME, it acts on a global unnamed
25 section. cedit is short for config edit.
26
27 -q Quiet
28 -v Verbose
29 -b Keep backup file
30 -h|--help Help"
31 local s diff name init
32 local file_dir="$(dirname "$file")"
33 local exists=true
34 local verbose=false
35 local backup=false
36 local quiet=false
37
38 case $1 in
39 -b) backup=true; shift ;;
40 -v) verbose=true; shift ;;
41 -q) quiet=true; shift ;;
42 -h|--help) echo "$help"; return ;;
43 esac
44
45 if (( $# == 2 )); then
46 name=": $1"
47 shift
48 fi
49
50 local file="$1"
51 local file_name="${file##*/}"
52
53 local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#"
54 local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#"
55
56 if [[ ! -e $file_dir ]]; then
57 if ! mkdir -p $file_dir; then
58 s=sudo
59 $s mkdir -p $file_dir || return 2
60 fi
61 fi
62 if [[ ! -e $file ]]; then
63 exists=false
64 if ! $s touch $file; then
65 s=sudo
66 $s touch $file || return 2
67 fi
68 fi
69
70 [[ -w $file ]] || s=sudo
71
72
73 local in_section=false
74 if $exists; then
75 local tailn=1
76 local temp="$(mktemp -d)/$file_name"
77 cp "$file" "$temp"
78 cp /dev/null "$file"
79 while IFS= read -r line; do
80 tailn=$(( tailn + 1 ))
81 if [[ $line == "$begin" ]]; then
82 in_section=true;
83 break
84 fi
85 printf '%s\n' "$line" >> $file
86 done < "$temp"
87 fi
88
89 IFS= read -d '' -n 1 -r init
90 if [[ $init ]]; then
91 $s tee -a "$file" >/dev/null <<<"$begin"
92 printf '%s' "$init" | $s tee -a "$file" >/dev/null
93 $s tee -a "$file" >/dev/null
94 $s tee -a "$file" >/dev/null <<<"$end"
95 fi
96
97 if $exists && $in_section; then
98 while IFS= read -r line; do
99 if [[ $line == "$begin" ]]; then
100 in_section=true;
101 fi
102 if ! $in_section; then
103 printf '%s\n' "$line" >> $file
104 fi
105 if [[ $line == $end ]]; then
106 in_section=false;
107 fi
108 done < <(tail -n +$tailn "$temp")
109 fi
110
111
112 if ! $exists; then
113 ret=1
114 if $verbose; then
115 echo "New file $file:"
116 cat "$file"
117 fi
118 elif type -t diff &>/dev/null; then
119 diff=$(diff -u "$temp" "$file")
120 ret=$?
121 if (( $ret )) && ! $quiet; then
122 echo "backup of original at $temp"
123 echo diff -u "$temp" "$file":
124 echo "$diff"
125 #elif $debug; then
126 # echo "No changes made to $file"
127 fi
128 else
129 # for systems like openwrt which don't have diff
130 diff=$(cmp "$temp" "$file")
131 ret=$?
132 if $verbose; then
133 echo "$diff"
134 fi
135 fi
136 if ! $backup && $exists; then
137 rm -r "$temp"
138 fi
139 return $ret
140 }
141 cedit "$@"