#!/bin/bash

TMPIMG=tmpimg.bin
NEWIMG=newimg.bin
WEBPAGES=webpages-6104k.bin


function die() {
    echo $@
    exit 1
}

for i in `seq 0 7`; do losetup -d /dev/loop$i; done

test -d newimg || mkdir newimg
test -d tmpimg || mkdir tmpimg

mount -o loop $TMPIMG tmpimg                        || die "Mount tmpimg fail!"

echo "******  make changes ******"
/bin/bash

echo -n "" >$NEWIMG
dd if=/dev/zero of=$NEWIMG bs=1k count=4500         || die "dd fail"
mke2fs -F -m 0 -b 1024 $NEWIMG                      || die "mke2fs fail"
tune2fs -c 0 $NEWIMG
mount -o loop $NEWIMG newimg                        || die "mount $NEWIMG fail"
cp -a tmpimg/* newimg
chown -R root.root newimg
sleep 2
umount newimg
umount tmpimg

rm $NEWIMG.bz2
bzip2 -9 $NEWIMG 
../bin/padfile $NEWIMG.bz2 1187840
cat kernel.bin  $NEWIMG.bz2 >vmlinux.bin

test -f vmlinux.bin.gz && rm vmlinux.bin.gz
gzip vmlinux.bin
../bin/mksyshdr csys.bin vmlinux.bin.gz
cat mainheader.bin $WEBPAGES csys.bin vmlinux.bin.gz >firmware.bin


