#!/bin/sh

DEST=$PWD/voyage-install

KERNEL_VER=2.6.8-486-voyage

KERNEL_IMAGE=kernel-image-"$KERNEL_VER"

EXCLUDE=""

INCLUDE=""

REMOVE_PKGS=""

EXTRA_PKGS=""

MIRROR="file:///cdrom/debian"
#MIRROR="http://www.zentek-international.com/mirrors/debian"
#MIRROR="http://www.hk.debian.org/debian"

DIST="sarge"

if [ -d $DEST ] ; then
	rm -rf $DEST
fi

export PATH=$PATH:/usr/sbin
debootstrap --exclude=$EXCLUDE --include=$INCLUDE $DIST $DEST $MIRROR

if [ ! -d $DEST ] ; then
	exit
fi

# Running post-installing scripts
echo "Running post-installing scripts ..."

###################################################################################
#### /etc/apt/source.list
####
gen_sourceslist()
{
cat <<EOF > $DEST/etc/apt/sources.list
deb ftp://ftp.us.debian.org/debian/ sarge main
deb http://security.debian.org/ sarge/updates main
deb http://www.voyage.hk/dists/0.2 ./
EOF

chroot $DEST apt-get update
}


#################################
# generate scripts
#################################
gen_scripts()
{
	export DEST
	./gen-remount.sh
}

#################################
#Remove extra package
#################################
remove_pkg()
{
	echo -n "Removing extra packages ... "
	REMOVE_PKGS=`echo $REMOVE_PKGS | sed "s/,/ /g"`
	for PKG in $REMOVE_PKGS
	do
		chroot $DEST apt-get -q -y remove $PKG
	done
	echo "Done"
}

#################################
# Setting hostname
#################################
set_hostname()
{
	HOST_NAME="voyage"
	echo "Setting hostname to voyage ..."
	echo "$HOST_NAME" > $DEST/etc/hostname
	#chroot $DEST hostname "$HOST_NAME"
}

#################################
# Setting Timezone
#################################
set_timezone()
{
	echo -n "Setting Timezone to GMT ... "
	
	TZ=GMT
	echo $TZ > $DEST/etc/timezone
	rm -f $DEST/etc/localtime
	ln -sf /usr/share/zoneinfo/$TZ $DEST/etc/localtime
	
	echo "Done"
}

#################################
# Copying scripts
#################################
copy_script()
{
	echo -n "Copying scripts ... "
	cp voyage.update $DEST/
	chmod +x $DEST/voyage.update
	
	#chroot $DEST wget -q http://www.voyage.hk/download/voyage/voyage.update
	#chroot $DEST chmod +x /voyage.update
	
	cp lilo.conf.voyage $DEST/etc/
	
	#chroot $DEST wget -q http://www.voyage.hk/download/voyage/lilo.conf.voyage
	#chroot $DEST mv lilo.conf.voyage etc/
	
	cp nat.sh $DEST/usr/local/sbin
	chmod +x $DEST/usr/local/sbin
	
	cp voyage-util $DEST/etc/init.d/
	chmod +x $DEST/etc/init.d/voyage-util 
	
	echo "Done"
}

#################################
# Installing Kernel
#################################
install_kernel()
{
	if expr $KERNEL_VER : 2.6 >/dev/null ; then 
		chroot $DEST apt-get -q=2 -y install module-init-tools
	fi
	chroot $DEST apt-get -q=2 -y install $KERNEL_IMAGE
	
	chroot $DEST depmod -a $KERNEL_VER -F /boot/System.map-$KERNEL_VER
}

#################################
# Install Extra package
#################################
install_extra()
{
	echo -n "Installing extra packages ... "
	EXTRA_PKGS=`echo $EXTRA_PKGS | sed "s/,/ /g"`
	for PKG in $EXTRA_PKGS
	do
		chroot $DEST apt-get -q=2 -y install $PKG
	done
	#chroot $DEST apt-get -q=2 -y install $EXTRA_PKGS
	echo "Done"
}

#################################
# Downloading Firmware from Prism54.org
#################################
install_prismfw()
{
	PRISM54_FIRMWARE_URL="http://prism54.org/~mcgrof/firmware/1.0.4.3.arm"
	#PRISM54_FIRMWARE_URL="http://ruslug.rutgers.edu/~mcgrof/802.11g/firmware/1.0.4.3.arm"
	echo -n "Downloading Firmware from Prism54.org ... "
	chroot $DEST wget "$PRISM54_FIRMWARE_URL" \
		-O /usr/lib/hotplug/firmware/isl3890
	echo "Done"
}

