#!/bin/sh

# not set -e: we are testing exit codes

# to have diffoscope able to output stuff in utf-8
export LC_ALL=C.UTF-8

if ! [ -d "$AUTOPKGTEST_TMP" ]; then
	AUTOPKGTEST_TMP=`mktemp -d`
	TEMP=true
fi

echo "a" > $AUTOPKGTEST_TMP/a
echo "a" > $AUTOPKGTEST_TMP/a_
echo "b" > $AUTOPKGTEST_TMP/b

echo "Testing identical files..."
diffoscope $AUTOPKGTEST_TMP/a $AUTOPKGTEST_TMP/a_
if [ $? -ne 0 ]; then
	echo "Exit code was different from 0 when comparing files with identical content." >&2
	exit 1
fi

echo "Testing different files..."
diffoscope $AUTOPKGTEST_TMP/a $AUTOPKGTEST_TMP/b
if [ $? -ne 1 ]; then
	echo "Exit code was different from 1 when comparing files with different content." >&2
	exit 1
fi

echo "Testing LC_ALL=C works..."
LC_ALL=C diffoscope --debug $AUTOPKGTEST_TMP/a $AUTOPKGTEST_TMP/a_ 2>/dev/null
if [ $? -ne 0 ]; then
	echo "diffoscope could not handle LC_ALL=C; make sure you're not unconditionally outputting non-ascii chars anywhere." >&2
	exit 1
fi

echo "Testing LC_ALL=C works (--help)..."
LC_ALL=C diffoscope --help >/dev/null
if [ $? -ne 0 ]; then
	echo "diffoscope could not handle LC_ALL=C; make sure you're not unconditionally outputting non-ascii chars anywhere." >&2
	exit 1
fi

echo "Testing invalid command line flag..."
diffoscope --thisflagdoesntexistandwontexist
if [ $? -ne 2 ]; then
	echo "Exit code was different from 2 when passing a non-existent flag." >&2
	exit 1
fi

if [ -n "${TEMP:-}" ]; then
	rm -rf "$AUTOPKGTEST_TMP"
fi

echo "All good!"
