#!/bin/env fish
begin
    set -l dir (status dirname)
    set -l service wekan
    set -l domain due ""
    source "$dir/../service.fish"

    function installSW -V dir -V service -V domain
        set -l domain (getServiceDomain $domain)
        set -l overrides (getServiceOverrides $argv)
        initializeServiceInstallation $argv

        sudo cp "$dir/docker-compose.base.yml" (getServiceRoot $argv)

        URL="https://$domain" yq "$(getServiceKey "$service").environment.ROOT_URL = env(URL)" "$dir/$(basename "$overrides")" |
            sudo tee "$overrides" >/dev/null

        installDockerService $argv
    end

    function configureSW -V dir
        configureDockerService $argv
    end

    function getServiceServers -V domain
        printf "%s\0" $domain
    end

    function getServiceLocations -V service
        printf "%s\0" \
            "$service" / "static files" \
            "$service" "~ ^/sockjs/.*/websocket\$" "Websocket"
    end

    function getExtraLocationSettings -a domain s location -V service
        if [ "$location" != "/" ]
            printf "%s\n" \
                '# Websocket' \
                "proxy_http_version 1.1;" \
                'proxy_set_header Upgrade $http_upgrade;' \
                'proxy_set_header Connection "upgrade";'
        end
    end

    runInstaller --force $argv
end