#! /bin/sh
#
# Copyright (C) 2016 Huawei Inc.
#
### BEGIN INIT INFO
# Provides:          dev_connlimit
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: net device connection contrack limit module
### END INIT INFO

SYSTEMCTL_SKIP_REDIRECT=1

kver=$(uname -r)
test -f /lib/modules/${kver}/updates/dev_connlimit.ko || exit 0

. /etc/init.d/functions

check_module() {
    lsmod | grep -w dev_connlimit > /dev/null
    return $?
}

insert_mod_if_required () {
    check_module || insmod /lib/modules/${kver}/updates/dev_connlimit.ko
    return $?
}

remove_mod() {
    check_module || return 0
    rmmod dev_connlimit
    return $?
}

show_status() {
    check_module
    if [ $? -eq 0 ];then
        echo "dev_connlimit is loaded now."
    else
        echo "dev_connlimit is not loaded now."
    fi    
}

case $1 in
    start)
	action $"insmod dev_connlimit" insert_mod_if_required
	show_status
	;;
    stop)
	action $"rmmod dev_connlimit " remove_mod
	show_status
        ;;
    status)
	action $"checking status of dev_connlimit " check_module
        exit $?
        ;;
    *)
        echo "Usage: $0 {start|stop|status}" >&2
        exit 1
        ;;
esac

exit 0
