#!/bin/sh


# Hint: "ori" = ORIginal; "rb" = ReadBack; "res" = RESult; "err" = ERRor



# Make shure that mounting Directory exists
if [ ! -d /mnt/usb1 ]
then
    mkdir /mnt/usb1
fi

# Make shure that result Directory exists
if [ ! -d /test/result ]
then
    mkdir /test/result
fi

# Make shure that result file of Test does NOT exists
if [ -s /test/result/usb1.res ]
then
    rm /test/result/usb1.res
fi

# Make shure that error file of Test does NOT exists
if [ -s /test/result/usb1.err ]
then
    rm /test/result/usb1.err
fi

# Mount USB-Stick to /mnt/usb1
mount /dev/sda1 /mnt/usb1 2>> /test/result/usb1.err

# Copy Testfile from Test Directory to USB-Stick
cp /test/testfile /mnt/usb1 2>> /test/result/usb1.err

# Compare original Testfile and copied Testfile on USB-Stick
# Print compare results to /test/result/usb1.res
# Print compare errors to /test/result/usb1.err
if [ ! -e /mnt/usb1/testfile ]
then
    echo "TESTFILE DOES NOT EXIST" >> /test/result/usb1.err
fi
diff /test/testfile /mnt/usb1/testfile 1>> /test/result/usb1.res 
                                                                                    2>> /test/result/usb1.err

# Delete Testfile on USB-Stick
rm /mnt/usb1/testfile 2>> /test/result/usb1.err

# Unmount USB-Stick
umount /mnt/usb1 2>> /test/result/usb1.err

# Delete Folder usb1 in /mnt
rmdir /mnt/usb1 2>> /test/result/usb1.err

# Display content of /test/result/usb1.res
echo -e "\n\rContent of usb1.res"
cat /test/result/usb1.res

# Display content of /test/result/usb1.err
echo -e "\n\rContent of usb1.err"
cat /test/result/usb1.err

# If comparison returned neither results nor errors, the copying and comparision was successful
if [ ! -s /test/result/usb1.res -a ! -s /test/result/usb1.err ]
then
    # Test was successful
    echo -e "\n\rUSB1 TEST-PASSED"
    echo "USB1 TEST-PASSED"  >> /test/result/usb1.res
else
    # Test failed
    echo -e "\n\rUSB1 TEST-FAIL"
    echo "USB1 TEST-FAILED"  >> /test/result/usb1.res
fi