#!/bin/sh
# FAI script for preparing LDAP objects. It calls ldap2fai to generate
# the config space after everything is done.
#
# (C) 2005 Cajus Pollmeier <pollmeier@gonicus.de>
echo 0 > /proc/sys/kernel/printk
trap '' INT
PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
LANG=C

. /usr/lib/goto/goto-support.lib

#dialog() {
#	echo $*
#}

abort() {
	setterm -cursor off
	while true; do sleep 60; done
}
	
# Try to figure out which interface is configured, in doubt
# choose the first one.
interfaces=$(ifconfig | awk '/^[a-z0-9]/ {print $1}' | grep -v "lo")
for int in $interfaces; do
	ip=$(v=`ifconfig $int | awk '/inet addr/ {print $2}'`; echo ${v##*:})
	mac=$(ifconfig $int | awk '/HWaddr/ {print $5}')
	[ -n "$ip" ] && break
done

# Cancel if there's no IP available
if [ -z "$ip" ]; then
	dialog --title 'Fehler' --no-shadow --infobox 'Fehler: Das System konnte keine Netzwerk-Adresse ermitteln.\n\nDie Installation kann ohne diese Adresse nicht fortgesetzt werden.' 5 60
	abort
fi

# Check if DNS setup is correct and set the hostname
hostname=$(get_hostname_from_ip $ip)
if [ "$hostname" == "unknown" ]; then
	dialog --title 'Fehler' --no-shadow --infobox 'Fehler: Das System konnte keinen Rechner-Namen ermitteln.\n\nDie Installation kann ohne diese Information nicht fortgesetzt werden.' 5 60
	abort
fi

echo "* setting hostname: $hostname"
hostname "$hostname"
mount -t tmpfs tmpfs /etc/ldap


# Look for interesting parameters on kernel commandline
ldap=""; splash=""
for v in $(cat /proc/cmdline); do
   case $v in
      ldap=*)
                echo -n "* found LDAP information, adapting configuration: "
                ldap=$(echo ${v##ldap=}|base64-decode)

		# ldap://hostname:389/basedn
		LDAP_HOST=$(echo $ldap|sed 's!^[^:][^:]*://\([^:/][^:/]*\).*$!\1!g')
		LDAP_PORT=$(echo $ldap|sed 's!^[^:]*://[^:][^:]*:\([0-9]*\)/.*$!\1!g')
		echo -n $ldap_port | grep -q '^[0-9]*$' || LDAP_PORT=389
		LDAP_BASE=$(echo $ldap|sed 's!^[^:][^:]*://[^/][^/]*/\(.*\)$!\1!g')
		echo -e "BASE   $LDAP_BASE\nURI     ldap://$LDAP_HOST:$LDAP_PORT\n" > /etc/ldap/ldap.conf
                echo "ok"
                ;;
      splash=*)
                echo -n "* setting splash mode: "
                splash=$(echo ${v##splash=})
                [ $splash == "silent" ] && echo "silent" || echo "normal"
                ;;
    esac
done

[ -z "$ldap" ] && exit 0

# Check if autosetup is needed at this point
echo -n "* configurator: "
if ! terminal_has_hardware_profile $mac; then
    setterm -cursor off
    echo "not configured yet - please wait, detecting hardware"

    # Switch from bootsplash to normal screen, show dialog
    [ -f /proc/splash ] && echo "verbose" > /proc/splash

    setterm -blank 60
    chvt 1
    dialog --infobox 'Bitte warten, die installierte Hardware wird untersucht...' 3 64

    # Get common config
    hwsetup
    terminal_alsa_setup
    terminal_autofs_setup

    # Save hardware profile
    terminal_save_hardware_profile $mac
fi

if ! terminal_activated $mac; then
    # wait till we get activated
    setterm -blank 60
    chvt 1
    dialog --infobox 'Warte auf Aktivierung durch den Systemadministrator.' 3 60

    while ! terminal_activated $mac; do
                sleep 2
    done

    # GOsa writes the GOto entry in three steps. To continue, we check
    # if XDRIVER is present.
    dialog --infobox 'System wurde aktiviert. Eintr�e werden nun bernommen.' 3 60
    while ! terminal_load_hardware_profile $mac &> /dev/null; do
	cat /etc/sysconfig/GOto | grep -v 'XDRIVER="unknown"' | grep -q 'XDRIVER'
	sleep 2
    done

    # Enable splash if it was enabled before
    [ -f /proc/splash ] && echo "silent" > /proc/splash

    echo -n "* configurator (pass2): "
    setterm -cursor on
fi

# Mount configuration space
[ ! -d /tmp/goto-fai ] && mkdir /tmp/goto-fai
mount -obind /tmp/goto-fai /fai
ldap2fai $mac

chvt 3
exit 0
