# Config backend

if test -f "$SYSCONFDIR/config.cf"; then
  . $SYSCONFDIR/config.cf
fi

_config_init() {
  case "$ULTRAPOSSUM_CONFIG_BACKEND" in
  ldap)
    ldapsearch -LLL -x -H "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_URI" -b "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BASEDN" -D "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDDN" -w "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDPW" description=\* cn description  | egrep '(cn:|description:)' | uniq | while read cn; do
    read description
    if echo $description | grep -v :: > /dev/null; then
      cn=`echo $cn | cut -d' ' -f2-`
      description=`echo $description | cut -d' ' -f2-`
      if test "x${!cn}" = "x"; then
        echo "$cn='$description'"
      fi
    fi
  done
  ;;
  esac
}

eval `_config_init`

# ultrapossum_getconf <name> <default>
ultrapossum_getconf() {
  case "$ULTRAPOSSUM_CONFIG_BACKEND" in
    *)
      echo ${!1:-$2}
    ;;
  esac
}


# ultrapossum_setconf <category> <name> <val>
ultrapossum_setconf() {
  if test "x$1" = "x.status"; then
    touch $localstatedir/lib/$PACKAGE/status
    ultrapossum_setconf_shell $localstatedir/lib/$PACKAGE/status $2 "$3"
  else
    case "$ULTRAPOSSUM_CONFIG_BACKEND" in
      ldap|*)
        if test "x$1" = "x."; then
          config=$SYSCONFDIR/ultrapossum.cf
        elif test "x$1" = "xsecret"; then
          config=$SYSCONFDIR/secret.cf
        else
          config=$SYSCONFDIR/module.d/$1.cf
        fi
        touch $config
        ultrapossum_setconf_shell $config $2 "$3"
      ;;
    esac
  fi
}

# ultrapossum_setconf <path> <name> <val>
ultrapossum_setconf_shell() {
  config=$1
  val=$3
  tmpsetconf=`tempfile`
  /bin/cp -p $config $tmpsetconf
  if grep "^$2=" $config > /dev/null; then
    sed -e "s!^$2=.*\$!$2=\"$val\"!" < $config > $tmpsetconf 
  elif grep "^#$2=" $config > /dev/null; then
    sed -e "/#$2=.*\$/a\\" -e "$2=\"$val\"" < $config > $tmpsetconf
  else
    echo "$2=\"$val\"" >> $tmpsetconf
  fi
  /bin/mv $tmpsetconf $config
}

# ultrapossum_removeconf <category> <var>
ultrapossum_removeconf() {
  case "$ULTRAPOSSUM_CONFIG_BACKEND" in
    ldap|*)
      ultrapossum_removeconf_shell "$1" "$2"
    ;;
  esac
}

# ultrapossum_removeconf_shell <category> <var>
ultrapossum_removeconf_shell() {
  if test "x$1" = "x."; then
    config=$SYSCONFDIR/ultrapossum.cf
  elif test "x$1" = "xsecret"; then
    config=$SYSCONFDIR/secret.cf
  elif test "x$1" = "x.status"; then
    config=$localstatedir/lib/$PACKAGE/status
  else
    config=$SYSCONFDIR/module.d/$1.cf
  fi
  if test -f "$config"; then
    removetmp=`tempfile`
    /bin/cp -p $config $removetmp
    egrep -v "^$2=" $config > $removetmp || true
    /bin/cp -p $removetmp $config
    /bin/rm -f $removetmp
  fi
}

# ultrapossum_parse_config <config>
ultrapossum_parse_config() {

  if test "x$ULTRAPOSSUMDEFAULT" != "x"; then
    return
  fi

  case "$ULTRAPOSSUM_CONFIG_BACKEND" in
    ldap|*)
      ultrapossum_parse_config_shell "$MODULECONFDIR/$1.cf"
    ;;
  esac

}

# ultrapossum_parse_config_shell <path>
ultrapossum_parse_config_shell() {
  if test -f "$1"; then
    fs="$1"
  fi
  if test "x$EXTRACONFSUFFIX" != "x" && test -f "$1$EXTRACONFSUFFIX"; then
    fs="$fs $1$EXTRACONFSUFFIX"
  fi
  for f in $fs; do
    if test -r "$f"; then
      cat $f
    else
      echo "Warning: no permission to read $f" 1>&2
    fi
  done
}


# retrieve the known variables
# getvariable [<module>...]
getvariable() {
  if test "x$1" = "x"; then
    modules=`ultrapossum-config module`
  else
    modules="$@"
  fi
  case "$ULTRAPOSSUM_CONFIG_BACKEND" in
  ldap)
    if test "x$cache" != "x" && test -f "$CACHEDIR/$cache"; then
      grep -v '#' "$CACHEDIR/$cache" | sed 's/^\([^=]*\)=.*"/\1/'
    else
      ( ldapsearch -LLL -x -H "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_URI" -b "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BASEDN" -D "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDDN" -w "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDPW" objectClass=\* cn  | egrep cn: | cut -d' ' -f2-
      getvariable_shell $modules ) | sort | uniq
    fi
    ;;
  *)
    getvariable_shell $modules
    ;;
  esac
}

getvariable_shell() {
 ( for m in "$@"
  do
    if test -f "$MODULEDIR/$m/variable-$m"; then
      cat "$MODULEDIR/$m/variable-$m"
    else
      if test -d "$MODULEDIR/$m/"; then
        /bin/ls $MODULEDIR/[0-9][0-9]$m | while read f; do
          egrep "[A-Z_][A-Z_]*=" $f | cut -d= -f1 | grep -v IFS | tr -d ' ' | sort | uniq
        done
      else
        echo "E: No such module: $m" 1>&2
        return 1
      fi
    fi
  done
  cat $SHAREDIR/variable ) | sort | uniq
}

# retrieve current values of the specified variables
# getconfig [<var...>]
getconfig() {
  if test "x$1" = "x"; then
    ultrapossum-config variable | while read a
    do
      echo $a=\"${!a}\"
    done
  else
    for a in "$@"; do
      echo $a=\"${!a}\"
    done
  fi
}

# retrieve the value of the specified varialble in the specified status file
# getvalues <status> [<var>...]
getvalues() {
  if test "x$2" = "x"; then
    cat "$1"
  else
    f=$1
    shift
    for a in $@; do
      egrep "^$a=" "$f"
    done
  fi
}

# getvalue <status> <var>
getvalue() {
  getvalues "$1" "$2" | cut -d= -f2- | sed -e 's/"\(.*\)"/\1/' 
}
