#!/bin/sh

# Print an opening message
echo ""
echo "********************************"
echo "* Debian ncompress smoke test  *"
echo "********************************"
./compress -V 2>&1 | head -1
echo ""

# Create an input file
echo -n "Creating input file..."

cp debian/sample.txt input >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: $?"
   rm -f input input.Z
   exit 1
fi

echo "done."

# Compress the input file
echo -n "Compressing the input file..."

./compress input >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: error $?"
   rm -f input input.Z
   exit 1
fi

if [ ! -f input.Z ]; then
   echo "failed: did not get expected output file input.Z"
   rm -f input input.Z
   exit 1
fi

echo "done."

# Check for compatibility by uncompressing the input file with the gzip uncompress
echo -n "Checking for compatibility with gzip..."

/bin/uncompress input.Z >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: error $?"
   rm -f input input.Z
   exit 1
fi

if [ ! -f input ]; then
   echo "failed: did not get expected output file input"
   rm -f input input.Z
   exit 1
fi

/usr/bin/diff -q input debian/sample.txt >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: uncompressed file does not match original sample"
   rm -f input input.Z
   exit 1
fi

echo "done."

# Compress the input file again
echo -n "Compressing the input file again..."

./compress input >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: error $?"
   rm -f input input.Z
   exit 1
fi

if [ ! -f input.Z ]; then
   echo "failed: did not get expected output file input.Z"
   rm -f input input.Z
   exit 1
fi

echo "done."

# Check that uncompress works
echo -n "Checking uncompress behavior..."

./compress -d input.Z >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: error $?"
   rm -f input input.Z
   exit 1
fi

if [ ! -f input ]; then
   echo "failed: did not get expected output file input"
   rm -f input input.Z
   exit 1
fi

/usr/bin/diff -q debian/sample.txt input >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "failed: uncompressed file does not match original sample"
   rm -f input input.Z
   exit 1
fi

echo "done."

# Clean up after ourselves
rm -f input input.Z

# Print a closing message
echo ""
echo "**********************"
echo "* Smoke test passed. *"
echo "**********************"
echo ""

