#!/usr/bin/env bash

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
    local old_ver=$2
    local new_ver=$1
    echo ":: Upgrading linux-tachyon from $old_ver to $new_ver"
    echo ":: BBRv3 Configuration Check"

    # Recommended config for BBRv3
    local qdisc cong_algo
    qdisc=$(sysctl -n net.core.default_qdisc)
    cong_algo=$(sysctl -n net.ipv4.tcp_congestion_control)

    if [ "$qdisc" != "fq" ] || [ "$cong_algo" != "bbr" ]; then
        echo "It is recommended to reconfigure your TCP subsystem for BBR TCP algorithm:"
        echo "Create a file called: /etc/sysctl.d/99-bbr_tachyon.conf"
        echo "Add the following lines to it"
        echo "net.core.default_qdisc = fq"
        echo "net.ipv4.tcp_congestion_control = bbr"
    else
        echo "BBRv3 TCP algorithm is already configured."
    fi
}

# arg 1:  the new package version
post_install() {
    echo ":: Tachyon Linux provides the following recommended command line:"
    echo "   intel_iommu=igfx_off kvm-intel.nested=1 page_alloc.shuffle=1 lsm=landlock"
    
    # Call post_upgrade, passing the new version ($1) and a placeholder old version
    post_upgrade "$1" "none"
}
