# Creating Custom Netmodes in IOWRT

This guide provides developers with detailed instructions on how to create and manage custom network modes (netmodes) in IOWRT. The `netmode` script allows for flexible network configuration, and developers can define their own modes by structuring the necessary files and scripts within the `/etc/netmodes/` directory.

## Table of Contents
1. [Overview of Netmodes](#overview-of-netmodes)
2. [Directory Structure](#directory-structure)
3. [Creating a Custom Netmode](#creating-a-custom-netmode)
   - [Step 1: Pre-Execution Scripts](#step-1-pre-execution-scripts)
   - [Step 2: UCI Configuration Files](#step-2-uci-configuration-files)
   - [Step 3: Custom Execution Scripts](#step-3-custom-execution-scripts)
   - [Step 4: Post-Execution Scripts](#step-4-post-execution-scripts)
4. [Enabling and Switching Netmodes](#enabling-and-switching-netmodes)

## Overview of Netmodes

Netmodes in IOWRT provide a way to switch between different network configurations based on the needs of the environment. Developers can create custom netmodes by organizing scripts and configuration files in specific directories under `/etc/netmodes/<NETMODE_NAME>`.

## Directory Structure

A custom netmode is defined within the `/etc/netmodes/<NETMODE_NAME>` directory, which should contain the following subdirectories:

- **/lib/netmode/pre/**: Generic scripts executed before the netmode-specific configurations are applied.
- **/etc/netmodes/<NETMODE_NAME>/uci/**: Contains UCI configuration files that will be copied to `/etc/config/` during the application of the netmode.
- **/etc/netmodes/<NETMODE_NAME>/scripts/**: Custom scripts specific to the netmode that are executed after the UCI configurations are applied.
- **/lib/netmode/post/**: Generic scripts executed after the netmode-specific configurations are completed.

## Creating a Custom Netmode

To create a new netmode, follow these steps:

### Step 1: Pre-Execution Scripts

Scripts located in `/lib/netmode/pre/` are executed before any mode-specific actions. These are typically used for preparing the system or cleaning up configurations from the previous netmode.

- **Create Pre-Execution Scripts**:
  - Place your generic pre-execution scripts in `/lib/netmode/pre/`.
  - Example script (`/lib/netmode/pre/cleanup.sh`):
    ```bash
    #!/bin/sh
    echo "Cleaning up old network configurations..."
    # Add commands here
    ```

### Step 2: UCI Configuration Files

The UCI configuration files stored in `/etc/netmodes/<NETMODE_NAME>/uci/` will be copied to `/etc/config/`, effectively applying the desired network configuration.

- **Place UCI Config Files**:
  - Create UCI configuration files under `/etc/netmodes/<NETMODE_NAME>/uci/`.
  - Example (`/etc/netmodes/bridge/uci/network`):
````bash
config device 'br_lan'
        option name 'br-lan'
        option type 'bridge'
        option multicast_to_unicast '0'
        option bridge_empty '1'
        list ports 'eth1'
        list ports 'eth3'
        list ports 'eth4'

config interface 'lan'
        option proto 'dhcp'
        option device 'br-lan'
        option force_link '1'
        option reqopts '43 125'
````

### Step 3: Custom Execution Scripts

After the UCI files are applied, any scripts in `/etc/netmodes/<NETMODE_NAME>/scripts/` are executed. These can be used to perform additional configuration tasks that are specific to the netmode.

- **Create Custom Scripts**:
  - Add scripts to `/etc/netmodes/<NETMODE_NAME>/scripts/`.
  - Example (`/etc/netmodes/bridge/scripts/setup_bridge.sh`):
    ```bash
    #!/bin/sh
    echo "Setting up bridge mode..."
    # Additional configuration commands here
    ```

### Step 4: Post-Execution Scripts

Finally, the generic scripts in `/lib/netmode/post/` are executed. These scripts typically finalize the setup or perform any necessary cleanups.

- **Create Post-Execution Scripts**:
  - Place scripts in `/lib/netmode/post/`.
  - Example script (`/lib/netmode/post/restart_services.sh`):
    ```bash
    #!/bin/sh
    echo "Restarting network services..."
    # Add commands here
    ```

## Enabling and Switching Netmodes

The netmode mechanism can be enabled or disabled via the UCI configuration, and you can switch between netmodes using UCI commands.

- **Enable Netmode**:
  ```bash
  uci set netmode.global.enabled=1
  uci commit netmode
    ```
    
- **Switch Netmode**:
  ```bash
  uci set netmode.global.mode='<NETMODE_NAME>'
  uci commit netmode
    ```
