#!/bin/sh

# This is the testsuite for GNU Midnight Commander.
# Maintainer doing the release must ensure that this testsuite passes.

set -e

trap 'echo >&2 "ERROR: Test not completed, exit with code $?"; exit 1' exit

distcheck() {
    id="$1"
    shift
    make distcheck "$MAKEFLAGS" "$@" >test-$id.out 2>test-$id.err
}

# Specify which warnings we want to see.
# Don't add options that produce massive warnings.
# Add -Wstrict-prototypes after main.h is fixed (always use cpanel).
# Maybe add -W if a better way to initialize quick dialogs is found.
export CFLAGS="-O2 -Wall -Wno-unused-parameter -Wno-sign-compare \
-Wmissing-declarations -Wmissing-prototypes -Wbad-function-cast \
-Wcast-align -Wpointer-arith -Wredundant-decls -Wundef -Wfloat-equal"

# Suppress progress indicator
MAKEFLAGS='MSGMERGE_FLAGS=--no-location --quiet'

echo "Checking the documentation"
maint/doctest

echo "Bootstraping from CVS"
./autogen.sh >test-autogen.out 2>test-autogen.err

echo "Checking configure"
bash -n configure 2>test-configure.err

echo "Making everything in the source directory"
make all >test0.out 2>test0.err

echo "Checking the default configuration"
distcheck 1

echo "Checking the configuration with maximal code coverage"
distcheck 2 enable_charset=yes with_samba=yes \
     with_included_gettext=yes

echo "Checking the configuration with minimal code coverage"
distcheck 3 enable_largefile=no enable_nls=no with_vfs=no \
     with_gpm_mouse=no with_subshell=no with_edit=no with_ext2undel=no \
     with_screen=ncurses with_x=no enable_background=no

echo "Checking the configuration with minimal code coverage + editor"
distcheck 4 enable_largefile=no enable_nls=no with_vfs=no \
     with_gpm_mouse=no with_subshell=no with_ext2undel=no \
     with_screen=ncurses

echo "Checking the configuration with experimental and rarely used options"
distcheck 5 with_mmap=no with_subshell=optional

RPMBUILD=/usr/bin/rpmbuild
if test -x $RPMBUILD; then
	echo "Building RPM package"
	RPM_SRC_DIR="`pwd`/rpm"
	rm -rf $RPM_SRC_DIR
	mkdir "$RPM_SRC_DIR"
	mkdir "$RPM_SRC_DIR/BUILD"
	mkdir "$RPM_SRC_DIR/RPMS"
	mkdir "$RPM_SRC_DIR/RPMS/`$RPMBUILD --eval='%{_host_cpu}' 2>/dev/null`"
	mkdir "$RPM_SRC_DIR/SPECS"
	$RPMBUILD -tb --define="_topdir $RPM_SRC_DIR" mc-*.tar.gz >test-rpm.out 2>test-rpm.err
else
	echo "rpmbuild not found"
fi

trap - exit
echo "All done.  No fatal errors.  Please check test*.err files."
