#!/usr/bin/env nix-shell
#! nix-shell -i oil -p coreutils curl jq

# Generate the config with Duckduckgo bangs.
# Very useful if you really want just to search as you would in Duckduckgo.

const bang_url = "${BANGS_URL:-https://duckduckgo.com/bang.js}"
const config = {}

# We still have to use some external tools like jq since Oil doesn't support nested objects yet.
# For more information, see https://github.com/oilshell/oil/issues/741
curl --silent --location $bang_url | jq 'sort_by(.t) | .[]' --raw-output --compact-output --sort-keys | while read --line {
    write -- $_line | json read :bang
    var _data = {}
    var trigger = bang['t']

    setvar _data['name'] = bang['s']
    setvar _data['url'] = bang['u']
    setvar config[trigger] = _data
}

json write :config

