#!/bin/bash

	set -x

	WGET="/usr/bin/wget"

# disable fsck on ext4 filesystems

	for part in $(mount |grep "type ext4"|perl -ane 'print "$F[0] "')
	do
		echo "Disable periodic fsck on $part"
		tune2fs -c 0 -i 0 $part
	done

# Set hostname (seems to be done by preseed; remove if so)

	#fcopy /etc/hostname ||
	#	echo ${HOSTNAME}.matrix.msu.edu > $target/etc/hostname

# Set timezone (seems to be done by preseed; remove if so)

	#echo $TIMEZONE > $target/etc/timezone
	#ln -fs /usr/share/zoneinfo/${TIMEZONE} $target/etc/localtime

# Set up host-local firewall (move to CF?)

	#chroot $target /usr/sbin/update-rc.d firewall start 37 S 0 6 .

# Install udev persistent net if naming rules

	rm -f /etc/udev/rules.d/70-persistent-net.rules
	$WGET http://preseed.matrix.msu.edu/70-persistent-net.rules \
		-O /etc/udev/rules.d/70-persistent-net.rules

	$WGET http://preseed.matrix.msu.edu/netinterfaces.sh &&
		bash netinterfaces.sh
	# FOR NOW... && rm netinterfaces.sh

# Set up resolv.conf

	cat << EOF > /etc/resolv.conf
search matrix.msu.edu h-net.msu.edu msu.edu
nameserver 35.8.0.2
nameserver 35.8.0.3
EOF

# Install ssh known-hosts and key bits so Rmrsh works

	$WGET http://preseed.matrix.msu.edu/ssh_known_hosts \
		-O /etc/ssh/ssh_known_hosts
	chown root:root /etc/ssh/ssh_known_hosts
	chmod 644 /etc/ssh/ssh_known_hosts

	mkdir -p /root/.ssh
	$WGET http://preseed.matrix.msu.edu/authorized_keys \
		-O /root/.ssh/authorized_keys
	chown root:root /root/.ssh/authorized_keys
	chmod 644 /root/.ssh/authorized_keys
	mkdir -p /root/Rmrsh
	ln -s /root/.ssh /root/Rmrsh/.ssh

	#Not needed with pull_ppkey
	#$WGET http://preseed.matrix.msu.edu/id_Rmrsh_rsa \
	#	-O /etc/ssh/id_Rmrsh_rsa
	#chown root:root /etc/ssh/id_Rmrsh_rsa
	#chmod 600 /etc/ssh/id_Rmrsh_rsa

# Try to allow root password login; current ssh config insists on key

	perl -pi.bak \
		-e 's/^PermitRootLogin without-password/PermitRootLogin yes/' \
		/etc/ssh/sshd_config

# Try to prevent #$%&*U() `dash' from being the default /bin/sh

	dpkg-reconfigure dash

# Set up rc.local

	$WGET http://preseed.matrix.msu.edu/rc.local \
		-O /etc/rc.local
	chmod 755 /etc/rc.local

# Prep things for cfengine

	$WGET -q -O /dev/null \
		http://preseed.matrix.msu.edu/cgi-bin/pull_ppkey.cgi

	mkdir -p /etc/scripts

	$WGET http://preseed.matrix.msu.edu/cf.dorun.sh \
		-O /etc/scripts/cf.dorun.sh
	chmod 755 /etc/scripts/cf.dorun.sh

	$WGET http://preseed.matrix.msu.edu/update.conf \
		-O /etc/cfengine/update.conf

	ln -s /etc/scripts/cf.dorun.sh /etc/scripts/cf.dobootrunreboot.sh

# Sort out #$%^&*()_ systemd /etc/mtab symlink change
# Will we _ever_ be done cleaning up after this fucktarded horror?

	rm /etc/mtab
	ln -s /proc/self/mounts /etc/mtab

# Finished

	date +%Y%m%d%H%M > /var/log/PRESEEDED

	sleep 60
	exit 0

# THINGS I DON'T THINK WE NEED ANYMORE

	# NOT SURE ABOUT THIS ONE, BUT WE CAN'T USE FCOPY FOR IT
	# a list of modules which are loaded at boot time

	#fcopy -i /etc/modules
	#for module in $MODULESLIST; do
	#    ainsl -a $target/etc/modules "^$module$"
	#done

# OK, I CHECKED OLD CONFIG HERE IS THE LIST OF FILES I STILL HAVEN'T 
# PUT IN CFENGINE OR RECONCILED OTHERWISE
#
# handled by preseed, I think /drb

	# default/ntp
	# default/ntpdate
	# ntp.conf

# SEEMS LIKE WE HAVE ALTERNATIVE WAYS TO DO WHAT MAY NEED DONE HERE
#
# handled by preseed, I think /drb

	## create keyboard layout table
	#$ROOTCMD bash -c "echo 'console-data console-data/keymap/full select $KEYMAP' | debconf-set-selections"
	#$ROOTCMD install-keymap $KEYMAP || true
	## dumpkeys | gzip -9f >$target/etc/console/boottime.kmap.gz
