#!/bin/env fish
begin
    set -l dir (status dirname)
    set -l source "$dir/docker-compose.overrides.yml"
    source "$dir/../service.fish"

    function installSW -V dir -V domain -V source
        set -l service teamspeak
        set -l pw (nix-shell -p keepassxc --run "keepassxc-cli generate --length 32")
        set -l db TeamSpeak
        set -l tsEnv "$(getServiceKey "$service").environment"
        set -l dbEnv "$(getServiceKey db).environment"
        initializeServiceInstallation $argv
        sudo cp "$dir/docker-compose.base.yml" (getServiceRoot $argv)

        USER="$service" begin
            yq "$tsEnv.TS3SERVER_DB_USER = env(USER)" "$source" |
                yq "$dbEnv.MARIADB_USER = env(USER)"
        end | \
            PW="$pw" begin
            yq "$tsEnv.TS3SERVER_DB_PASSWORD = env(PW)" |
                yq "$dbEnv.MARIADB_PASSWORD = env(PW)"
        end | \
            DB="$db" begin
            yq "$tsEnv.TS3SERVER_DB_NAME = env(DB)" |
                yq "$dbEnv.MARIADB_DATABASE = env(DB)"
        end |
            sudo tee (getServiceOverrides $argv) >/dev/null

        installDockerService $argv
    end

    function configureSW -V dir
        configureDockerService $argv
    end

    function getServiceServers -V server
    end

    function getServiceLocations
    end

    runInstaller --force $argv
end