#!/bin/bash
# Configures a full development system for JFFs2
# Run with DEMO=1 to configre the demo that we ship.

warn () {
	echo "Warning: $1" >&2
}

quiet=
if [ "x$1" = "x-q" ]; then
	quiet=1
	shift 1
fi

mode="$1"

case "$mode" in
on|enable|"")

	if [ "$DEMO" ]; then
		# Switch to demo xinitrc
		save_files jffs2-configure /etc/init.d/xads.sh
		sed -i -e 's/xinitrc-ads$/xinitrc-ads2/' /etc/init.d/xads.sh
	fi

	for i in ssh hostapd inetd gpsd pcmcia ;
	do
		save_files jffs2-configure /etc/rc*.d/*${i}
		update-rc.d -f ${i} remove
	done
	
	# Don't start ssh server by default
	save_files jffs2-configure /etc/rc?.d/S*ssh
	rm -f /etc/rc?.d/S*ssh
	
	# Generate unique host key for this image.
	save_files jffs2-configure /etc/ssh/ssh_host_*_key*
	rm -f /etc/ssh/ssh_host_*_key*
	echo "Creating ssh keys, this will take some time."
	save_files jffs2-configure /etc/ssh/ssh_host_rsa_key
	ssh-keygen -q -f /etc/ssh/ssh_host_rsa_key -N "" -t rsa
	save_files jffs2-configure /etc/ssh/ssh_host_dsa_key
	ssh-keygen -q -f /etc/ssh/ssh_host_dsa_key -N "" -t dsa
	echo "Done creating ssh keys."

	# Or dbus.
	save_files jffs2-configure /etc/rc?.d/S*dbus-1
	rm -f /etc/rc?.d/S*dbus-1

	# Or screen cleanup.
	save_files jffs2-configure /etc/rcS.d/S70screen-cleanup
	rm -f /etc/rcS.d/S70screen-cleanup
	
	if [ -f /usr/bin/w ]; then
		save_files jffs2-configure /usr/bin/w
		mv /usr/bin/w /usr/bin/w.orig
		{
		echo '#!/bin/sh'
		echo 'uptime'
		echo 'who'
		} > /usr/bin/w
		chmod a+x /usr/bin/w
	fi

	# Won't be reversed..
	mkdir -p /mnt
	mkdir -p /mnt/sda
	mkdir -p /mnt/hda
	mkdir -p /mnt/nfs
	mkdir -p /mnt/mmc
	mkdir -p /mnt/flash


	save_files jffs2-configure /etc/init.d/bootmisc.sh
	{
	echo 'g/chmod -f/s/-f //'
	echo 'g/chown -f/s/-f //'
	echo 'x'
	} | ex - /etc/init.d/bootmisc.sh || warn "Didn't munge /etc/init.d/bootmisc.sh"

	#save_files jffs2-configure /etc/default/rcS
	#{
	#echo '/EDITMOTD=/s/yes/no/'
	#echo 'x'
	#} | ex - /etc/default/rcS || warn "Didn't munge /etc/default/rcS"

	save_files jffs2-configure /etc/motd
	sed -i 's/^Linux.*/ADS Debian derived image/' /etc/motd || warn "Didn't munge /etc/motd"

	if [ "$(dpkg --print-architecture)" = arm ]; then
		# Run cardmgr from inittab instead of /etc/rc
		# (recall pcmcia was turned off above)
		# (Armel doesn't use pcmcia-cs.)
		save_files jffs2-configure /etc/inittab
		{
		echo '/^#cm:23/s/^#//'
		echo 'x'
		} | ex - /etc/inittab || warn "Didn't munge /etc/inittab"
	fi

	#save_files jffs2-configure /etc/init.d/procps.sh
	#{
	#echo '/sysctl $quiet/s/$quiet//'
	#echo 'x'
	#} | ex - /etc/init.d/procps.sh || warn "Didn't munge /etc/init.d/procps.sh"

	# Won't be reversed.
	ln -sf /proc/mounts /etc/mtab

	save_files jffs2-configure /usr/share/xawtv/Index.map
	cat > /usr/share/xawtv/Index.map <<EOF
[us-bcast]
file = ntsc-bcast.list
EOF

	save_files jffs2-configure /etc/X11/xawtvrc
	cat > /etc/X11/xawtvrc <<EOF
[defaults]
group = main
norm = NTSC
capture = grab

[Camera]
capture = grab
EOF

	save_files jffs2-configure /etc/X11/app-defaults/Xawtv
	cat << EOF | patch /etc/X11/app-defaults/Xawtv || warn "Didn't munge /etc/X11/app-defaults/Xawtv"
385,391c
xawtv.onscreen.label.font:              6x13
.
18,25d
7,14d
EOF
	cat >> /etc/X11/app-defaults/Xawtv << EOF

Xawtv*geometry:         +200+150                 
EOF

	save_files jffs2-configure /usr/bin/xterm
	ln -sf /usr/bin/rxvt-xterm /usr/bin/xterm

	# Use busybox utilities instead of the Debian ones.
	busybox-links enable

	# Update fontconfig cache to avoid hitting all the fonts during exersise
	fc-cache

	if [ ! "$quiet" ]; then
		echo "System is configured for use on flash with jff2s."
		echo "Use 'adsrootbuilder -c ads-jffs2.conf' to create an image."
		echo "Use 'jffs2-configure off' to restore system to original configuration."
	fi
;;
off|disable)
	busybox-links disable
	restore_files jffs2-configure
	if [ ! "$quiet" ]; then
		echo "System restored from jffs2 configuration to original configuration."
	fi
;;
esac
