#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: 2016 Matt Churchyard <churchers@gmail.com>

# for a manual switch the bridge interface should already exist
# we just assign our description so it's visible in ifconfig
#
# @param string _name name of the switch
#
switch::manual::init(){
    local _name="$1"
    local _bridge="$2"

    # get bridge
    if [ -z "${_bridge}" ]; then
        config::core::get "_bridge" "bridge_${_name}"
        [ -z "${_bridge}" ] && return 1
    fi

    # don't rename custom bridges nor set a description.
    # manual interfaces are fully configured using rc.conf.
    ifconfig "${_bridge}" group vm-switch up >/dev/null 2>&1
    switch::set_viid "${_name}" "${_bridge}"
}

# show the configuration details for a manual switch
#
# @param string _name the switch name
# @param string _format output format
#
switch::manual::show(){
    local _name="$1"
    local _format="$2"
    local _bridge _priv

    config::core::get "_bridge" "bridge_${_name}"
    config::core::get "_priv" "private_${_name}" "no"
    printf "${_format}" "${_name}" "manual" "${_bridge}" "n/a" "${_priv}" "n/a" "n/a" "n/a"
}

# create a manual switch
# we just assign our switch name to the existing bridge
#
switch::manual::create(){

    # we need to have a bridge
    [ -z "${_bridge}" ] && util::err "you must specify a bridge to import when creating a manual switch"

    # check we can find this bridge on the system
    ifconfig "${_bridge}" >/dev/null 2>&1
    [ $? -eq 0 ] || util::err "${_bridge} does not appear to be a valid existing bridge"

    # store configuration
    config::core::set "switch_list" "${_switch}" "1"
    config::core::set "type_${_switch}" "manual"
    config::core::set "bridge_${_switch}" "${_bridge}"

    [ -n "${_priv}" ] && config::core::set "private_${_switch}" "${_priv}"

    # import
    switch::manual::init "${_switch}" "${_bridge}"
}

# remove a manual switch
#
# @param string _switch the name of the switch
#
switch::manual::remove(){
    local _switch="$1"
    local _id

    switch::manual::id "_id" "${_switch}"
    [ -z "${_id}" ] && return 0

    # try to remove our description
    # viid stays but it's not worth the extra hassle to remove that
    ifconfig ${_id} -descr -group vm-switch >/dev/null 2>&1
}

# add a new interface to this switch
# manual switches should be managed by the user
# using rc.conf, hence "manual"
#
# @param string _switch name of the switch
# @param string _if the interface to add
#
switch::manual::add_member(){
    util::err "manual switches and member interfaces should be configured using /etc/rc.conf"
}

# remove an interface
# manual switches should be managed by the user
# using rc.conf, hence "manual"
#
# @param string _switch name of the switch
# @param string _if the interface to remove
#
switch::manual::remove_member(){
    util::err "manual switches and member interfaces should be configured using /etc/rc.conf"
}

# set vlan id
#
# @param string _switch name of switch
# @param int _vlan vlan id to set
#
switch::manual::vlan(){
    util::err "manual switches and member interfaces should be configured using /etc/rc.conf"
}

# get id for a manual switch
# in this case we need to return the bridge name
#
# @param string _var variable to put id into
# @param string _switch switch name
# @return 0 if switch id found
#
switch::manual::id(){
    config::core::get "$1" "bridge_$2"
}
