From 89733e2dd26b51c16b2c9f090881127dbba58ce8 Mon Sep 17 00:00:00 2001
From: Dan Fandrich <dan@coneharvesters.com>
Date: Thu, 8 Feb 2024 11:34:34 -0800
Subject: [PATCH] configure: build & install shell completions when enabled

The --with-fish-functions-dir and --with-zsh-functions-dir options
currently have no effect on a normal build because the scripts/ directory
where they're used is not built. Add scripts/ to a normal build and
change the completion options to default to off to preserve the existing
behaviour.

Closes: #12906
---
 Makefile.am         |  2 +-
 configure.ac        | 10 ++++++----
 scripts/Makefile.am | 12 ++++++++----
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index bc995f1ed56067..5bb0e2548228af 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -134,7 +134,7 @@ CLEANFILES = $(VC14_LIBVCXPROJ) $(VC14_SRCVCXPROJ) \
 
 bin_SCRIPTS = curl-config
 
-SUBDIRS = lib src
+SUBDIRS = lib src scripts
 DIST_SUBDIRS = $(SUBDIRS) tests packages scripts include docs
 
 pkgconfigdir = $(libdir)/pkgconfig
diff --git a/configure.ac b/configure.ac
index 09d5364f4de575..c1877771927347 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3483,10 +3483,10 @@ AS_HELP_STRING([--with-zsh-functions-dir=PATH],[Install zsh completions to PATH]
 AS_HELP_STRING([--without-zsh-functions-dir],[Do not install zsh completions]),
   [OPT_ZSH_FPATH=$withval])
 case "$OPT_ZSH_FPATH" in
-  no)
+  default|no)
     dnl --without-zsh-functions-dir option used
     ;;
-  default|yes)
+  yes)
     dnl --with-zsh-functions-dir option used without path
     ZSH_FUNCTIONS_DIR="$datarootdir/zsh/site-functions"
     AC_SUBST(ZSH_FUNCTIONS_DIR)
@@ -3497,6 +3497,7 @@ case "$OPT_ZSH_FPATH" in
     AC_SUBST(ZSH_FUNCTIONS_DIR)
     ;;
 esac
+AM_CONDITIONAL(USE_ZSH_COMPLETION, test x"$ZSH_FUNCTIONS_DIR" != x)
 
 dnl **********************************************************************
 dnl Check for fish completion path
@@ -3508,10 +3509,10 @@ AS_HELP_STRING([--with-fish-functions-dir=PATH],[Install fish completions to PAT
 AS_HELP_STRING([--without-fish-functions-dir],[Do not install fish completions]),
   [OPT_FISH_FPATH=$withval])
 case "$OPT_FISH_FPATH" in
-  no)
+  default|no)
     dnl --without-fish-functions-dir option used
     ;;
-  default|yes)
+  yes)
     dnl --with-fish-functions-dir option used without path
     CURL_CHECK_PKGCONFIG(fish)
     if test "$PKGCONFIG" != "no" ; then
@@ -3527,6 +3528,7 @@ case "$OPT_FISH_FPATH" in
     AC_SUBST(FISH_FUNCTIONS_DIR)
     ;;
 esac
+AM_CONDITIONAL(USE_FISH_COMPLETION, test x"$FISH_FUNCTIONS_DIR" != x)
 
 dnl Now check for the very most basic headers. Then we can use these
 dnl ones as default-headers when checking for the rest!
diff --git a/scripts/Makefile.am b/scripts/Makefile.am
index ae95e85acc98ff..690c328e2812a5 100644
--- a/scripts/Makefile.am
+++ b/scripts/Makefile.am
@@ -40,7 +40,7 @@ $(ZSH_COMPLETION_FUNCTION_FILENAME): completion.pl
 if CROSSCOMPILING
 	@echo "NOTICE: we can't generate zsh completion when cross-compiling!"
 else # if not cross-compiling:
-	@if ! test -x "$(PERL)"; then echo "No perl: can't install completion.pl"; exit 0; fi
+	@if ! test -x "$(PERL)"; then echo "No perl: can't install completion script"; exit 0; fi
 	$(PERL) $(srcdir)/completion.pl --curl $(top_builddir)/src/curl$(EXEEXT) --shell zsh > $@
 endif
 
@@ -48,16 +48,20 @@ $(FISH_COMPLETION_FUNCTION_FILENAME): completion.pl
 if CROSSCOMPILING
 	@echo "NOTICE: we can't generate fish completion when cross-compiling!"
 else # if not cross-compiling:
-	@if ! test -x "$(PERL)"; then echo "No perl: can't install completion.pl"; exit 0; fi
+	@if ! test -x "$(PERL)"; then echo "No perl: can't install completion scriptl"; exit 0; fi
 	$(PERL) $(srcdir)/completion.pl --curl $(top_builddir)/src/curl$(EXEEXT) --shell fish > $@
 endif
 
 install-data-local:
 if CROSSCOMPILING
-	@echo "NOTICE: we can't install zsh completion when cross-compiling!"
+	@echo "NOTICE: we can't install completion scripts when cross-compiling!"
 else # if not cross-compiling:
+if USE_ZSH_COMPLETION
 	$(MKDIR_P) $(DESTDIR)$(ZSH_FUNCTIONS_DIR)
-	$(MKDIR_P) $(DESTDIR)$(FISH_FUNCTIONS_DIR)
 	$(INSTALL_DATA) $(ZSH_COMPLETION_FUNCTION_FILENAME) $(DESTDIR)$(ZSH_FUNCTIONS_DIR)/$(ZSH_COMPLETION_FUNCTION_FILENAME)
+endif
+if USE_FISH_COMPLETION
+	$(MKDIR_P) $(DESTDIR)$(FISH_FUNCTIONS_DIR)
 	$(INSTALL_DATA) $(FISH_COMPLETION_FUNCTION_FILENAME) $(DESTDIR)$(FISH_FUNCTIONS_DIR)/$(FISH_COMPLETION_FUNCTION_FILENAME)
 endif
+endif
