Autodetect spidermonkey version in ./configure

I have struggled with this point when building
couchDB, because it's written nowhere in the documentation
that you need to configure the version properly.

It seems that other people had the problem as well:
https://couchdb.slack.com/archives/C49LEE7NW/p1632249155263400

The updated configure script automatically finds
out the installed library version. It also introduces
a dependency on `sed`, `ldconfig` and `head`,
and remove the default to version 1.8.5 which is old
and not in most distributions anymore.

The argument was also removed from the CI scripts
because it does not exist anymore.
diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml
index 4de1bc8..7636914 100644
--- a/.github/workflows/ubuntu.yml
+++ b/.github/workflows/ubuntu.yml
@@ -88,7 +88,7 @@
         cd couch
         sed -i -e "s@DRV_CFLAGS -DPIC@DRV_CFLAGS -I/usr/local/include -DPIC@" src/couch/rebar.config.script
         sed -i -e "s@DRV_LDFLAGS -lm@DRV_LDFLAGS -L/usr/local/lib -lm@" src/couch/rebar.config.script
-        sh ./configure --spidermonkey-version=91 --disable-docs
+        sh ./configure --disable-docs
     - name: Compile
       run: |
         cd couch
diff --git a/build-aux/Jenkinsfile.full b/build-aux/Jenkinsfile.full
index 26cec87..8a5fbb6 100644
--- a/build-aux/Jenkinsfile.full
+++ b/build-aux/Jenkinsfile.full
@@ -25,37 +25,31 @@
 meta = [
   'centos7': [
     name: 'CentOS 7',
-    spidermonkey_vsn: '1.8.5',
     image: "apache/couchdbci-centos:7-erlang-${ERLANG_VERSION}"
   ],
 
   'centos8': [
     name: 'CentOS 8',
-    spidermonkey_vsn: '60',
     image: "apache/couchdbci-centos:8-erlang-${ERLANG_VERSION}"
   ],
 
   'bionic': [
     name: 'Ubuntu 18.04',
-    spidermonkey_vsn: '1.8.5',
     image: "apache/couchdbci-ubuntu:bionic-erlang-${ERLANG_VERSION}"
   ],
 
   'focal': [
     name: 'Ubuntu 20.04',
-    spidermonkey_vsn: '68',
     image: "apache/couchdbci-ubuntu:focal-erlang-${ERLANG_VERSION}"
   ],
 
   'stretch': [
     name: 'Debian 9',
-    spidermonkey_vsn: '1.8.5',
     image: "apache/couchdbci-debian:stretch-erlang-${ERLANG_VERSION}"
   ],
 
   'buster': [
     name: 'Debian 10',
-    spidermonkey_vsn: '60',
     image: "apache/couchdbci-debian:buster-erlang-${ERLANG_VERSION}"
   ],
 
@@ -63,21 +57,18 @@
 
   // 'buster-arm64': [
   //   name: 'Debian 10 ARM'
-  //   spidermonkey_vsn: '60',
   //   image: "apache/couchdbci-debian:arm64v8-buster-erlang-${ERLANG_VERSION}",
   //   node_label: 'arm64v8'
   // ],
 
   // 'buster-ppc64': [
   //   name: 'Debian 10 POWER'
-  //   spidermonkey_vsn: '60',
   //   image: "apache/couchdbci-debian:pp64le-buster-erlang-${ERLANG_VERSION}",
   //   node_label: 'ppc64le'
   // ],
 
   'bullseye': [
     name: 'Debian 11',
-    spidermonkey_vsn: '78',
     image: "apache/couchdbci-debian:bullseye-erlang-${ERLANG_VERSION}"
   ],
 
@@ -119,12 +110,13 @@
             withEnv([
                 'HOME='+pwd(),
                 'PATH+USRLOCAL=/usr/local/bin',
-                'MAKE='+meta[platform].gnu_make
+                'MAKE='+meta[platform].gnu_make,
+                'SM_VSN='+meta[platform].spidermonkey_vsn
                 ]) {
               sh( script: "mkdir -p ${COUCHDB_IO_LOG_DIR} ${platform}/build", label: 'Create build directories' )
               sh( script: "tar -xf apache-couchdb-*.tar.gz -C ${platform}/build --strip-components=1", label: 'Unpack release' )
               dir( "${platform}/build" ) {
-                sh "./configure --skip-deps --spidermonkey-version ${meta[platform].spidermonkey_vsn}"
+                sh "./configure --skip-deps"
                 sh '$MAKE'
                 sh '$MAKE eunit'
                 sh '$MAKE elixir-suite'
@@ -176,7 +168,7 @@
                   sh( script: "mkdir -p ${COUCHDB_IO_LOG_DIR} ${platform}/build", label: 'Create build directories' )
                   sh( script: "tar -xf apache-couchdb-*.tar.gz -C ${platform}/build --strip-components=1", label: 'Unpack release' )
                   dir( "${platform}/build" ) {
-                    sh "./configure --skip-deps --spidermonkey-version ${meta[platform].spidermonkey_vsn}"
+                    sh "./configure --skip-deps"
                     sh 'make'
                     sh 'make eunit'
                     sh 'make elixir-suite'
@@ -271,14 +263,10 @@
           registryCredentialsId 'dockerhub_creds'
         }
       }
-      environment {
-        // TODO find a way to avoid setting this explicitly
-        spidermonkey = '78'
-      }
       steps {
         timeout(time: 15, unit: "MINUTES") {
           sh (script: 'rm -rf apache-couchdb-*', label: 'Clean workspace of any previous release artifacts' )
-          sh "./configure --spidermonkey-version ${spidermonkey}"
+          sh "./configure"
           sh 'make erlfmt-check'
           sh 'make elixir-check-formatted'
           sh 'make dist'
diff --git a/configure b/configure
index c016c31..866bb81 100755
--- a/configure
+++ b/configure
@@ -29,11 +29,22 @@
 SKIP_DEPS=0
 
 COUCHDB_USER="$(whoami 2>/dev/null || echo couchdb)"
-SM_VSN=${SM_VSN:-"1.8.5"}
+OS="$(uname)"
 ARCH="$(uname -m)"
 
+SM_VSN=${SM_VSN:-$([ "$OS" = "Linux" ] || echo "1.8.5")}
+# Try to detect SpiderMonkey (libmozjs) version automatically on linux
+SM_VSN=${SM_VSN:-$(/sbin/ldconfig -p | sed -n 's/.libmozjs-*\([0-9]*\).*/\1/p' | head -n 1)}
+if [ -z "$SM_VSN" ]; then
+  SM_VSN="1.8.5"
+fi
+
+if [ "$SM_VSN" = "185" ]; then
+  SM_VSN="1.8.5"
+fi
+
 . ${rootdir}/version.mk
-COUCHDB_VERSION=${vsn_major}.${vsn_minor}.${vsn_patch}
+COUCHDB_VERSION=${vsn_major}.${vsn_minor}.${vsn_patch}src/couch/rebar.config.script
 
 display_help () {
     cat << EOF
@@ -211,6 +222,14 @@
 
 parse_opts $@
 
+# If SM_VSN is empty, the SpiderMonkey version could not be detected,
+# and it was not set with the CLI argument. Quit the script and warn the user.
+if [ -z "$SM_VSN" ]; then
+  echo 'ERROR: Could not detect SpiderMonkey version. Ensure SpyderMonkey is installed' \
+       'or set the version explicitly with "--spidermonkey-version".'
+  exit 1
+fi
+
 if [ "${ARCH}" = "aarch64" ] && [ "${SM_VSN}" = "60" ]
 then
   echo "ERROR: SpiderMonkey 60 is known broken on ARM 64 (aarch64). Use another version instead."