# --- SDE-COPYRIGHT-NOTE-BEGIN ---
|
|
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
|
|
#
|
|
# Filename: lib/libsde-ini.in
|
|
# Copyright (C) 2006 The OpenSDE Project
|
|
#
|
|
# More information can be found in the files COPYING and README.
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; version 2 of the License. A copy of the
|
|
# GNU General Public License can be found in the file COPYING.
|
|
# --- SDE-COPYRIGHT-NOTE-END ---
|
|
|
|
# this file has to be sourced only by tools which really want to
|
|
# write ini formated files. reading methods are available at libsde.in
|
|
|
|
# writes a list of variables into a section of an ini file
|
|
# USAGE: ini_write "file" "section" var1 var2 var3 ...
|
|
#
|
|
ini_write() {
|
|
local file="$1" section="$2" var= c=
|
|
local tmpfile=
|
|
shift 2
|
|
|
|
# something to write?
|
|
[ $# -gt 0 ] || return
|
|
|
|
# make sure the file exists
|
|
[ -f "$file" ] || touch "$file"
|
|
tmpfile="$file.$$"
|
|
|
|
# FIXME: a lock may be needed
|
|
if grep -q "^\[$section\][ \t]*$" "$file"; then
|
|
# known section
|
|
#
|
|
|
|
#FIXME: better performance may be required
|
|
|
|
# split the file
|
|
rm -rf $tmpfile.{0,1,2}
|
|
gawk "BEGIN { level=0; }
|
|
/^\[/ {
|
|
if ( level == 0 && \$0 ~ /^\[$section\][ \t]*\$/ )
|
|
level=1;
|
|
else if ( level == 1 )
|
|
level=2;
|
|
}
|
|
{ print > \"$tmpfile.\" level }
|
|
" "$file"
|
|
|
|
# remove empty lines
|
|
sed -i -e '/^[ \t]*$/d;' $tmpfile.1
|
|
|
|
# edit the section
|
|
for var; do
|
|
if grep -q "^$var=" $tmpfile.1; then
|
|
sed -i -e "s|^$var=.*|$var=$( eval echo \$$var )|" $tmpfile.1
|
|
else
|
|
echo "$var=$( eval echo \$$var )" >> $tmpfile.1
|
|
fi
|
|
done
|
|
|
|
# and finally write the new file!
|
|
(
|
|
cat $tmpfile.0
|
|
cat $tmpfile.1
|
|
if [ -s $tmpfile.2 ]; then
|
|
echo
|
|
cat $tmpfile.2
|
|
fi
|
|
) > "$file"
|
|
|
|
rm -rf $tmpfile.{0,1,2}
|
|
else
|
|
# new section
|
|
#
|
|
echo -e "\n[$section]" >> "$file"
|
|
for var; do
|
|
echo "$var=$( eval echo \$$var )"
|
|
done >> "$file"
|
|
fi
|
|
}
|