#!/bin/sh

cd test

log()
{
    echo
    echo =========================================================================
    echo $*
    echo =========================================================================
    echo
}

VALGRIND=valgrind
VALGRIND=

run()
{
    echo $*
    $VALGRIND $*
}

log "Format 1G disk file with 2 DOS partitions. Put grub in MBR."
run ../fatdisk mydisk.img format name FATDISK size 1G part 0 50% boot grub.img part 1 40% fat32
if [ $? -ne 0 ]
then
    exit 1
fi

log "Detailed disk info"
run ../fatdisk mydisk.img info
if [ $? -ne 0 ]
then
    exit 1
fi

log "Disk summary"
run ../fatdisk mydisk.img summary
if [ $? -ne 0 ]
then
    exit 1
fi

log "Adding files to disk"
run ../fatdisk mydisk.img add testfile
if [ $? -ne 0 ]
then
    exit 1
fi

log "Repeating to make sure we do not get two copies"
run ../fatdisk mydisk.img add testfile
if [ $? -ne 0 ]
then
    exit 1
fi

cat >testfile <<%%
this is a test file
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
01234568790123456879012345687901234568790123456879012345687901234568790123456879
this is the end of the test file.
%%

cp testfile testfile.orig

log "Adding a test file and checking the contents"
run ../fatdisk mydisk.img add ./testfile
if [ $? -ne 0 ]
then
    exit 1
fi

log "Hexdump of file"

run ../fatdisk mydisk.img hexdump testfile
if [ $? -ne 0 ]
then
    exit 1
fi

log "Extracting the file"
run ../fatdisk mydisk.img extract testfile
if [ $? -ne 0 ]
then
    exit 1
fi

log "Listing disk"
run ../fatdisk mydisk.img ls
if [ $? -ne 0 ]
then
    exit 1
fi

log "Comparing extracted dir from disk, should see no difference"
run ../fatdisk mydisk.img ex
if [ $? -ne 0 ]
then
    exit 1
fi

log "Diffing files, should see no diff"
diff -r testfile.orig testfile

/bin/rm mydisk.img
