#!/bin/sh
OPTS="-O bigalloc -C 8192"
SIZE=4096
IMG=/tmp/foo.img
TMP=$(mktemp)
SIZE_A=15000
SIZE_B=5000
SIZE_C=20000

DEBUGFS=./debugfs/debugfs
MKE2FS=./misc/mke2fs
FSCK=./e2fsck/e2fsck

dd if=/dev/zero of=$IMG bs=1k count=$SIZE
echo $MKE2FS -F -t ext4 -L test $OPTS test.img $SIZE
$MKE2FS -F -t ext4 -L test $OPTS $IMG $SIZE
dd if=/dev/zero of=$TMP bs=$SIZE_A count=1 >& /dev/null
echo Writing $SIZE_A bytes to a
$DEBUGFS -w -R "write $TMP a" $IMG
BLKS=$(./debugfs/debugfs -R "blocks a" $IMG)
cp /dev/null $TMP
echo "Releasing blocks $BLKS"
for i in $BLKS ; do echo "freeb $i" >> $TMP; done
$DEBUGFS -w $IMG < $TMP >& /dev/null

echo Writing $SIZE_B bytes to b
dd if=/dev/zero of=$TMP bs=$SIZE_B count=1 >& /dev/null
$DEBUGFS -w -R "write $TMP b" $IMG
if [ -n "$SIZE_C" ]; then
    BLKS=$(./debugfs/debugfs -R "blocks b" $IMG)
    cp /dev/null $TMP
    echo "Releasing blocks $BLKS"
    for i in $BLKS ; do echo "freeb $i" >> $TMP; done
    $DEBUGFS -w $IMG < $TMP >& /dev/null

    echo Writing $SIZE_C bytes to c
    dd if=/dev/zero of=$TMP bs=$SIZE_C count=1 >& /dev/null
    $DEBUGFS -w -R "write $TMP c" $IMG
fi
echo "set_inode_field a mtime 201107040000" > $TMP
echo "set_inode_field b mtime 201107050000" >> $TMP
if [ -n "$SIZE_C" ]; then
    echo "set_inode_field c mtime 201107060000" >> $TMP
fi
$DEBUGFS -w $IMG < $TMP >& /dev/null

$FSCK -fy $IMG
rm $TMP

