#!/bin/sh

. /etc/utils/shell-utils.sh

DEBUG=0

exit 0

case "$1" in
  start)
    echo -n "Starting netconsole..."
    log "Starting netconsole..."

    NC_IP=""
    NC_PORT=""
    if [ -f "/ram/dhcp_ready" ]; then
        NC_IP=`cat /ram/dhcp_ready | grep nc_ip=`
        NC_IP=${NC_IP%%#*}
        NC_IP=${NC_IP#nc_ip=}
        NC_PORT=`cat /ram/dhcp_ready | grep nc_port=`
        NC_PORT=${NC_PORT%%#*}
        NC_PORT=${NC_PORT#nc_port=}
    fi

    if [ "$NC_IP" == "" ]; then
        NC_IP=`fw_printenv nc_ip 2>/dev/null`
        NC_IP=${NC_IP#nc_ip=}
        NC_PORT=`fw_printenv nc_port 2>/dev/null`
        NC_PORT=${NC_PORT#nc_port=}
    fi

    if [ "$NC_IP" == "" ]; then
        #Disable netconsole.
        if [ -f "/sys/kernel/config/netconsole/target1/enabled" ]; then
            echo 0 > /sys/kernel/config/netconsole/target1/enabled
        fi
        exit 0
    fi

    if [ "$NC_PORT" == "" ]; then
        NC_PORT=6666
    fi 

    mkdir /sys/kernel/config/netconsole/target1  > /dev/null 2>&1
    if [ ! -f "/sys/kernel/config/netconsole/target1/enabled" ]; then
        echo "Netconsole support disabled."
        log "Netconsole support disabled."
        exit 1
    fi
    echo 0 > /sys/kernel/config/netconsole/target1/enabled
    echo $NC_IP > /sys/kernel/config/netconsole/target1/remote_ip
    echo $NC_PORT > /sys/kernel/config/netconsole/target1/remote_port
    remote_mac=`arping $NC_IP -f | grep -o '\w*:\w*:\w*:\w*:\w*:\w*'`
    echo $remote_mac > /sys/kernel/config/netconsole/target1/remote_mac
    echo 1 > /sys/kernel/config/netconsole/target1/enabled


    #echo "Done."
    log "Done."
  ;;

  restart)
    echo -n "Restarting NETCONSOLE..."
    log "Restarting NETCONSOLE..."
    /etc/init.d/rc.netconsole stop
    sleep 1
    /etc/init.d/rc.netconsole start
    echo "Done."
    log "Done."
  ;;

  stop)
    echo -n "Stopping NETCONSOLE..."
    log "Stopping NETCONSOLE..."

    if [ -f "/sys/kernel/config/netconsole/target1/enabled" ]; then
        echo 0 > /sys/kernel/config/netconsole/target1/enabled
    fi

    echo "Done."
    log "Done."
  ;;

  *)
    echo "Usage: /etc/init.d/rc.netconsole {start|stop|restart}"
    log "Usage: /etc/init.d/rc.netconsole {start|stop|restart}"
    exit 1
    ;;
esac

exit 0









