#!/bin/bash

# configs.sh
# Functions for reading rlm.conf config files

file_format="RLM 1.0"

function config_close () {
  ## removes temporary file from last config_open call
  [ -f $config_tmp ] && rm -f $config_tmp
}

function config_open () { # args: filename
  ## parses filename and stores it in simpler format
  config_close
  if [ "`sed 1q "$1"`" != "$file_format" ]; then
    echo "rlm: error: $1: Unknown file format"
    return
  fi
  get_tmp_filename; config_tmp="$file"
  local regexp='^\(\w\+\):[ \t]*\"\([^\"]*\)\"$'
  sed -e "/$regexp/!D" -e "s/$regexp/\1~\2/g" "$1" > "$config_tmp"
}

function config_get_val() { # args: name
  ## retrieves value from currently "open" file
  local line="`grep -P "^$1~" $config_tmp`"
  result="${line#*~}"
}


