#!/bin/sh

#######################################################################
# automagic configuration for the Linux Terminal Server Project
#######################################################################

# where to find the scripts:
BASE=/opt/ltsp/templates/k12linux

# the device that the terminals will be connecting to:
DEV="eth0"

# default ip, netmask, and network addresses:
DEFAULTIP="192.168.0.254"
DEFAULTMASK="255.255.255.0"

## end of user configuration


#######################################################################
# get the network settings, exit if the config files are not found
#######################################################################

if [ -f /etc/sysconfig/network-scripts/ifcfg-$DEV -a /etc/sysconfig/network ]
then
	# Source the network configuration
	. /etc/sysconfig/network-scripts/ifcfg-$DEV
	. /etc/sysconfig/network

	# if a variable is not known, set it to the default
	[ "$IPADDR" ]    || export IPADDR=$DEFAULTIP
	[ "$NETMASK" ]   || export NETMASK=$DEFAULTMASK
	[ "$GATEWAY" ]   || export GATEWAY=$DEFAULTIP
	[ "$BROADCAST" ] || export `/bin/ipcalc -b $IPADDR $NETMASK`
	[ "$NETWORK" ]   || export `/bin/ipcalc -n $IPADDR $NETMASK`
else
	echo
	echo "Skipping automagic configuration for LTSP, one of these files not found:"
	echo 
	echo	/etc/sysconfig/network-scripts/ifcfg-$DEV
	echo	/etc/sysconfig/network
	echo
fi


#######################################################################
# update the configs that are sensitive to the network settings
#######################################################################

$BASE/hosts-update.pl $IPADDR $NETWORK $NETMASK
$BASE/setup-update.sh $NETWORK $NETMASK
if [ "$IPADDR" != "$DEFAULTIP" ]
then
	echo running ltsp.conf-update.pl
	$BASE/ltsp.conf-update.pl $DEFAULTIP $IPADDR
	echo running dhcpd-update.pl
	$BASE/dhcpd-update.pl $IPADDR $NETWORK $NETMASK
fi


#######################################################################
# update various configuration files
#######################################################################

SCRIPTS="gdm.conf-update.pl \
	kdmrc-update.pl \
	syslog-update.pl \
	tftp-update.pl \
	Xaccess-update.pl \
	xdm-config-update.pl \
	xinetd-sysconfig-update.pl \
	sysctl-update.sh \
	sawfish-update.sh \
	runlevel-hack.sh \
	xfs-update.pl \
	screensaver-update.sh \
	openoffice-papersize-fix.sh \
	kwin-update.sh"

for SCRIPT in $SCRIPTS
do
	if [ -x $BASE/$SCRIPT ]
	then
		echo running $SCRIPT
		$BASE/$SCRIPT
	else
		echo WARNING: $SCRIPT not found
	fi
done


#######################################################################
# services to start on bootup
#######################################################################

SERVICES="dhcpd named nfs nfslock portmap xinetd nat"
for SERVICE in $SERVICES
do
	if [ -x /etc/init.d/$SERVICE ]
	then
		/sbin/chkconfig $SERVICE on
	else
		echo $SERVICE not found!
	fi
done
