#!/usr/bin/bash
# lumrebaseuser - move users entries to different subhier

# source bash base library
# shellcheck disable=SC1091
source /usr/libexec/bash-base.bash || {
        echo "$0: fatal error: failed to source /usr/libexec/bash-base.bash" >&2
        exit 1
}

bb_require_libs bash-ini ldapusermgmt/common

# shellcheck disable=SC2120,SC2034
function local_usage() {
        usage "${1-1}" "${2-}" "subhier user-cn [..]" <<EOF
  Move one or more users specified by their cn to another subhier.
EOF
}

# move_user <subhier> <cn>
function move_user() {
        (( $# == 2 )) || bb_fatal "move_user called with $# instead of 2 args"

        local _subhier="$1"
        local _cn="$2"

        local _user_dn # _group_dn _autofs_dn
        _user_dn=$(ldap_cmd read ldapsearch -QLLL \
                -b "ou=$_subhier,${LDAP_USER_SUBHIER},${LDAP_SEARCH_BASE}" \
                cn="$_cn")
        if [ -z "$_user_dn" ]
        then
                bb_msg err "no user found with cn: $_cn"
                return 0
        fi
        _user_dn="${_user_dn#dn: }"
  echo "DEBUG FOUND USER with dn: $_user_dn"
}

function main() {
    FIXME
        parse_common_args

        # shellcheck disable=SC2119
        (( $# > 1 )) || local_usage "Missing mandatory argument"

        load_config

        local subhier="$1"
        shift

        while (( $# > 0 ))
        do
                move_user "$subhier" "$1"
        done
}

main "$@"
bb_quit
