#!/bin/bash
#
# Client side implementation
# of https://stalw.art/docs/configuration/bootstrap-mode
#
#
if [ -z "${1}" ] ; then
cat << HERE > ./example.stabob
#
# This is a stalwart bootstrap object
# ( https://stalw.art/docs/ref/object/bootstrap/ )
#
# Comments get filtered out through a  grep -v ^#
#
{
  "using": [
    "urn:ietf:params:jmap:core",
    "urn:stalwart:jmap"
  ],
  "methodCalls": [
    [
      "x:Bootstrap/set",
      {
        "update": {
          "singleton": {
            "defaultHostname": "foo.example.net",
            "defaultDomain": "example.com",
            "blobStore": {
              "@type": "Default"
            },
            "dataStore": {
              "@type": "RocksDb",
              "blobSize": 4096,
              "bufferSize": 134217728,
              "path": "/var/lib/stalwart/",
              "poolWorkers": null,
              "cacheSize": null
            },
            "directory": {
              "@type": "Internal"
            },
            "dnsServer": {
              "@type": "Manual"
            },
            "generateDkimKeys": true,
            "inMemoryStore": {
              "@type": "Default"
            },
            "requestTlsCertificate": false,
            "searchStore": {
              "@type": "Default"
            },
            "tracer": {"@type":"Log","path":"/var/log/stalwart/"}
          }
        }
      },
      "set"
    ]
  ]
}
HERE
# error to user and getting user used to JSON
cat << HERE
{ "methodResponses": [
    [ "error", {
        "type": "missing_parameter",
        "description": "a parameter is required",
        "info0": "Current directory now has  example.stabob",
        "info1": "Call this script with such a file as paramater"
      }, "0" ]
  ], "sessionState": "deadbeef" }
HERE
exit 2
fi
# Now using $1 as file with stalwart bootobject data
# Yes, it is an assummption that $1 is a text file
# (No tests will be done on how valid and usable $1 is.)

if [ -z "$STABOB_ADMIN" ] ; then
  STABOB_ADMIN=libre:PasGratuit
  # Using credentials as Debian package default provides
fi

if [ -z "$STABOB_PORT" ] ; then
  STABOB_PORT=10127
  # ssh -L 10127:localhost:60025 stalwart.ser.ver
fi

# Preparation is done, now do
grep -v ^\# "$1" \
| curl -X POST --silent --show-error \
  --max-time 4 --retry 1 --retry-all-errors \
  -u "$STABOB_ADMIN" \
  -H 'Content-Type: application/json' \
  --upload-file - http://127.0.0.1:$STABOB_PORT/jmap/ \
| jq

# Git repository https://codeberg.org/stappers/stalwart-bootstrap
# has a README.md and manual page with further information.

# License is GPL v2 or later

# Author Geert Stappers  <stappers@stappers.it>

# l l
