#!/usr/bin/env bash
#
# A script that quickly validates some things that can easily be broken by
# commits. This should run fast enough that it can be run after every commit or
# push.

# This should fail as soon as something goes awry.
set -e

CWD=$(cd $(dirname $0) ; pwd)

# Ensure CC is respected, since it is typically ignored by chplenv/.
if [ -n "${CC}" ] ; then
    case $CC in
        gcc)
            # Deal with a frequent chapel stumbling block...
            echo "Setting CHPL_HOST_COMPILER and CHPL_TARGET_COMPILER to: gnu"
            export CHPL_HOST_COMPILER=gnu
            export CHPL_TARGET_COMPILER=gnu
            ;;
        *)
            echo "Setting CHPL_HOST_COMPILER and CHPL_TARGET_COMPILER to: ${CC}"
            export CHPL_HOST_COMPILER=$CC
            export CHPL_TARGET_COMPILER=$CC
            ;;
    esac
fi

# Ensure environment is correctly configured to run chpl.
export CHPL_HOME=$(cd "$CWD/../.." ; pwd)
source $CHPL_HOME/util/setchplenv.bash

# Add some space between env setup output and test output.
echo ""

# Show me the environment!
$CHPL_HOME/util/printchplenv --all --no-tidy
echo ""

# Check for tabs.
num_tabs=$($CHPL_HOME/util/devel/lookForTabs | wc -l)
if (( $num_tabs > 0 )) ; then
    echo "Found tabs :-("
    echo $($CHPL_HOME/util/devel/lookForTabs)
    exit 1
fi

# Need to clean up all of the trailing spaces before adding this
# Check for trailing spaces
#num_trailing_space_lines=$($CHPL_HOME/util/devel/lookForTrailingSpaces | wc -l)
#if (( $num_trailing_space_lines > 0 )) ; then
#    echo "Found $num_trailing_space_lines lines with trailing spaces :-("
#    echo $($CHPL_HOME/util/devel/lookForTrailingSpaces)
#    exit 1
#fi

# Check for standard C headers.
standard_c_headers=$($CHPL_HOME/util/devel/grepstdchdrs || :)
if [ -n "${standard_c_headers}" ] ; then
    echo "Standard C headers found in the following file(s):"
    echo "${standard_c_headers}"
    echo "The above list was generated with \$CHPL_HOME/util/devel/grepstdchdrs."
    exit 1
fi

# Ensure nightly does not have syntax errors.
perl -c $CHPL_HOME/util/buildRelease/gen_release
perl -c $CHPL_HOME/util/cron/nightly
perl -c $CHPL_HOME/util/cron/start_opt_test
perl -c $CHPL_HOME/util/tokencount/tokctnightly

# Check copyrights in source files.
$CHPL_HOME/util/test/checkCopyrights.bash

# Check that test script names match their registered config names (for consistency).
config_name_errors=$(grep CHPL_NIGHTLY_TEST_CONFIG_NAME $CHPL_HOME/util/cron/test-*.ba{t,sh} | \
    $CHPL_HOME/util/cron/verify_config_names.py)
if [ -n "${config_name_errors}" ] ; then
    echo ""
    echo "Test script naming errors:"
    echo "${config_name_errors}"
    exit 1
fi

# Source common.bash, which sets up a bunch of environment variables that are
# required for nightly testing.
source $CHPL_HOME/util/cron/common.bash

# Disable GMP and re2 to speed up build.
export CHPL_GMP=none
export CHPL_REGEXP=none

echo ""

# If NIGHTLY_TEST_SETTINGS is set, call make with DEBUG=0 WARNINGS=1 OPTIMIZE=1
# (like nightly testing).
chpl_make_args="-C ${CHPL_HOME}"
if [ "${NIGHTLY_TEST_SETTINGS+x}" = "x" ] ; then
    chpl_make_args="${chpl_make_args} DEBUG=0 WARNINGS=1 OPTIMIZE=1"
fi
echo "Setting these make variables: ${chpl_make_args}"

# Record whether CHPL_DEVELOPER was set during the build to console.
if [ "${CHPL_DEVELOPER+x}" = "x" ] ; then
    echo "CHPL_DEVELOPER is set for this build."
fi

echo ""

# Number of logical processes on current system. Will be used as number of jobs
# when calling make with parallel execution.
num_procs=$($CHPL_HOME/util/buildRelease/chpl-make-cpu_count)

# If make check fails, store the log file in CHPL_HOME. Also, create a temp dir
# in $CHPL_HOME where the hello*.chpl tests are copied and run.
export CHPL_CHECK_INSTALL_DIR=$CHPL_HOME

if [ "${CHPL_SMOKE_SKIP_MAKE_CHECK}" != "true" ] ; then
    # Compile chapel and make sure `make check` works. Compile first with parallel
    # execution, but call `make check` without it.
    make -j${num_procs} $chpl_make_args && \
        CHPL_CHECK_DEBUG=1 make $chpl_make_args check || exit 2
fi

if [ "${CHPL_SMOKE_SKIP_DOC}" != "true" ] ; then
    # Build chpldoc, make sure the chpldoc primer runs, and the docs build.
    make -j${num_procs} $chpl_make_args chpldoc && \
        make $chpl_make_args check-chpldoc && \
        make docs || exit 2
fi