#################################
# Installing lilo
#################################
install_lilo()
{
	chroot $DEST apt-get -q=2 -y install lilo
}

#################################
# Installing voyage-util
#################################
install_voyage_util()
{
	chroot $DEST update-rc.d -f rmnologin remove 
	chroot $DEST update-rc.d voyage-util defaults 99
}

###################################################################################
#### Generate voyage.dpkg-l, voyage.dpkg.list, voyage.depends.list
####
gen_dpkglist()
{
	chroot $DEST dpkg -l > $DEST/voyage.dpkg-l
	cat $DEST/var/lib/dpkg/available | grep "^Package" | cut -f 2 -d " " | sort > $DEST/voyage.dpkg.list
	
	rm -f $DEST/voyage.depends.list
	for DPKG in $( find $DEST/var/cache/apt/archives/ -name "*.deb");	 
	do 
		dpkg -I $DPKG | egrep "^ Package|^ Depends"  | sed "s/^ Package : //g" >> $DEST/voyage.depends.list
	done
}

#################################
# Removing Locale
#################################
remove_locale()
{
	echo -n "Removing Locale in /usr/share/locale ... "
	find $DEST/usr/share/locale -name "*.mo" -exec rm '{}' ';'
	echo "Done"
}


#################################
# Removing Timezone
#################################
remove_timezone()
{
	echo -n "Removing Timezone in /usr/share/zoneinfo ... "
	rm -rf $DEST/usr/share/zoneinfo/posix
	rm -rf $DEST/usr/share/zoneinfo/right
	echo "Done"
}


