add silent mode
[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 -s Silent. Quiet and exit 0 on modified file.
29 -v Verbose
30 -b Keep backup file
31 -h|--help Help"
32 local s diff name init file_dir exists verbose backup quiet silent
33 file_dir="$(dirname "$file")"
34 exists=true
35 verbose=false
36 backup=false
37 quiet=false
38 silent=false
39
40 case $1 in
41 -b) backup=true; shift ;;
42 -v) verbose=true; shift ;;
43 -q) quiet=true; shift ;;
44 -s) quiet=true; silent=true; shift ;;
45 -h|--help) echo "$help"; return ;;
46 esac
47
48 if (( $# == 2 )); then
49 name=": $1"
50 shift
51 fi
52
53 local file="$1"
54 local file_name="${file##*/}"
55
56 local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#"
57 local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#"
58
59 if [[ ! -e $file_dir ]]; then
60 if ! mkdir -p $file_dir; then
61 s=sudo
62 $s mkdir -p $file_dir || return 2
63 fi
64 fi
65 if [[ ! -e $file ]]; then
66 exists=false
67 if ! $s touch $file; then
68 s=sudo
69 $s touch $file || return 2
70 fi
71 fi
72
73 [[ -w $file ]] || s=sudo
74
75
76 local in_section=false
77 if $exists; then
78 local tailn=1
79 local temp="$(mktemp -d)/$file_name"
80 cp "$file" "$temp"
81 cp /dev/null "$file"
82 while IFS= read -r line; do
83 tailn=$(( tailn + 1 ))
84 if [[ $line == "$begin" ]]; then
85 in_section=true;
86 break
87 fi
88 printf '%s\n' "$line" >> $file
89 done < "$temp"
90 fi
91
92 IFS= read -d '' -n 1 -r init
93 if [[ $init ]]; then
94 $s tee -a "$file" >/dev/null <<<"$begin"
95 printf '%s' "$init" | $s tee -a "$file" >/dev/null
96 $s tee -a "$file" >/dev/null
97 $s tee -a "$file" >/dev/null <<<"$end"
98 fi
99
100 if $exists && $in_section; then
101 while IFS= read -r line; do
102 if [[ $line == "$begin" ]]; then
103 in_section=true;
104 fi
105 if ! $in_section; then
106 printf '%s\n' "$line" >> $file
107 fi
108 if [[ $line == $end ]]; then
109 in_section=false;
110 fi
111 done < <(tail -n +$tailn "$temp")
112 fi
113
114
115 if ! $exists; then
116 ret=1
117 if $verbose; then
118 echo "New file $file:"
119 cat "$file"
120 fi
121 elif type -t diff &>/dev/null; then
122 diff=$(diff -u "$temp" "$file")
123 ret=$?
124 if (( $ret )) && ! $quiet; then
125 echo "backup of original at $temp"
126 echo diff -u "$temp" "$file":
127 echo "$diff"
128 #elif $debug; then
129 # echo "No changes made to $file"
130 fi
131 else
132 # for systems like openwrt which don't have diff
133 diff=$(cmp "$temp" "$file")
134 ret=$?
135 if $verbose; then
136 echo "$diff"
137 fi
138 fi
139 if ! $backup && $exists; then
140 rm -r "$temp"
141 fi
142 if $silent; then
143 case $ret in
144 0|1) return 0 ;;
145 *) return $ret ;;
146 esac
147 else
148 return $ret
149 fi
150 }
151 cedit "$@"