#!/bin/bash

installDockerNetwork() 
{
  # Check if network setup is needed
  if [[ "$DOCKER_NETWORK_SETUP_NEEDED" == "true" ]]; then
    isHeader "Create a Docker Network"

    isNotice "Network $CFG_NETWORK_NAME not found, creating now"

    # Check if we need to generate a new subnet
    updateDockerNetworkSubnet

    # Create the Docker network command
    local network_create="docker network create \
--driver=bridge \
--subnet=$CFG_NETWORK_SUBNET \
--ip-range=${CFG_NETWORK_SUBNET%.*}.0/24 \
--gateway=${CFG_NETWORK_SUBNET%.*}.1 \
--opt com.docker.network.bridge.name=$CFG_NETWORK_NAME \
$CFG_NETWORK_NAME"
    
    # Run the network creation command
    local result=$(dockerCommandRun "$network_create")
    if [[ $? -eq 0 ]]; then
      checkSuccess "Docker network $CFG_NETWORK_NAME created successfully"
    else
      isError "Failed to create Docker network $CFG_NETWORK_NAME"
      isError " $result"
    fi
  else
    isNotice "Docker network $CFG_NETWORK_NAME already exists or setup not needed"
  fi
}
