#!/bin/sh
#
# Override default values based on the profile in
# /etc/debian-edu/config.
#
# Check that the system have the network configuration required for
# the given profile.

set -e

# debug=true

pkglibdir=/usr/lib/debian-edu-install

. $pkglibdir/debian-edu-common

if test "$debug" = "true"; then
    defaults=defaults
else
    defaults=$pkglibdir/defaults
fi 

load_config

#set -x

load_defaults() {
    filename="$defaults.$1"
    if test -r $filename; then
	print "info: Loading defaults from $filename"
	if /usr/sbin/debconf-load-defaults $filename ; then
	    :
	else
	    error "unable to load defaults from $filename"
	fi
    else
	error "unable to read defaults from $filename"
    fi
}

load_defaults common

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

# Make sure the default values have priority
#  1 main-server
#  2 thin-client-server
#  3 workstation
#  4 networked (Common for non-standalone)
#  5 standalone 
#  6 barebone 

if test "$standalone" = true ; then
	load_defaults standalone
fi
if test "$networked" = true ; then
	load_defaults networked
fi
if test "$workstation" = true ; then
	load_defaults workstation
fi
if test "$ltspserver" = true ; then
	load_defaults thin-client-server
fi
if test "$server" = true ; then
	load_defaults main-server
fi


# Make sure some hostname is defined.
currname=`hostname`
if [ -z "$currname" ] ; then
	hostname localhost
fi

# Set /etc/mailname if it is missing
if [ ! -f /etc/mailname ] ; then
	echo "postoffice.intern" > /etc/mailname
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
}

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 poing
    if ! ping -c1 tjener.intern > /dev/null 2>&1 ; then
	error "Unable to contact tjener.intern."
    fi
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

exit 0
