inc/ini-config
bf2ad701
 #!/bin/bash
 #
 # **inc/ini-config** - Configuration/INI functions
 #
 # Support for manipulating INI-style configuration files
 #
 # These functions have no external dependencies and no side-effects
 
 # Save trace setting
 INC_CONF_TRACE=$(set +o | grep xtrace)
 set +o xtrace
 
 
 # Config Functions
 # ================
 
 # Append a new option in an ini file without replacing the old value
f44a024f
 # iniadd [-sudo] config-file section option value1 value2 value3 ...
bf2ad701
 function iniadd {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="-sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
     shift 3
 
     local values="$(iniget_multiline $file $section $option) $@"
f44a024f
     iniset_multiline $sudo $file $section $option $values
bf2ad701
     $xtrace
 }
 
 # Comment an option in an INI file
f44a024f
 # inicomment [-sudo] config-file section option
bf2ad701
 function inicomment {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
 
f44a024f
     $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
bf2ad701
     $xtrace
 }
 
 # Get an option from an INI file
 # iniget config-file section option
 function iniget {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
     local file=$1
     local section=$2
     local option=$3
     local line
 
     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
     echo ${line#*=}
     $xtrace
 }
 
 # Get a multiple line option from an INI file
 # iniget_multiline config-file section option
 function iniget_multiline {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
     local file=$1
     local section=$2
     local option=$3
     local values
 
     values=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { s/^$option[ \t]*=[ \t]*//gp; }" "$file")
     echo ${values}
     $xtrace
 }
 
 # Determinate is the given option present in the INI file
 # ini_has_option config-file section option
 function ini_has_option {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
     local file=$1
     local section=$2
     local option=$3
     local line
 
     line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
     $xtrace
     [ -n "$line" ]
 }
 
 # Add another config line for a multi-line option.
 # It's normally called after iniset of the same option and assumes
 # that the section already exists.
 #
 # Note that iniset_multiline requires all the 'lines' to be supplied
 # in the argument list. Doing that will cause incorrect configuration
 # if spaces are used in the config values.
 #
f44a024f
 # iniadd_literal [-sudo] config-file section option value
bf2ad701
 function iniadd_literal {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
     local value=$4
 
92884ede
     if [[ -z $section || -z $option ]]; then
         $xtrace
         return
     fi
bf2ad701
 
     # Add it
f44a024f
     $sudo sed -i -e "/^\[$section\]/ a\\
bf2ad701
 $option = $value
 " "$file"
 
     $xtrace
 }
 
 # Remove an option from an INI file
f44a024f
 # inidelete [-sudo] config-file section option
bf2ad701
 function inidelete {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
 
92884ede
     if [[ -z $section || -z $option ]]; then
         $xtrace
         return
     fi
bf2ad701
 
     # Remove old values
f44a024f
     $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
bf2ad701
 
     $xtrace
 }
 
 # Set an option in an INI file
f44a024f
 # iniset [-sudo] config-file section option value
cede7874
 #  - if the file does not exist, it is created
bf2ad701
 function iniset {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
     local value=$4
 
92884ede
     if [[ -z $section || -z $option ]]; then
         $xtrace
         return
     fi
bf2ad701
 
     if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
         # Add section at the end
f44a024f
         echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
bf2ad701
     fi
     if ! ini_has_option "$file" "$section" "$option"; then
         # Add it
f44a024f
         $sudo sed -i -e "/^\[$section\]/ a\\
bf2ad701
 $option = $value
 " "$file"
     else
         local sep=$(echo -ne "\x01")
         # Replace it
f44a024f
         $sudo sed -i -e '/^\['${section}'\]/,/^\[.*\]/ s'${sep}'^\('${option}'[ \t]*=[ \t]*\).*$'${sep}'\1'"${value}"${sep} "$file"
bf2ad701
     fi
     $xtrace
 }
 
 # Set a multiple line option in an INI file
f44a024f
 # iniset_multiline [-sudo] config-file section option value1 value2 valu3 ...
bf2ad701
 function iniset_multiline {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
 
     shift 3
     local values
     for v in $@; do
         # The later sed command inserts each new value in the line next to
         # the section identifier, which causes the values to be inserted in
         # the reverse order. Do a reverse here to keep the original order.
         values="$v ${values}"
     done
     if ! grep -q "^\[$section\]" "$file"; then
         # Add section at the end
f44a024f
         echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
bf2ad701
     else
         # Remove old values
f44a024f
         $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
bf2ad701
     fi
     # Add new ones
     for v in $values; do
f44a024f
         $sudo sed -i -e "/^\[$section\]/ a\\
bf2ad701
 $option = $v
 " "$file"
     done
     $xtrace
 }
 
 # Uncomment an option in an INI file
 # iniuncomment config-file section option
 function iniuncomment {
433a9b10
     local xtrace
     xtrace=$(set +o | grep xtrace)
bf2ad701
     set +o xtrace
f44a024f
     local sudo=""
     if [ $1 == "-sudo" ]; then
         sudo="sudo "
         shift
     fi
bf2ad701
     local file=$1
     local section=$2
     local option=$3
f44a024f
     $sudo sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
bf2ad701
     $xtrace
 }
 
 # Restore xtrace
 $INC_CONF_TRACE
 
 # Local variables:
 # mode: shell-script
 # End: