#!/bin/sh

#export PATH=/tracks/toolchain/usr/bin/:$PATH
# Abort script on ANY error
set -e

#Flash boot partition size in megabytes
KVERS=2.4.20

PCMCIAMODULES="ds.o i82365.o ide-cs.o pcmcia_core.o pcnet_cs.o serial_cs.o \
xirc2ps_cs.o hermes.o orinoco_cs.o orinoco.o"
PCMCIADELETE="cardctl dump_cis ftl_* pack_cis pcinitrd"

FLASHFILES="bb uc base dev kernel-$KVERS modules-$KVERS wine x11-4.2.1 opt xconfig tools misc-utils libc6"
E2FSSIZE=22

#F#LASHFILES="bb uc base dev kernel-$KVERS modules-$KVERS opt tools misc-utils"
#E2FSSIZE=10

# Uncomment following line to include nss libs in libc6. Needed for libc6
# net tools like lftp. (Not used here)
#WITHLIBNSS=1

# Uncomment following line to build two kernel monte with modules
# So far only used in 1st stage loader on flash which is hand built anyway
#WITHMONTE=1

EMBROOT=/home/brad/embeded
TARGET=`mktemp -d -p $EMBROOT`
export CC=/usr/local/i386-linux-uclibc/bin/i386-uclibc-gcc
#export CC=/tracks/toolchain/usr/bin/gcc
ARCHDIR=$EMBROOT/files-devel
EMBEDDIR=$EMBROOT
LILO=$EMBROOT/disk/sbin/lilo
FLASHDISK=/dev/hde

PCMCIA=`ls $EMBEDDIR/src/pcmcia* -d`
E2FSPROGS=`ls $EMBEDDIR/src/e2fsprogs* -d`
LILODIR=`ls $EMBEDDIR/src/lilo* -d`
DOSFSTOOLS=`ls $EMBEDDIR/src/dosfstools* -d`
MONTE=`ls $EMBEDDIR/src/beoboot* -d`\/monte
#UTELNETD=`ls $EMBEDDIR/src/utelnetd* -d`
UTILLINUX=`ls $EMBEDDIR/src/util-linux* -d`
SUDO=`ls $EMBEDDIR/src/sudo* -d`
WIRELESS=`ls $EMBEDDIR/src/wireless_tools* -d`
IPTABLE=`ls $EMBEDDIR/src/iptables* -d`
HDPARM=`ls $EMBEDDIR/src/hdparm* -d`
SETSERIAL=`ls $EMBEDDIR/src/setserial* -d`
PCIUTILS=`ls $EMBEDDIR/src/pciutils* -d`
#UCLIBC=`ls $EMBEDDIR/toolchain/gcc-3.2.1/build/uClib* -d`


debug ()
{
echo $PCMCIA
echo $E2FSPROGS
echo $LILODIR
echo $DOSFSTOOLS
echo $MONTE
#echo $UTELNETD
echo $UTILLINUX
echo $SUDO
echo $HDPARM
echo $TARGET
echo $SETSERIAL
echo $PCIUTILS
echo $UCLIBC
echo $IPTABLE
}

#--------------------------------------------------------------------------
# Tool and Utillity routines

archive ()
{
cd $TARGET
tar -cv * | gzip -9 > $ARCHDIR/$1.tgz
rm $TARGET -rf
mkdir -p $TARGET
}

archivestrip ()
{
cd $TARGET
strip `find . -type f`
tar -cvzf $ARCHDIR/$1.tgz *
rm $TARGET -rf
mkdir -p $TARGET
}

#--------------------------------------------------------------------------

uClibc ()
{
rm /usr/local/i386-linux-uclibc/ -rf
cd $EMBEDDIR/src/uClibc
make clean 
make 
make install
#cd $UCLIBC
make PREFIX=$TARGET install_target
cd $TARGET
mkdir -p usr/local/i386-linux-uclibc/lib
ln -s /lib/ld-uClibc.so.0 usr/local/i386-linux-uclibc/lib/ld-uClibc.so.0 
archive uc
}

#--------------------------------------------------------------------------

busybox ()
{
cd $EMBEDDIR/src/busybox
make clean
make CC=$CC
make CC=$CC
make CC=$CC PREFIX=$TARGET install

#cd $UTELNETD
#make clean
#make
#cp utelnetd $TARGET/bin

mkdir -p $TARGET/sbin
cd $SUDO
make clean
./configure --with-CC=$CC --disable-saved-ids
make
strip sudo
cp sudo $TARGET/sbin

archivestrip bb
}


#--------------------------------------------------------------------------

disktools ()
{
#e2fsprogs
cd $E2FSPROGS
rm build -rf
mkdir build && cd build
../configure --enable-dynamic-e2fsck \
--enable-elf-shlibs --disable-fsck --disable-debugfs --disable-imager\
	--disable-resizer --prefix=$TARGET

make all install-progs-recursive \
install-shlibs-libs-recursive
cd $E2FSPROGS
rm build -rf

cd $TARGET/sbin
rm fsck.ext2 fsck.ext3 mkfs.ext2 dumpe2fs tune2fs
ln -s e2fsck fsck.ext3
ln -s e2fsck fsck.ext2
ln -s mke2fs mkfs.ext2

#lilo
cd $LILODIR
make clean
make
cp lilo $TARGET/sbin

#fdisk - hwclock
cd $UTILLINUX
make clean
configure
cd fdisk
make fdisk sfdisk
cp fdisk sfdisk $TARGET/sbin

# Hwclock is now in busybox!
#cd ../hwclock
#make
#cp hwclock $TARGET/sbin

# HDPARM
cd $HDPARM
make clean
make CC=$CC 
cp hdparm $TARGET/sbin

rm $TARGET/man -r

rm `find $TARGET/lib -type l`
ldconfig -n -N $TARGET/lib

archivestrip tools
}

#--------------------------------------------------------------------------
dosfstools ()
{
cd $DOSFSTOOLS
make clean
make
make PREFIX=$TARGET install
rm $TARGET/usr -rf
archivestrip dostools
}

#--------------------------------------------------------------------------

updateflash ()
{
cd $ARCHDIR
mount /flash
rm /flash/files/* -f
for I in $FLASHFILES ; do
echo Copying $I
cp $I.tgz /flash/files/
done;
umount /flash

}

#--------------------------------------------------------------------------

kernel ()
{

cd $EMBEDDIR/src/linux
make bzImage
make modules
VERSION=`strings init/version.o | grep "Linux version" | cut -f3 -d" "`
make INSTALL_MOD_PATH=$TARGET modules_install
mkdir $TARGET/boot
cp arch/i386/boot/bzImage $TARGET/boot

if [ $WITHMONTE ] ; then
mkdir $TARGET/sbin
cd $MONTE
make clean
make kmonte.o
make CC=$CC monte
#kver=`./kver kmonte.o` && install -m 644 kmonte.o $(prefix)/lib/modules/$$kver/misc
#kver=`strings --target binary kmonte.o | grep ^kernel_version= | cut -d= -f2`
kver=$VERSION
mkdir -p $TARGET/lib/modules/$kver/misc $TARGET/sbin
cp kmonte.o $TARGET/lib/modules/$kver/misc
cp monte $TARGET/sbin
fi;

cd $TARGET
#VERSION=`ls lib/modules/`
tar --exclude=boot -cvzf $ARCHDIR/modules-$VERSION.tgz *
tar -cvzf $ARCHDIR/kernel-$VERSION.tgz boot/
cd $EMBEDDIR
rm $TARGET -r 
}

#--------------------------------------------------------------------------

makeflash ()

{
su -p -c "echo Creating Flash Disk &&\
echo -e ',$E2FSSIZE,L,*\n,,06\n' | sfdisk -uM $FLASHDISK &&\
mke2fs $FLASHDISK\1 &&\
tune2fs -i 0 $FLASHDISK\1 &&\
mkdosfs $FLASHDISK\2 &&\
mount -t ext2 $FLASHDISK\1 /flash &&\
cd /flash &&\
tar -xzf $ARCHDIR/1stroot.tgz &&\
tar -xzf $ARCHDIR/stage1-2.4.19.tgz &&\
$LILO -r /flash -b $FLASHDISK &&\
cd / &&\
umount /flash"
}

#--------------------------------------------------------------------------

updwine ()
{
BASEDLLS="x11drv.dll ntdll.dll"
unset CC
cd /usr/src/cvs/wine/embed
#make clean

make depend && SKIP=1
if [ $SKIP ] ; then
make
else
../configure --prefix=/usr/local/wine --without-opengl 
#	--disable-debug --disable-trace --without-curses
#	--x-libraries=/tracks/xtest/usr/X11R6/lib \
#	--x-includes=/tracks/xtest/usr/X11R6/include

make depend
make
fi;
echo Doing temp install
rm temp -rf
mkdir temp
make prefix=/usr/src/cvs/wine/embed/temp/ install
cd temp
rm man include -rf
mv bin/wine bin/wineserver .
rm bin -rf
rm lib/wine/*.exe.so share `find . -name "*.def"` -rf
cd lib/wine/
mkdir temp
mv *.so temp
for i in `cat $EMBEDDIR/config/wineexports` $BASEDLLS ; do
mv temp/$i.so .
done;
rm temp -rf
cd ../../

mkdir -p $TARGET/usr/local/wine $TARGET/usr/bin
cp -a * $TARGET/usr/local/wine
cd ..
rm temp -rf
 
cd $TARGET/usr/local/wine
echo Stripping
strip `find . -type f`
cd ../../bin
ln -s ../local/wine/wine wine
ln -s ../local/wine/wineserver wineserver
cd $TARGET
tar -cvzf $ARCHDIR/wine.tgz *
cd ..
rm $TARGET -rf
}

#--------------------------------------------------------------------------

updatex ()
{
unset CC

rm -rf /tracks/xtest/*
cd /tracks/xfree-4.2.1/xc
#cd /tracks/xfree-4.3/xc
#cd /tracks/xfree-4.1/xc
#make DESTDIR=/tracks/xtest World
make DESTDIR=/tracks/xtest install

mkdir -p $TARGET
cp -a /tracks/xtest/* $TARGET
cd $TARGET
XVERS=`usr/X11R6/bin/X -version 2>&1  | grep "XFree86 Version" | cut -f 3 -d" "`
echo XFree Version $XVERS
cd usr/X11R6/bin
mkdir temp
mv      X \
        XFree86 \
        startx \
        xauth \
        xinit \
        temp
cd ../../../


tar --exclude=usr/X11R6/bin/temp -cvzf $ARCHDIR/x11extra-$XVERS.tgz \
        usr/X11R6/include \
        usr/include \
        usr/X11R6/lib/*.a \
        usr/X11R6/lib/X11/doc \
        usr/X11R6/lib/X11/config \
        usr/X11R6/lib/X11/xkb \
        usr/X11R6/bin

rm usr/X11R6/include \
        usr/include \
        usr/X11R6/lib/*.a \
        usr/X11R6/lib/X11/doc \
        usr/X11R6/lib/X11/config \
        usr/X11R6/lib/X11/xkb \
        -rf

cd usr/X11R6/bin
rm `find . -type f -maxdepth 1`
mv temp/* .
rm temp -r
cd ../../../

archive x11-$XVERS
}

#--------------------------------------------------------------------------

syslinux ()
{
echo Writing blank syslinux image
zcat /home/brad/diskimages/tools/syslinux.disk.gz | dd of=/dev/fd0
}

bootdisk ()
{
echo Mounting disk
mount /floppy
rm -f /floppy/linux /floppy/rootimg.bin
echo copying kernel
cp kernel/bzImage /floppy/linux

BEFORE=`ls -la rootimg.bin.gz | tr -s " " | cut -d" " -f5`

TMP1=`du -c disk | grep total | cut -f1`
TMP2=`dc -e "$TMP1 200 + p"`
TMP3=`find disk | grep -c \/`
TMP4=`dc -e "$TMP3 100 + p"`
echo Filesystem size must be $TMP2
echo There are $TMP3 files in the filesystem so
echo Making ext2 File system of $TMP4 files
genext2fs/genext2fs -d disk -b $TMP2 -i $TMP4 rootimg.bin
echo -n Compresing...
gzip -f -9 rootimg.bin
echo Done
AFTER=`ls -la rootimg.bin.gz | tr -s " " | cut -d" " -f5`
TOTAL=`dc -e "$AFTER $BEFORE - p"`
echo Old $BEFORE New $AFTER Difference $TOTAL
cp rootimg.bin.gz /floppy/rootimg.bin
cat kernel/syslinux.cfg | sed -e "s/xxxx/$TMP2/" > /floppy/syslinux.cfg
df -h | grep \/floppy
echo Unmounting Floppy
umount /floppy
}

#--------------------------------------------------------------------------
gendisk ()
{
DISK=/home/brad/embeded/disk
FLOPPYFILES="bb uc floppy-base tools pcmcia- waap"
#tools dos-tools
rm $DISK -rf
mkdir -p $DISK
cd $DISK

for I in $FLOPPYFILES ; do 
echo Uncompressing $I
tar -xzf $ARCHDIR/$I.tgz
done;

cd $EMBEDDIR/src/linux

make bzImage
make modules
make INSTALL_MOD_PATH=$DISK modules_install 
cp arch/i386/boot/bzImage $EMBEDDIR/kernel/bzImage
cd $DISK
su -p -c "tar -xzf $ARCHDIR/dev.tgz"
rm -f `find $DISK -name kmonte.o`
rm -f $DISK/sbin/monte $DISK/sbin/lilo $DISK/sbin/sudo

echo Finished re-creating boot floppy image
}
#--------------------------------------------------------------------------
pcmcia ()
{

cd $PCMCIA

make clean
./Configure --ucc=$CC -n --kernel=$EMBEDDIR/src/linux/ --nopnp \
--noapm --srctree --sysv --target=$TARGET --rcdir=/etc/rc.d
make all
make install
rm $TARGET/usr -rf
rm $TARGET/etc/rc.d -rf
if [ -e $TARGET/lib/modules ] ; then
KV=`ls $TARGET/lib/modules`
fi;
cd $WIRELESS
make clean
make
cp iwconfig $TARGET/sbin

if [ -n "$KV" ] ; then
cd $TARGET/lib/modules/$KV/pcmcia
mkdir temp
mv $PCMCIAMODULES temp
rm *.o
mv temp/* .
rm -rf temp
fi;
cd $TARGET/sbin
rm $PCMCIADELETE -f

archive pcmcia-$KV
}

#--------------------------------------------------------------------------
iptables ()
{

cd $IPTABLE

make clean
make KERNEL_DIR=/home/brad/embeded/src/linux all
make PREFIX=$TARGET KERNEL_DIR=/home/brad/embeded/src/linux install
rm $TARGET/man -rf
archive iptables

}

#--------------------------------------------------------------------------
libc6 ()
{

LIBS="/lib/ld-2.2.5.so /lib/libc-2.2.5.so /lib/libdl-2.2.5.so \
/usr/lib/libz.so.1.1.4 /usr/lib/libjpeg.so.62.0.0 /lib/libm-2.2.5.so \
/lib/libgcc_s.so.1 /usr/lib/libungif.so.4" 

if [ $WITHLIBNSS ] ; then
LIBS=$LIBS" /lib/libnss_compat-2.2.5.so /lib/libnss_nis-2.2.5.so \
/lib/libnss_db-2.2.5.so /lib/libnss_nisplus-2.2.5.so /lib/libnss_dns-2.2.5.so \
/lib/libnss_files-2.2.5.so /lib/libnss_hesiod-2.2.5.so"
fi;

mkdir -p $TARGET/usr/lib $TARGET/lib $TARGET/etc $TARGET/sbin
#for I in $DBINS ; do
#cp `which $I` $TARGET/dbin
#done ;

cp `which ldconfig` $TARGET/sbin

for I in $LIBS ; do
cp $I $TARGET/usr/lib
done;

printf "/usr/X11R6/lib\n/usr/local/wine/lib\n" > $TARGET/etc/ld.so.conf
cd $TARGET/lib
ln -s ../usr/lib/ld-2.2.5.so ld-linux.so.2


archive libc6

}

#--------------------------------------------------------------------------

miscutils ()
{
# mcookie
cd $UTILLINUX
make clean
configure
cd misc-utils
make
mkdir -p $TARGET/usr/bin
cp mcookie $TARGET/usr/bin

# lspci
cd $PCIUTILS
make clean
make CC=$CC
mkdir -p $TARGET/bin
cp lspci $TARGET/bin

# serserial
cd $SETSERIAL
make clean
configure
make
cp setserial $TARGET/bin

archivestrip misc-utils 
}

copyapp ()
{
SOURCE=$EMBEDDIR/apps/$1
echo $SOURCE
if [ -d $SOURCE ] ; then
su -p -c "echo Copying Aplication $2 &&\
mount -t vfat -o umask=0000,quiet $FLASHDISK\2 /flash &&\
cd /flash &&\
rm * -rf &&\
cp -a $SOURCE/* . &&\
cd / &&\
umount /flash"
fi;
}
wineimports ()
{
echo Removing old imports
rm $EMBDEDDIR/config/wineexports -f
/usr/local/wine/wine --debugmsg +loaddll,-commctrl /home/brad/win/bup/dgi.exe die 2>&1 \
	| grep load_dll | cut -f4 -d\\ | cut -f1 -d\' > $EMBEDDIR/config/wineexports
echo Creating New Imports
cat $EMBEDDIR/config/wineexports

}
#--------------------------------------------------------------------------

startweb ()
{
mini_httpd -p 4000
}

if [  -d $TARGET ] ; then
	rm $TARGET -rf
	mkdir -p $TARGET
	fi;

case $1 in
#--------Routines to update software

all)
	uClibc ; busybox ; disktools ; kernel ; updwine ; updatex
	dosfstools ; pcmcia ; libc6 ; iptables ; miscutils
	;;	
uClibc)
	uClibc ; busybox ; disktools ; dosfstools ; pcmcia ; iptables ; miscutils
	;;
busybox)
	busybox
	;;
disktools)
	disktools
	;;
kernel)
	kernel
	;;
wine)
	updwine
	wineimports
	;;
imports)
	wineimports
	;;
xfree)
	updatex
	;;
dosfstools)
	dosfstools
	;;
pcmcia)
	pcmcia
	;;
libc6)
	libc6
	;;
iptables)
	iptables
	;;
miscutils)
	miscutils
	;;
uc)
	uClibc
	;;
#--------Routines to generate and update flash card

updflash)
	updateflash
	;;
makeflash)
	makeflash
	;;

#--------Routines to create/update boot disk

syslinux)
	syslinux
	;;
bootdisk)
	bootdisk
	;;
gendisk)
	gendisk
	;;
copyapp)
	copyapp $2
	;;
	
#--------Nothing recognised or specified

startweb)
	startweb
	;;
debug)
	debug
	;;
test)	uClibc
	;;
*)
	echo usage $0 "all | uClibc | busybox | tinylogin | disktools | flash"
	echo "kernel | updflash | makeflash | wine | xfree | syslinux"
	echo "bootdisk | startweb | dosfstools | debug"
	echo
	echo "uClibc tinylogin disktools kernel wine xfree dosfstools"
	echo "- all update"
	echo "the relevant binary files from the available source code"
	echo
	echo "updflash refreshes the archive files on the flash card"
	echo
	echo "makeflash partitions and formats the flash card while placing"
	echo "the dos partition application code on the card"
	echo
	echo "syslinux sets up a formatted floppy with the syslinx boot code"
	echo
	echo "bootdisk creates a syslinuxed disk from the disk directory"
	echo
	echo "startweb starts the mini_httpd web server for on-line updates"
	echo
	;;
esac


if [ -d $TARGET ] ; then
	rm $TARGET -rf
	fi;
	
echo 
echo If you can read this then whatever you were doing worked!
