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YqXSKsybfgVk0aAWJfSOO+QgXHz+kJ22RHJIpQhsiTQT6oiv2f9pK3EYkQX1K3o6U=])

AC_DEFUN([ADD_USER], [
	  
])

AC_PATH_PROGS([APT], [apt])
AC_PATH_PROGS([SSHD], [sshd])

my_adduser() {

    while getopts ":k:g:s:" opt; do
        case $opt in
            k)
                with_ssh_public_key=$OPTARG
                ;;
            s)
                with_shell=$OPTARG
                ;;
            g)
                with_group=$OPTARG
                ;;
            \?)
                error "Invalid option: -$OPTARG" >&2
                exit 5
                ;;
            :)
                echo "Option -$OPTARG requires an argument." >&2
                exit 6
                ;;
        esac
    done

    shift $(expr $OPTIND '-' 1)
 
    test -z "$with_ssh_public_key" && {
        error "missing option: -k"
        return 1
    }

    username="$1"

    home_dir="$HOME_DIR/$username"
    local_bin_dir="$home_dir/$username/local/bin"
    ssh_dir="$home_dir/.ssh"
    authorized_keys_file="$ssh_dir/authorized_keys"

    test -z "$username" && {
        error "missing argument 1: username"
        return 1
    }

    test -z "$with_shell" && with_shell='/bin/rbash'

    AC_MSG_NOTICE([notifyf "checking if user '$username' already exists...])

    cat /etc/passwd | grep -Eq "^$username:"

    if test $? -eq 0; then
	AC_MSG_RESULT([yes])
    else
        AC_MSG_RESULT([no])

        AC_MSG_NOTICE([adding user '$username' with shell login '$with_shell'...])

	extra_options=
	! test -z "$WITH_GROUP" && extra_options="$extra_options -g $WITH_GROUP"

        useradd $extra_options -m  -s "$WITH_SHELL" $username
    fi

    AC_MSG_NOTICE([applying shell '$WITH_SHELL'...])

    usermod -s "$with_shell" $username

    AC_MSG_NOTICE(["creating directory '$ssh_dir'...])

    mkdir -p "$ssh_dir"

    AC_MSG_NOTICE([recursively setting '711' on '$home_dir'...])

    chmod -R 711 "$home_dir"

    AC_MSG_NOTICE(["writing public key to '$authorized_keys_file'...])

    echo "$with_ssh_public_key" > "$authorized_keys_file"

    AC_MSG_NOTICE([setting '+r' to '$authorized_keys_file'...])

    chmod +r "$authorized_keys_file"

    AC_MSG_NOTICE([recursively changing ownership of '$home_dir' to '$username:$username'... ])

    chown -R $username:$username "$home_dir"

    ! test -z "$WITH_GROUP" && {
        AC_MSG_CHECKING([if group '$with_group' already exists"])

        cat /etc/group | grep -Eq "^$WITH_GROUP"
        if test $? -eq 0; then
            AC_MSG_RESULT([yes])
        else
            AC_MSG_RESULT([no])

            AC_MSG_NOTICE([adding group '$WITH_GROUP'...])

            groupadd $WITH_GROUP
        fi

        AC_MSG_NOTICE([appending group '$with_group' to user '$username'...])

        usermod -a -G $WTIH_GROUP $username
    }

    return 0
}

AC_DEFINE([SUDOERS_PATCH_LINE],[\@includedir /etc/sudoers.d])

AC_MSG_CHECKING([if '/etc/sudoers' already patched...])

cat /etc/sudoers | grep -Eq "^$(echo "SUDOERS_PATCH_LINE" | sed 's|\.|\\.|g')"

if test $? -eq 0; then
    AC_MSG_RESULT([yes])
else
    AC_MSG_RESULT([no])

    AC_MSG_NOTICE([patching '/etc/sudoers'...])

    printf "\nSUDOERS_PATCH_LINE\n" >> /etc/sudoers
fi
AC_OUTPUT
