
___x_cmd_user_add(){
    local x_; x os name_
    if [ "$x_" != linux ]; then
        x:error "'user add' can only work in linux system. Current os is $_."
    fi

    local user_home
    local user_uid
    local user_gid
    local user_shell=${SHELL:-/bin/bash}
    while [ $# -gt 0 ]; do
    	case $1 in
    		--home) 	[ -z "$2" ] || { user_home="$2"; shift 2; } ;;
    		--uid) 	    [ -z "$2" ] || { user_uid="$2"; shift 2; } ;;
    		--gid) 	    [ -z "$2" ] || { user_gid="$2"; shift 2; } ;;
            --shell) 	[ -z "$2" ] || { user_shell="$2"; shift 2; } ;;
    		*)			break ;;	# Default case: If no more options then break out of the loop.
    	esac
    done

    while [ "$#" -gt 0 ]; do
        local user_name="$1"
        ___x_cmd_user_add___add "$1" || return
        shift
    done
}


___x_cmd_user_add___add(){
    if x hascmd useradd; then
        ___x_cmd_user_add___useradd "$@"
    elif x hascmd adduser; then
        ___x_cmd_user_add___adduser "$@"
    else
        ___x_cmd_user_add___self "$@"
    fi
}

___x_cmd_user_add___useradd(){
	! ___x_cmd_cmds useradd \
			--home-dir "${user_home}" \
			--no-create-home \
			--shell "${user_shell}" \
			--uid "${user_uid}" \
			--gid "${user_gid}" \
			"${user_name}" || return 0

		user:warn "There was a problem setting up the user"
		user:warn "Trying manual addition"
		___x_cmd_user_add___self_passwd_shadow
}


___x_cmd_user_add___adduser(){
	if ! ___x_cmd_cmds addgroup "${user_name}"; then
		user:warn "There was a problem setting up the group"
		user:warn "Trying manual addition"
		printf "%s:x:%s:" "${user_name}" "${user_gid}" >> /etc/group
	fi

	if ! ___x_cmd_cmds adduser \
			--home "${user_home}" \
			--no-create-home \
			--disabled-password \
			--shell "${user_shell}" \
			--uid "${user_uid}" \
			--ingroup "${user_name}" \
			"${user_name}"; then

		user:warn "There was a problem setting up the user"
		user:warn "Trying manual addition"
		___x_cmd_user_add___self_passwd_shadow
	fi
}

___x_cmd_user_add___self(){
    : Creating user by our own script
    user:error "Unimplemented function."
    return 1

    # ___x_cmd_user_add___self_passwd_shadow
}

___x_cmd_user_add___self_passwd_shadow(){
    >>/etc/passwd printf "%s:x:%s:%s:%s:%s:%s" \
	    "${user_name}" "${user_uid}" "${user_gid}" "${user_name}" "${user_home}" "${user_shell}"
	>>/etc/shadow printf "%s::1::::::" "${user_name}"
}
