#!/bin/sh

DIR="$AUTOPKGTEST_TMP/sample"
ORG_FS=sample.cramfs
SWP_1_FS=sample_swp_1.cramfs
SWP_2_FS=sample_swp_2.cramfs

# create directory
mkdir -p $DIR

# create file system image
/usr/sbin/mkfs.cramfs -E $DIR $ORG_FS

# show file details
file $ORG_FS

# swap
cramfsswap $ORG_FS $SWP_1_FS

# show swapped file details
file $SWP_1_FS

# compare first endian swap with original
diff $ORG_FS $SWP_1_FS

# save result
RESULT=$?

# check result
if [ $RESULT -eq 0 ]; then
    # file not swapped
    rm -rf $DIR
    rm *.cramfs
    exit 1
fi

# swap second time to revert back to original
cramfsswap $SWP_1_FS $SWP_2_FS

# show swapped file details
file $SWP_2_FS

# compare second endian swap with original
diff $ORG_FS $SWP_2_FS

# save result
RESULT=$?

# cleanup
rm -rf $DIR
rm *.cramfs

# check result
if [ $RESULT -eq 0 ]; then
    exit 0
else
    exit 1
fi
