#!/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
#######################################################################

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


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

SCRIPTS="gdm.conf-update.pl \
	gdm-kdm-purge-user.sh \
	import-gpg-keys \
	kdm-session-hack.sh \
	kdmrc-update.pl \
	syslog-update.pl \
	tftp-update.pl \
	no-volume-control-fix.sh \
	Xaccess-update.pl \
	xdm-config-update.pl \
	xinetd-sysconfig-update.pl \
	sysctl-update.sh \
	sawfish-update.sh \
        selinux-alter.sh \
        set-default-background.sh \
	runlevel-hack.sh \
	vncts-enable.sh \
	xfs-update.pl \
	kwin-update.sh \
	xscreensaver.sh \
	openoffice-a4-to-letter.sh \
	openoffice-ms-format-defaults.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 ltspswapd ltspfs-insecure iptables-k12ltsp"

for SERVICE in $SERVICES
do
	if [ -x /etc/init.d/$SERVICE ]
	then
		/sbin/chkconfig $SERVICE on
	else
		echo $SERVICE not found!
	fi
done
