Improved lib/sde-package/patch-copyright.sh to remove trailing spaces from the copyri...
[opensde-nopast.git] / bin / sde-config-ini
blobe706cafb61f64b3932961df47b8cad2f4cf5cd64
1 #!/bin/sh
2 # --- SDE-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 #
5 # Filename: bin/sde-config-ini
6 # Copyright (C) 2006 - 2008 The OpenSDE Project
7 #
8 # More information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- SDE-COPYRIGHT-NOTE-END ---
16 [ -n "$SDEROOT" ] ||
17 export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
19 . $SDEROOT/lib/libsde.in
21 ini_usage() {
22 local progname=${0##*/}
23 cat <<EOT
24 Usage: $progname --file <file> --sections
25 $progname --file <file> --keys <section>
26 $progname --file <file> [--delete] section[.key] ...
27 $progname --file <file> section[[.key]=value] ...
29 Valid aliases:
30 --file (-F)
31 --sections (-S)
32 --keys (-K)
33 EOT
36 # splits an .ini file in three parts:
37 # * before the section ($tmpname.0)
38 # * the section ($tmpname.1)
39 # * after the section ($tmpname.2)
41 # USAGE: ini_split "file" "section" "tmpname"
43 ini_split() {
44 local file="$1" section="$2" tmpname="$3"
46 rm -rf $tmpname.{0,1,2}
48 if [ ! -e "$file" ]; then
49 # create the file if it's not there
50 touch "$file" || echo_abort 1 "$file: can't write."
53 [ -f "$file" -a -w "$file" ] || echo_abort 1 "$file: can't write."
55 gawk "BEGIN { level=0; }
56 /^\[/ {
57 if ( level == 0 && \$0 ~ /^\[$section\][ \t]*\$/ )
58 level=1;
59 else if ( level == 1 )
60 level=2;
62 { print > \"$tmpname.\" level }
63 " "$file"
65 # clean empty lines on the section to alter
66 [ ! -s "$tmpname.1" ] || sed -i -e "/^[ \t]*$/d;" "$tmpname.1"
70 # merge a previously split .ini file
71 # USAGE: ini_merge "file" "section" "tmpname"
73 ini_merge() {
74 local file="$1" tmpname="$2"
75 local x=
77 # new empty line at the end of the section, if there is a next
78 if [ -s "$tmpname.1" -a -s "$tmpname.2" ]; then
79 echo >> "$tmpname.1"
82 for x in 0 1 2; do
83 if [ -s $tmpname.$x ]; then
84 cat $tmpname.$x
86 done > "$file"
88 rm -rf $tmpname.{0,1,2}
91 # dumps the raw content of a section
92 # USAGE: ini_section_raw "file" "section"
94 ini_section_raw() {
95 gawk "BEGIN { show=0; }
96 /^\[/ {
97 if ( \$0 ~ /^\[$2\][ \t]*\$/ )
98 show=1;
99 else
100 show=0;
102 /^[^\[]/ { if ( show ) print; }" "$1"
105 shortopts='F:SK:'
106 longopts='file:,sections,keys:,delete'
107 options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
108 if [ $? -ne 0 ]; then
109 ini_usage
110 exit -1
113 # load new arguments list
114 eval set -- "$options"
116 if [ $# -lt 2 ]; then
117 ini_usage
118 exit -1
121 file=
122 delete=
123 while [ $# -gt 0 ]; do
124 case "$1" in
125 -F|--file) shift; file="$1" ;;
126 --delete) delete=yes ;;
128 --) shift; break ;;
130 -S|--sections)
131 [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
133 exec sed -n -e 's,^\[\(.*\)\][ \t]*$,\1,p' "$file"
135 -K|--keys)
136 [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
138 ini_section_raw "$file" "$2" |
139 sed -n -e 's,^[ \t]*\([^;= ]*\)[ \t]*=.*,\1,p'
140 exit $?
142 esac
143 shift
144 done
146 if [ -z "$file" ]; then
147 echo_abort 1 "no ini file specified."
150 oldsection=
151 tmpfile="$file.$$"
152 section= key= value= action=
154 for item; do
155 # parse 'item'
156 section="${item%%.*}"
157 value="${item#*=}"
158 key="${item#*.}"; key="${key%%=*}"
160 [ "$section" != "$key" ] || key=
162 # choose an action
163 if [ "$value" = "$item" ]; then
164 if [ -n "$delete" ]; then
165 action='delete'
166 else
167 action='get'
169 else
170 action='set'
173 case "$action" in
174 get) [ -r "$file" ] || echo_abort 1 "${file:-none}: can't read."
176 if [ -n "$key" ]; then
177 # just on key
178 ini_section_raw "$file" "$section" | sed -n \
179 -e "s,^[ \t]*$key[ \t]*=\(.*\),\1,p"
180 else
181 # the entire section, as variables
182 ini_section_raw "$file" "$section" | sed \
183 -e '/^[ \t]*;/d;' -e '/^[ \t]*$/d;' -e 's,",\\",g' -e 's,=\(..*\)$,="\1",'
184 fi ;;
185 set)
186 if [ -n "$oldsection" -a "$oldsection" != "$section" ]; then
187 # close previous split
188 ini_merge "$file" "$tmpfile"
189 oldsection=
192 if [ -z "$oldsection" ]; then
193 # split before mangling
194 ini_split "$file" "$section" "$tmpfile"
195 oldsection="$section"
198 if [ ! -s "$tmpfile.1" ]; then
199 # new section
200 if [ -s "$tmpfile.0" ]; then
201 # empty line at the end of the last section
202 echo >> "$tmpfile.0"
204 echo -e "[$section]" >> "$tmpfile.1"
207 if grep -q "^[ \t]*$key[ \t]*=" $tmpfile.1; then
208 sed -i -e "s|^[ \t]*$key[ \t]*=.*|$key=$value|" "$tmpfile.1"
209 else
210 echo "$key=$value" >> "$tmpfile.1"
213 delete)
214 if [ -n "$oldsection" -a "$oldsection" != "$section" ]; then
215 # close previous split
216 ini_merge "$file" "$tmpfile"
217 oldsection=
220 if [ -z "$oldsection" ]; then
221 # split before mangling
222 ini_split "$file" "$section" "$tmpfile"
223 oldsection="$section"
226 if [ -n "$key" ]; then
227 # just on key
228 grep -v "^[ \t]*$key[ \t]*=" "$tmpfile.1" > "$tmpfile.1+"
229 mv -f "$tmpfile.1+" "$tmpfile.1"
230 else
231 # the entire section
232 echo "[$section]" > "$tmpfile.1"
235 esac
236 done
238 if [ -n "$oldsection" ]; then
239 ini_merge "$file" "$tmpfile"