#!/bin/sh
#
# $Id$
#
# Copyright 2019-2020, Juniper Networks, Inc
# All Rights Reserved
#

VERIEXEC=/sbin/veriexec

if ! type is_verified > /dev/null 2>&1; then
    # we'll override this below if appropriate
    is_verified() { return 0; }

    if test -s $VERIEXEC; then
        if $VERIEXEC -i active > /dev/null 2>&1; then
            is_verified() { $VERIEXEC -x $1; }
        fi
    fi
fi

# avoid multiple inclusion and duplicate effort
_VDOT_SH=:

# in case we want to avoid repeats
dotted=
# try not to include things we cannot verify
vdot() {
    local f rc=0

    for f in "$@"
    do
        test -s $f || continue
        if is_verified $f 2> /dev/null; then
            dotted="$dotted $f"
            . $f
        else
            rc=80               # EAUTH
        fi
    done
    return $rc
}

vdot_once() {
    local f rc=0

    for f in "$@"
    do
        case " $dotted " in
        *" $f "*) continue;;
        esac
        vdot $f || rc=$?
    done
    return $rc
}

# for unverified things
dot() {
    local f

    for f in "$@"
    do
        if test -s $f; then
            dotted="$dotted $f"
            . $f
        fi
    done
}

dot_once() {
    local f

    for f in "$@"
    do
        case " $dotted " in
        *" $f "*) continue;;
        esac
        dot $f
    done
}

# unit test
case "/$0" in
*/vdot*) vdot "$@";;
esac
#!/bin/sh

# Copyright (c) 2022, Juniper Networks, Inc.
# All rights reserved

# facilitate testing
PKGTOOLS=${PKGTOOLS:-/usr/libexec}

vdot $PKGTOOLS/pkg_funcs.sh

is_kernel_newer() {
    get_xmltags $PKGDIR/package.xml compat-osreldate
    kernel_osreldate=$(sysctl -n kern.osreldate)
    test $kernel_osreldate -ge ${compat_osreldate:-0}
}

self_deactivate() {
    echo "Removing ${PKGDIR##*/} from $PKGSET: no longer required"
    rm -f $PKGSETS/$PKGSET/$PKG_LINKNAME
}

rc=0

case "$PKGSET" in
active)
    case "$1" in
    activate)
        case "$PKG_COMMAND" in
        setop|start)
            if is_kernel_newer; then
                rc=1; self_deactivate
            fi
            ;;
        esac
        ;;
    mounted)
        x=`sysctl -qn jnpr.compat.osreldate`
        if [ -n "$x" ]; then
            get_xmltags $PKGDIR/package.xml compat-osreldate
            if [ $x -ge $compat_osreldate ]; then
                exit 0
            fi
            kldunload -n fwdcompat
        fi
        kldload /modules/fwdcompat.ko
        ;;
    deactivate|delete)
        kldstat -n fwdcompat >/dev/null 2>&1 && kldunload -n fwdcompat
        ;;
    esac
    ;;
esac
exit $rc
