#!/bin/sh

# Bring the /etc/modutils/* stuff to /etc/modules
update-modules

if [ -f /etc/init.d/run-checks ]; then
	for i in 0 1 6; do
		if [ ! -L /etc/rc$i.d/K*run-checks ]; then
			ln -sf ../init.d/run-checks /etc/rc$i.d/K20run-checks
		fi
	done
	for i in 2 3 4 5; do
		if [ ! -L /etc/rc$i.d/S*run-checks ]; then
			ln -sf ../init.d/run-checks /etc/rc$i.d/S20run-checks
		fi
	done
fi

# newer madwifi wants wireless-tools.27 which has some
# differences in the iwconfig output. This packet has
# a newer version of set-wlan-config.awk with additional
# bssid, but if an old version is left we patch that here.

if grep -q ]]\"GHz\" /usr/sbin/set-wlan-config.awk || ! grep -q "14 < channel\[iface\]" /usr/sbin/set-wlan-config.awk;then
  mv /tmp/set-wlan-config.awk /usr/sbin
fi

if ! grep -q "alias e=" /etc/profile 2>&-; then
	cat>>/etc/profile<<EOM

alias ..='cd ..'
alias ...='cd ../..'
alias cd..='cd ..'
alias cls=clear
alias del='rm -i'
alias dir='ls -l -d'
alias e=exit
alias edit=vi
alias ll='ls -l'
alias la='ls -la'
alias md='mkdir -p'
alias rd=rmdir
EOM
fi

echo "***"
echo "Note: Firewall is disabled in /etc/local.fw!"
echo "***"

test -f /etc/olsrd.conf || exit

HOSTNAME=$(cat /etc/hostname)
export HOSTNAME

awk '
/^#LoadPlugin +"olsrd_nameservice/,/#}/ {
	if (sub("^#", "")) {
		if ("}" == $0) {
			print "\tPlParam \"name\" \""ENVIRON["HOSTNAME"]"\""
			print "\tPlParam \"suffix\" \".olsr\""
		}
	} 
}
{
	print $0
}
' /etc/olsrd.conf > /tmp/olsrd.conf.$$

if ! cmp -s /etc/olsrd.conf /tmp/olsrd.conf.$$; then
	echo "Enabling nameservice with $HOSTNAME in /etc/olsrd.conf"
	cp -f /tmp/olsrd.conf.$$ /etc/olsrd.conf
fi
rm /tmp/olsrd.conf.$$

ln -sf /var/run/hosts_olsr /etc/hosts

if ! grep -q cron.dyngw /etc/cron/crontabs/root 2>&-; then
	echo "0-59/1 * * * *	/usr/sbin/cron.dyngw">>/etc/cron/crontabs/root
fi

if ! grep -q dyngw /etc/iproute2/rt_tables 2>&-; then
	echo "252	dyngw">>/etc/iproute2/rt_tables
fi

if [ ! -f ~/.ssh/id_dsa ]; then
	echo
	echo "Generating dsa privkey/pubkey for ssh. This process takes a minute."
	echo "Accept the standard file locations, then enter meaningful password twice."
	echo "Append ~/.ssh/id_dsa.pub to ~/.ssh/authorized_keys on other hosts as well."
	echo
	echo "Note: Keygen is very quiet. Press [X] and [backspace] keys to ensure shell"
	echo "is still alive. If you miss it: 'ssh-keygen -t dsa -p' to change password."
	echo 
	ssh-keygen -t dsa
fi