#################################
# Removing Extra
#################################
remove_extra()
{
	echo -n "Zeoring available Package lists ... "
	chroot $DEST apt-get clean 
	for i in $(find $DEST/var/lib/apt/lists -type f \( -name \*Packages -o -name \*Sources \) 2>/dev/null); do  :>"$i"; done
	chroot $DEST dpkg --clear-avail
	rm -f $DEST/var/cache/apt/*.bin
	chroot $DEST apt-cache gencaches > /dev/null
	chroot $DEST remove.docs
	echo "Done"
}
		
#############################################################################

#################################
# Installing /ro and /rw directories
#################################
setup_rw()
{
	echo -n "Installing /ro and /rw directories ... "
	chroot $DEST mkdir /rw /ro ro/dev ro/etc ro/usr ro/var
	mv $DEST/root $DEST/ro/
	chroot $DEST ln -sf /rw/root /root
	mv $DEST/tmp $DEST/ro/
	chroot $DEST ln -sf /rw/tmp /tmp
	echo "Done"
	
	#################################
	# Setting up devices
	#################################
	echo -n "Setting up devices in /rw and /ro ... "
	DEVICES=`chroot $DEST ls /dev/ttyp*`
	DEVICES="$DEVICES `chroot $DEST ls /dev/ttyS*`"
	DEVICES="$DEVICES `chroot $DEST ls /dev/tty[0-9]`"
	DEVICES="$DEVICES `chroot $DEST ls /dev/tty1[0-2]`"
	DEVICES="$DEVICES `chroot $DEST ls /dev/tty`"
	DEVICES="$DEVICES `chroot $DEST ls /dev/pty*`"
	DEVICES="$DEVICES `chroot $DEST ls /dev/xconsole`"
	
	for DEVICE in $DEVICES
	do
	        if [ -c $DEVICE ]  ; then
	                chroot $DEST mv $DEVICE /ro/dev/
	                chroot $DEST ln -sf /rw$DEVICE $DEVICE
	        fi
	done
	                chroot $DEST mv /dev/xconsole /ro/dev/
	                chroot $DEST ln -sf /rw/dev/xconsole /dev/xconsole
	                chroot $DEST ln -sf /rw/dev/log  /dev/log
	echo "Done"
	
	#################################
	# Remapping files to /rw
	#################################
	echo -n "Remapping files to /rw ... "
	FILES="/etc/adjtime /etc/hostname /etc/issue /etc/issue.net /etc/resolv.conf"
	FILES=$FILES" /var/lock /var/log /var/mail /var/run /var/spool /var/tmp"
	
	for FILE in $FILES
	do
		chroot $DEST mv $FILE /ro/`dirname $FILE`
		chroot $DEST ln -sf /rw/$FILE $FILE
	done
	echo "Done"	
	
	#################################
	# Modifying /etc/network/ifstate
	#################################
	echo -n "Modifying /etc/network/run/ifstate ... "
	#chroot $DEST ln -sf /rw/etc/network/ifstate /etc/network/ifstate
	#mkdir -p $DEST/ro/etc/network/
	#touch $DEST/ro/etc/network/ifstate

	chroot $DEST ln -sf /rw/etc/network/run/ifstate /etc/network/run/ifstate
	mkdir -p $DEST/ro/etc/network/run
	touch $DEST/ro/etc/network/run/ifstate
	echo "Done"
} 

setup_etc()
{
	#################################
	# Modifying /etc/init.d/syslogd
	#################################
	echo -n "Modifying /etc/init.d/syslogd ... "
	cat $DEST/etc/init.d/sysklogd | \
	sed -e 's/^SYSLOGD=\"\"/SYSLOGD\=\"\-p \/rw\/dev\/log \-m 0\"/' \
	>  $DEST/etc/init.d/sysklogd.tmp
	mv  $DEST/etc/init.d/sysklogd.tmp  $DEST/etc/init.d/sysklogd
	chmod +x $DEST/etc/init.d/sysklogd
	echo "Done"
	
	#################################
	# Modifying /etc/mtab
	#################################
	echo -n "Modifying /etc/mtab ... "
	chroot $DEST rm -f /etc/mtab
	chroot $DEST ln -s /proc/mounts /etc/mtab
	echo "Done"
	
	#################################
	# Modifying /etc/network/options
	#################################
	echo -n "Modifying /etc/network/options ... "
	cat $DEST/etc/network/options | sed -e "s/ip_forward=no/ip_forward=yes/" > $DEST/etc/network/options.tmp
	mv $DEST/etc/network/options.tmp $DEST/etc/network/options
	echo "Done"
	
	#################################
	# Adding configuration for watchdog
	#################################
	echo -n "Adding configuration for watchdog ... "
	WATCHDOG_CONF=$DEST/etc/watchdog.conf
	if [ -f $WATCHDOG_CONF ] ; then
		echo "watchdog-device = /dev/wd" >> $WATCHDOG_CONF
		echo "interval	    = 15" >> $WATCHDOG_CONF
		chroot $DEST mknod -m 600 /dev/wd c 10 130
	fi;
	echo "Done"
	
	#################################
	# Modifying /etc/init.d/mountall.sh
	#################################
	echo -n "Modifying /etc/init.d/mountall.sh ... "
	echo "cp -a /ro/* /rw/" >> $DEST/etc/init.d/mountall.sh
	echo "Done"
	
	#################################
	# Modifying /etc/modules
	#################################
	echo -n "Modifying /etc/modules ... "
	echo "natsemi" >> $DEST/etc/modules
	echo "hostap_pci" >> $DEST/etc/modules
	echo "Done"	
}

###############################################################################################

gen_cd()
{
	echo "Generating bootable CD ..."
	echo "deb http://developer.linuxtag.net/knoppix ./" >> $DEST/etc/apt/sources.list
	chroot $DEST apt-get update	
	chroot $DEST apt-get install hwsetup hwdata-knoppix
	chroot $DEST apt-get install kernel-image-2.4.26
}

if [ "$1" == "cd" ] ; then
	gen_sourceslist
	gen_scripts
	remove_pkg
	set_hostname
	set_timezone
	#copy_script
	install_extra
	install_prismfw
	gen_cd
	#gen_dpkglist
	remove_locale
	remove_extra
	remove_timezone
	setup_etc
else
	gen_sourceslist
	gen_scripts
	remove_pkg
	set_hostname
	set_timezone
	copy_script
	install_kernel
	install_extra
	install_prismfw
	install_lilo
	install_voyage_util
	gen_dpkglist
	remove_locale
	remove_timezone
	remove_extra
	setup_rw
	setup_etc
fi                         
###############################################################################################
                           
##########################################################
echo ""                    
echo "Build completed "    
echo "Complete Voyage package is stored in:"
echo "	$DEST"             
echo ""
