#!/bin/sh

# Configures a full development system (or a system preconfigured using
# jffs2-configure is also ok) to be as close as possible to the standard
# cramfs demo that we've been shipping.

# Note that the relative links used below are important, they allow
# modification of copies of the tree by things like the adsrootbuilder.

mode="$1"

case "$mode" in
on|enable|"")
	echo "Configuring for cramfs"
	jffs2-configure -q

	cd /

	# dev tarball
	tar -C dev -zcf .dev.tar.gz .

	# move static parts of /etc to /usr
	# XXX not currently reversed
	mkdir -p /usr/etc
	if [ ! -d /usr/etc/pcmcia ]; then
		mv /etc/pcmcia /usr/etc
	fi
	if [ ! -d /usr/etc/init.d ]; then
		mv /etc/init.d /usr/etc
	fi
	ln -sf ../usr/etc/pcmcia /etc/pcmcia
	ln -sf ../usr/etc/init.d /etc/init.d

	# enable cramfs scripts
	ln -sf ../init.d/altshadow.sh /etc/rcS.d/S37altshadow.sh
	ln -sf ../init.d/mountetc.sh /etc/rcS.d/S12mountetc.sh

	# make modules directory link to a writable place
	# XXX not currently reversed
	rm -rf lib/modules
	mkdir -p /etc/lib/modules
	ln -sf ../etc/lib/modules /lib/modules

	# XXX?
	save_files cramfs-configure /etc/rc?.d/*makedev
	rm -f /etc/rc?.d/*makedev || true
	save_files cramfs-configure /etc/rc?.d/S*sysklogd
	rm -f /etc/rc?.d/S*sysklogd || true
	save_files cramfs-configure /etc/rc?.d/S*klogd
	rm -f /etc/rc?.d/S*klogd || true
	save_files cramfs-configure /etc/rc?.d/*savevar.sh
	rm -f /etc/rc?.d/*savevar.sh || true
	save_files cramfs-configure /etc/init.d/savevar.sh
	rm -f /etc/init.d/savevar.sh || true

	# XXX not currently reversed
	if [ -d /root ] && [ ! -L /root ]; then
		mv /root /etc/root
	fi
	ln -sf etc/root /root

	# XXX?
	# XXX not currently reversed
	ln -sf ../etc/log dev/log

	# Set up /alt, which will populate the jffs2 flashfs2 image and be
	# shadowed over top of the cramfs image by altshadow.
	# XXX not currently reversed
	mkdir -p /alt/etc/X11/xcalibrate
	touch /alt/etc/X11/xcalibrate/xcalibrate.conf
	cp -a /etc/passwd /alt/etc
	cp -a /etc/shadow /alt/etc
	cp -a /etc/group /alt/etc

	echo "System is configured for use on flash with cramfs."
	echo "Use 'adsrootbuilder -c ads-cramfs.conf' to create images."
	echo "Use 'cramfs-configure off' to restore system to original configuration."
;;
off|disable)
        jffs2-configure -q off
	restore_files cramfs-configure
	rm -f /.dev.tar.gz /etc/rcS.d/S37altshadow.sh /etc/rcS.d/S12mountetc.sh
;;
esac
