#!/bin/sh
fdisk /dev/mmcblk1 <<EOF
o
p
n
p
1

2048
t
e
a
1
n
p
2


p
w
EOF
umount /dev/mmcblk1p1
dd if=/dev/zero of=/dev/mmcblk1p1 bs=1024 count=1024
mkfs.vfat -F 16 /dev/mmcblk1p1
umount /dev/mmcblk1p2
dd if=/dev/zero of=/dev/mmcblk1p2 bs=1024 count=1024
mkfs.ext4 /dev/mmcblk1p2
# temp. mountdirectories
mkdir /tmp/boot
mkdir /tmp/bootdata
mkdir /tmp/rootfs
# mounten der Partitionen
mount /dev/mmcblk1p1 /tmp/boot
mount /dev/mmcblk1p2 /tmp/rootfs
mount /dev/mmcblk0p1 /tmp/bootdata
# daten kopieren
cp -f /tmp/bootdata/MLO /tmp/boot
cp -f /tmp/bootdata/*.dtb /tmp/boot
cp -f /tmp/bootdata/u-boot.img /tmp/boot
cp -f /tmp/bootdata/uImage /tmp/boot
#--- build rootfs -- make default sys dirs for rootfs
mkdir -p /tmp/rootfs/sys
mkdir -p /tmp/rootfs/proc
mkdir -p /tmp/rootfs/tmp/fontconfig
mkdir -p /tmp/rootfs/dev/pts
mkdir -p /tmp/rootfs/dev/shm
WRKDIR=`pwd`
cd /tmp/rootfs/dev
ln -s ../tmp/log ./log
cd $WRKDIR
for i in `seq 0 7` ; do 
  echo "usb$i"
  mkdir -p /tmp/rootfs/media/usb$i
done
mkdir -p /tmp/rootfs/mnt
#--- copy rootfs from SD
cd /
tar --exclude=./sys --exclude=./proc --exclude=./dev --exclude=./media --exclude=./tmp --exclude=./mnt -cf - . | (cd /tmp/rootfs && tar -xvf -)
#--- copy done
cd 
df -h
umount /dev/mmcblk1p1 /dev/mmcblk1p2 /dev/mmcblk0p1
