# 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
    cn=`echo $cn | cut -d' ' -f2-`
    description=`echo $description | cut -d' ' -f2-`
    if test "x${!cn}" = "x"; then
      echo "$cn='$description'"
    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)
v="dn: cn=$2,$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BASEDN
objectClass: device
cn: $2
description: $3
"
        if echo "$v" | ldapadd -x -H "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_URI" -D "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDDN" -w "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDPW" > /dev/null 2>&1; then 
          :
        else
          if test "$?" = "68"; then
        ldapmodify -x -H "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_URI" -D "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDDN" -w "$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BINDPW" > /dev/null <<EOF
dn: cn=$2,$ULTRAPOSSUM_CONFIG_BACKEND_LDAP_BASEDN
changetype: modify
replace: description
description: $3
EOF
          fi 
         fi
       ;;
      *)
        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_parse_config <config>
ultrapossum_parse_config() {

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

  case "$ULTRAPOSSUM_CONFIG_BACKEND" in
    ldap)
    ;;
    *)
      if test -f "$MODULECONFDIR/$1.cf"; then
        fs="$MODULECONFDIR/$1.cf"
      fi
      if test "x$EXTRACONFSUFFIX" != "x" && test -f "$MODULECONFDIR/$1.cf$EXTRACONFSUFFIX"; then
        fs="$fs $MODULECONFDIR/$1.cf$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

   ;;
  esac

}

# 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-
    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/' 
}
