# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing,
#   software distributed under the License is distributed on an
#   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#   KIND, either express or implied.  See the License for the
#   specific language governing permissions and limitations
#   under the License.
---
name: couchdb
version: 3.4.3
type: app
base: core22
license: Apache-2.0
summary: Official Apache CouchDB snap - a clustering document oriented database
description: |
  CouchDB is a database that completely embraces the web. Store your data with
  JSON documents. Access your documents and query your indexes with your web
  browser, via HTTP. Index, combine, and transform your documents with
  JavaScript.
assumes: [command-chain, common-data-dir]
grade: stable
confinement: strict

architectures:
  - build-on: [amd64]
  - build-on: [armhf]
  - build-on: [arm64]
  - build-on: [s390x]
  - build-on: [ppc64el]

parts:
  erl-iex:
    plugin: dump
    build-environment:
      - ASDF_ERLANG_VERSION: "25.3.2.19"
      - ASDF_ELIXIR_VERSION: "1.17.3-otp-25"
    source: https://github.com/asdf-vm/asdf.git
    source-branch: v0.16.7
    source-type: git
    build-packages:
      - libncurses-dev
      - unzip
      - curl
    override-build: |
      source "./asdf.sh"
      
      # Install Erlang
      asdf plugin add erlang
      asdf install erlang "$ASDF_ERLANG_VERSION"
      asdf global erlang "$ASDF_ERLANG_VERSION"
      
      # Install Erlang
      asdf plugin add elixir
      asdf install elixir "$ASDF_ELIXIR_VERSION"
      asdf global elixir "$ASDF_ELIXIR_VERSION"
  couchdb:
    after: [erl-iex]
    plugin: make
    build-environment:
      - SPIDERMONKEY_VERSION: "102"
    source: https://dlcdn.apache.org/couchdb/source/3.4.3/apache-couchdb-3.4.3.tar.gz
    source-type: tar
    override-build: |
      # Use the version of erlang/elixir we configured before
      source "$(pwd)/../../erl-iex/build/asdf.sh"
      
      # Configure, build and release CouchDB binaries
      ./configure --spidermonkey-version "$SPIDERMONKEY_VERSION" && make release && cp -r rel/couchdb/* $SNAPCRAFT_PART_INSTALL
      
      # Verifying that all is working
      #make check
    build-packages:
      - gcc
      - g++
      - libc6-dev
      - libicu-dev
      - libssl-dev
      # Mozilla JS engine
      - libmozjs-102-dev
    stage-packages:
      - libicu70
      - libssl3
      - libmozjs-102-0
    stage:
      - -usr/share/doc
      - -usr/share/man
      - -var

environment:
  COUCHDB_ARGS_FILE: ${SNAP_DATA}/etc/vm.args
  COUCHDB_FAUXTON_DOCROOT: ${SNAP}/share/www
  COUCHDB_QUERY_SERVER_JAVASCRIPT: ${SNAP}/bin/couchjs ${COUCHJS_ARGS} ${SNAP}/share/server/main.js
  COUCHDB_QUERY_SERVER_COFFEESCRIPT: ${SNAP}/bin/couchjs ${COUCHJS_ARGS} ${SNAP}/share/server/main-coffee.js
  COUCHDB_INI_FILES: ${SNAP}/etc/default.ini ${SNAP}/etc/default.d ${SNAP_DATA}/etc/local.ini ${SNAP_DATA}/etc/local.d

apps:
  couchdb:
    command: bin/couchdb
    plugs: [network, network-bind, mount-observe]
  server:
    daemon: simple
    command: bin/couchdb
    plugs: [network, network-bind, mount-observe]
  remsh:
    command: bin/remsh
    plugs: [network, network-bind]
  couchjs:
    command: bin/couchjs
    plugs: [network, network-bind]
