# $PATCHES stores the user preferences.  $REAL_PATCHES is the subset of
# $PATCHES that is valid for this build.
persistent_add REAL_PATCHES &&
REAL_PATCHES="" &&

# Because the exact order matters.
for PATCH in `grep -v '^#' "${PATCH_DIRECTORY}/PATCH_ORDER"`
do
  if [ -x "${PATCH_DIRECTORY}/${PATCH}/PATCH_DETAILS" ]; then
    PATCH_BRANCHES=""
    PATCH_NEEDS=""
    . "${PATCH_DIRECTORY}/${PATCH}/PATCH_DETAILS" &&
    if ! list_find "${PATCH_BRANCHES}" "${MUTT_BRANCH}"; then
      continue
    fi &&
    for NEED in ${PATCH_NEEDS}
    do
      if ! list_find "${PATCHES}" "${NEED}"; then
        continue 2
      fi
    done &&
    config_query_option PATCHES "Apply the ${PATCH} patch?"            \
                        "n"                                            \
                        "${PATCH}"                                     \
                        "-${PATCH}"                                    &&
    if list_find "${PATCHES}" "${PATCH}"; then
      REAL_PATCHES="${REAL_PATCHES} ${PATCH}"
    fi
  fi
done &&

if spell_ok "linuxdoc-tools"; then
  UPDATE_DOC_DEFAULT="y"
else
  UPDATE_DOC_DEFAULT="n"
fi &&
if [[ "${MUTT_BRANCH}" == "scm" ]]; then
  config_query UPDATE_DOC                                              \
    "Build documentation? (requires linuxdoc-tools)"                   \
    "${UPDATE_DOC_DEFAULT}"
elif [[ ! -z "${REAL_PATCHES}" ]]; then
  config_query UPDATE_DOC                                              \
    "Rebuild documentation affected by patches? (requires linuxdoc-tools)" \
    "${UPDATE_DOC_DEFAULT}"
fi &&

config_query_list   TERMINAL_LIBRARY                                 \
                    "Build against which terminal library?"          \
                    ncurses slang                                    &&

config_query_option MUTT_OPTS \
                    "Enable debugging support?" n \
                    --{enable,disable}-debug &&

config_query_option MUTT_OPTS "Enable POP support?"                  \
                              "y"                                    \
                              "--enable-pop"                         \
                              "--disable-pop"                        &&

config_query_option MUTT_OPTS "Enable IMAP support?"                 \
                              "y"                                    \
                              "--enable-imap"                        \
                              "--disable-imap"                       &&

for PATCH in ${REAL_PATCHES}; do
  if [[ -x "${PATCH_DIRECTORY}/${PATCH}/PATCH_CONFIGURE" ]]; then
    . "${PATCH_DIRECTORY}/${PATCH}/PATCH_CONFIGURE"
  fi
done &&

config_query_option MUTT_OPTS "Enable the header cache?"             \
                              "n"                                    \
                              "--enable-hcache"                      \
                              "--disable-hcache"                     &&
if list_find "${MUTT_OPTS}" "--enable-hcache"; then
  local BACKENDS="gdbm lmdb qdbm db tokyocabinet kyotocabinet" &&

  config_query_list HCACHE_BACKEND "Use which header cache backend?" \
                    ${BACKENDS}
fi &&
config_query_option MUTT_OPTS "Enable SMTP support?"                 \
                              "y"                                    \
                              "--enable-smtp"                        \
                              "--disable-smtp"                       &&

config_query_option MUTT_OPTS "Enable sidebar?"                    \
                              "n"                                  \
                              "--enable-sidebar"                   \
                              "--disable-sidebar"
