#!/bin/sh -e
#
# Report the current network status

success() {
    echo "success: $0: $*"
}
error() {
    echo "error: $0: $*"
}

PROFILE=
if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

if [ -x /sbin/ifconfig ] ; then
	/sbin/ifconfig -a | sed "s%^%info: $0: ifconfig: %" || true
else
	error "Unable to find /sbin/ifconfig"
fi

if [ -x /sbin/route ] ; then
	/sbin/route -n | sed "s%^%info: $0: route: %" || true
else
	error "Unable to find /sbin/route"
fi

if [ -x /usr/bin/nmap ] ; then
	/usr/bin/nmap localhost 2>&1 | sed "s%^%info: $0: nmap: %" || true
else
	error "Unable to find /sbin/nmap"
fi

# Only test if nfs-common is installed which is not the case on a
# standalone profile
if ! echo "$PROFILE" | grep -q Standalone ; then
	if [ -x /sbin/showmount ] ; then
		/sbin/showmount -e localhost 2>&1 | sed "s%^%info: $0: showmount: %" || true
	else
		error "Unable to find /sbin/showmount"
	fi
fi
	
if [ -f /etc/resolv.conf ] ; then
	cat /etc/resolv.conf | sed "s%^%info: $0: resolv.conf: %" || true
else
	error "Unable to find /etc/resolv.conf"
fi

if [ -f /etc/hosts ] ; then
	cat /etc/hosts | sed "s%^%info: $0: hosts: %" || true
else
	error "Unable to find /etc/hosts"
fi
if [ -x /usr/sbin/rpcinfo ] ; then
	/usr/sbin/rpcinfo -p localhost 2>&1 | sed "s%^%info: $0: rpcinfo: %" || true
else
	error "Unable to find /usr/sbin/rpcinfo"
fi

######################################################################
# Detect missing (or not detected) HW, and make sure we have the rest
# of the network available. [Should it move to a separate file?]
######################################################################

have_interface() {
    if /sbin/ifconfig "$1" > /dev/null 2>&1 ; then
        true
    else
        false
    fi
}

for value in `echo $PROFILE |sed 's/ /-/g' | sed 's/,-/ /g'`; do
  case $value in
      Workstation|Roaming-Workstation)
        networked=true
        workstation=true
        ;;
      Thin-Client-Server|LTSP-server)
        networked=true
        workstation=true
        ltspserver=true
        ;;
      Main-Server|Server)
        networked=true
        server=true
        ;;
      Standalone)
        networked=false
        standalone=true
        ;;
      Minimal)
        networked=true
        ;;
      *)
        error "unknown profile '$profile'"
        ;;
  esac
done

if test "$networked" = true && ! have_interface eth0 ; then
    error "Missing network interface eth0 needed by the main server, thin client server and workstation."
fi

if test "$ltspserver" = true && ! have_interface eth1 ; then
    error "Missing network interface eth1 needed by the thin client server."
fi

if test "$networked" = true -a "$server" != true ; then
    # should be able to reach tjener.intern at this point
    if ! ping -c1 tjener.intern > /dev/null 2>&1 ; then
        error "Unable to ping tjener.intern."
    fi
fi

# check if <URL: http://bugs.debian.org/705900 > is present.  First
# try IPv4.
if ping -c3 $(hostname) > /dev/null 2>&1 ; then
    success "ping $(hostname) work."
else
    error "ping $(hostname) fail, BTS #705900 present?"
fi
# Then try IPv6 if available
if getent ahosts $(hostname) | grep -q : ; then
    if ping6 -c3 $(hostname) > /dev/null 2>&1 ; then
	success "ping6 $(hostname) work."
    else
	error "ping6 $(hostname) fail, BTS #705900 present?"
    fi
else
    echo "info: not testing ping6, no IPv6 address found for $(hostname)."
fi

if false && test "$server" = true && have_interface eth0; then
    # Make sure we are the only main server on the net.  Should not be
    # able to reach another tjener.intern
    # Eh, how do we test this?  Check if we are the only DHCP server around?
    ifdown eth0
    if dhclient -d -e eth0 ; then
        :
    else
        # Oh, we got a reply from a DHCP server.  This is not supposed
        # to happen.
        error "Found another DHCP server on eth0!  Multiple main-server installs?"
    fi
fi

# Look for bug #765577, duplicate udev entries.
rules=/etc/udev/rules.d/70-persistent-net.rules
if [ -f $rules ] ; then
    if [ 1 -lt "$(sed -rn 's/^(SUBSYSTEM=="net", .*)NAME=.*$/\1/p' $rules | sort | uniq -c | sort -nr | awk '{ print $1; exit}')" ] ; then
	    error "duplicate persistent udev rules for network card in $rules (bug #765577)."
    else
	success "no duplicate persistent udev rule for network card found."
    fi
    else
	success "no network card related udev rules problem; file wasn't even created."
fi

exit 0
