Patches Directory
=================

This directory contains patches and scripts for modifying Net-SNMP source code to support ezsnmp functionality.

Overview
--------

The patches in this directory allow ezsnmp to work with different versions of Net-SNMP (5.6 through 5.9). The process involves fetching the Net-SNMP source code, applying necessary modifications, and generating version-specific patches.

Order of Operations
-------------------

Follow these steps in order to prepare and apply patches for your desired Net-SNMP version:

1. **Fetch Net-SNMP Versions**

   Run the ``fetch_net_snmp_versions.sh`` script to download the source code for Net-SNMP versions 5.6 through 5.9:

   .. code-block:: bash

      ./fetch_net_snmp_versions.sh

   This script will populate the ``net-snmp-X.Y/`` directories with the respective source code.

2. **Tweak Master Source Code**

   Modify the code in the ``master_src/`` directory as needed. This directory contains the base source code that will be used to generate patches for different Net-SNMP versions.

3. **Generate Patches**

   Run the ``make_patches.sh`` script to create patches for a specific Net-SNMP version:

   .. code-block:: bash

      ./make_patches.sh <version>

   Replace ``<version>`` with the desired Net-SNMP version (e.g., 5.8, 5.9).

4. **Apply Patches**

   Use the ``apply_patches.sh`` script to apply the generated patches to the Net-SNMP source code:

   .. code-block:: bash

      ./apply_patches.sh <version>

   This will apply the patches in the ``net-snmp-<version>-patches/`` directory to the corresponding ``net-snmp-<version>/`` source code.

Version-Specific Notes
----------------------

**Net-SNMP 5.8 and Lower**

For Net-SNMP versions 5.8 and lower, you must use ``netsnmp_cleanup_session(&session);`` instead of the newer cleanup functions, as it does not exist in these versions.

Automated Script
----------------

The ``do_all_that_stuff.sh`` script automates the entire process of downloading Net-SNMP versions, generating patches with version-specific modifications, applying patches, and formatting the code. It handles special cases for compatibility, such as removing ``netsnmp_cleanup_session`` calls for versions 5.6-5.8 and using ``gettimeofday`` instead of ``netsnmp_get_monotonic_clock`` for version 5.6.

To run the automated process:

.. code-block:: bash

   ./do_all_that_stuff.sh

This script performs the following steps in order:

1. Downloads Net-SNMP versions 5.6-5.9 in parallel (with ``--no-remove`` to avoid re-downloading if directories exist).
2. Generates patches for version 5.9 first using the unmodified ``master_src``.
3. Backs up ``master_src`` and removes ``netsnmp_cleanup_session(&session);`` calls (not available in 5.6-5.8).
4. Modifies timing calls in ``master_src`` from ``netsnmp_get_monotonic_clock`` to ``gettimeofday`` for 5.6 compatibility.
5. Generates patches for version 5.6 with the modified ``master_src``.
6. Reverts timing calls back to ``netsnmp_get_monotonic_clock`` for versions 5.7-5.8.
7. Generates patches for versions 5.7-5.8 in parallel.
8. Restores ``master_src`` from backup.
9. Applies patches to versions 5.6-5.9 in parallel, producing final patched source in ``../src/net-snmp-<version>-final-patched/``.
10. Runs clang-format on the entire repository to ensure consistent code formatting.

This script is designed for efficiency and reliability, running tasks in parallel where possible to speed up the process. It ensures that each Net-SNMP version receives the appropriate modifications without manual intervention.

.. note::
   If you are building from source and applying patches, you may need to install `dos2unix` on your system. On Ubuntu/Debian, run `sudo apt install dos2unix`. On macOS, use `brew install dos2unix`.

This content was generated by AI and reviewed by humans. Mistakes may still occur. PRs for corrections are welcome.