if [ -n "$conf_tmpdir" ]; then
  conf_echo "Can not initialize configuration - variable overlap" 1>&2
  return 1
fi

function conf_echo () {
  echo $*
}

function conf_debug () {
#  conf_echo $*
  :
}

function conf_init () {

  if [ -z "$1" ]; then
#    conf_tmpdir=`mktemp -d /tmp/grid_conf.XXXXXX`
    conf_tmpdir=`mktemp /tmp/grid_conf.XXXXXX`
    rm $conf_tmpdir
    mkdir $conf_tmpdir

  else
    conf_tmpdir=$1
  fi
}

function conf_exit () {
  rm -fr $conf_tmpdir
  exit $1
}

function conf_close() {
  if [ -d "$conf_tmpdir" ]; then
    rm -fr $conf_tmpdir
#    declare +r conf_tmpdir
  fi
}

function conf_open () {

    # Allow only letters, numbers, underscore and hyphen in attributes
    # and sections. Sub-sections are devided with forward slashes

    valid_section_tokens="[a-zA-Z0-9_\-/]"
    valid_attribute_tokens="[a-zA-Z0-9_\-]"

    conffile="$1"
    if [ -z "$conf_tmpdir" ]; then
      conf_echo "Configuration not initialized - call conf_init"
      return 1
    fi

    if [ ! -r "$conffile" ]; then
      conf_echo "No such file?"
      return 1
    fi

    newdir=""
    dir=""
    nr=0
    ( cat "$conffile" ; echo ) | while read line ; do

      nr=$[$nr+1]

      # Find section header
      newdir=`echo "$line" | grep '^\[.*\]' | sed 's/^\[\(.*\)\].*/\1/'`
      if [ -n "$newdir" ]; then
        conf_debug "New section [$newdir] on line $nr"
        bad=`echo $newdir | tr -d "$valid_section_tokens"`
        if [ -n "$bad" ]; then
          conf_echo "Error parsing $conffile:"
          conf_echo "   bad attribute token(s) in line $nr in section name [$newdir]: \"$bad\""
          conf_exit 1
        fi
      fi
      dir=${newdir:-$dir}

      if [ -z "$newdir" ]; then
	if [ -n "$dir" ]; then

	  if [ ! -d "$conf_tmpdir/$dir" ]; then
	    mkdir -p $conf_tmpdir/$dir
            touch $conf_tmpdir/$dir/contents
	  fi

          # Remove comments and empty lines
	  line=`echo "$line" | sed 's@^#.*@@' |grep -v '^$' `
          nline=`echo "$line" |grep "^${valid_attribute_tokens}* *="`
          if [ -n "$line" -a -z "$nline" ]; then
            bad=`echo "$line" | cut -f1 -d= | sed 's/ *$//' |tr -d "$valid_attribute_tokens"`
            conf_echo "Error parsing $conffile:"
	    conf_echo "   bad attribute token(s) in line $nr in section [$dir]: \"$bad\""
            conf_exit 1
          fi
          # Allow space around '='
          attr=`echo $nline |cut -f1 -d= |sed 's/ *$//'`
          val=`echo $nline |cut -f2- -d= |sed 's/^ *//'`
          # 
          prev=0
          if [ -r $conf_tmpdir/$dir/contents ]; then
            prev=`grep "^${attr}_*[0-9]*=" $conf_tmpdir/$dir/contents|cut -f1 -d= |tail -1`
            if [ -n "$prev" ] ; then
	      nr=`echo $prev |sed 's/^.*_\([0-9]*\)$/\1/'`
              if [ "$nr" = "$attr" ]; then
                nr=0
              fi
              nr=$[$nr + 1]
              attr="${attr}_$nr"
            fi
          fi
          [ -n "$attr" ] && echo "${attr}=${val}" >> $conf_tmpdir/$dir/contents
        fi
      fi

    done
}

# Read configuration
function conf_read () {
  for i in $* ; do
    if [ -r "$conf_tmpdir/$i/contents" ]; then
      source $conf_tmpdir/$i/contents
    fi
  done
}

function conf_match () {
  if [ -d "$conf_tmpdir/$1" ]; then
    return 0
  else
    return 1
  fi
}

function conf_entries () {
  for i in $conf_tmpdir/$1/*/; do
    if [ -d $i ]; then
      echo -n " "`basename $i`
    fi
  done
}
