#!/bin/sh

# Note: the debci infrastructure had problems with this test between March and
# June of 2016.  It consistently detected that the GTML Makefile had not been
# generated.  This doesn't make much sense, because it always works for me in
# each of my test environments, and there are no errors reported in the debci
# build log prior to the supposedly-missing file.  Starting with version
# 3.5.4-17, I've added a lot of extra diagnostic information, in an attempt
# to debug the problem. That makes this script and the output a little more
# cluttered than usual for my other packages, but I can't figure out any other
# way to get a handle on what's different in the debci build environment.

# Make sure $ADTTMP is available
if [ "${ADTTMP}"F = "F" ]; then
   echo "Error: expected environment variable ADTTMP is not set."
   exit 1
fi

# Make sure $ADT_ARTIFACTS is available
if [ "${ADT_ARTIFACTS}"F = "F" ]; then
   echo "Error: expected environment variable ADT_ARTIFACTS is not set."
   exit 1
fi

# Determine the test execution mode 
if [ "${1}"F = "F" ]; then
   MODE="autopkgtest"
elif [ "${1}" = "-r" ]; then
   MODE="debian/rules"
else 
   echo "usage: $0 [-r]\n-r Run in debian/rules mode instead of autopkgtest mode"
   exit 2
fi

# Determine locations of required tools
SOURCE_TREE="${PWD}"
if [ "${MODE}" = "debian/rules" ]; then
   # In debian/rules mode, the executables are assumed to be in the source tree
   GTML="${SOURCE_TREE}/gtml"
else
   # In autopkgtest mode, the executables are assumed to be installed
   GTML="/usr/bin/gtml"
fi

# Print a test summary
echo ""
echo "========================================================================="
echo "Running ${0} in mode: ${MODE}"
echo "========================================================================="
echo "SOURCE_TREE..: ${SOURCE_TREE}"
echo "ADTTMP.......: ${ADTTMP}"
echo "ADT_ARTIFACTS: ${ADT_ARTIFACTS}"
echo "GTML.........: ${GTML}"
echo "========================================================================="
echo ""

# Print out the executing user, for reference
id 

# Check that the source tree exists
if [ ! -d "${SOURCE_TREE}" ]; then
   echo "Error: SOURCE_TREE does not exist: ${SOURCE_TREE}"
   exit 1
fi
ls -ld ${SOURCE_TREE}
ls -l ${SOURCE_TREE}

# Check that the ADTTMP directory exists
if [ ! -d "${ADTTMP}" ]; then
   echo "Error: ADTTMP does not exist: ${ADTTMP}"
   exit 1
fi
ls -ld ${ADTTMP}
ls -l ${ADTTMP}

# Check that the ADT_ARTIFACTS directory exists
if [ ! -d "${ADT_ARTIFACTS}" ]; then
   echo "Error: ADT_ARTIFACTS does not exist: ${ADT_ARTIFACTS}"
   exit 1
fi
ls -ld ${ADT_ARTIFACTS}
ls -l ${ADT_ARTIFACTS}

# Check that the GTML script exists and is executable
if [ ! -r "${GTML}" ]; then
   echo "Error: GTML does not exist: ${GTML}"
   exit 1
elif [ ! -x "${GTML}" ]; then
   echo "Error: GTML is not executable: ${GTML}"
   ls -l ${GTML}
   exit 1
fi
ls -l ${GTML}

# Always run tests from within $ADTTMP
cd ${ADTTMP}

# Set up some file and directory locations 
PROJECT="${ADTTMP}/project"
CONFIG="${ADTTMP}/config"
SOURCE="${ADTTMP}/source"
GTP="${PROJECT}/project.gtp"
MAKEFILE="${ADTTMP}/Makefile"

# Copy in the project directory that we'll build
echo "Creating project in ${PROJECT}..."
echo "mkdir -p ${PROJECT}"
mkdir -p ${PROJECT}
if [ ! -d "${PROJECT}" ]; then
   echo "Error: failed to create ${PROJECT}"
   exit 1
fi
ls -ld ${PROJECT}
ls -l ${PROJECT}

echo "cp -r ${SOURCE_TREE}/debian/tests/data/project/config ${CONFIG}"
cp -r ${SOURCE_TREE}/debian/tests/data/project/config ${CONFIG}
if [ ! -d "${CONFIG}" ]; then
   echo "Error: failed to create ${CONFIG}"
   exit 1
fi
ls -ld ${CONFIG}
ls -l ${CONFIG}

echo "cp -r ${SOURCE_TREE}/debian/tests/data/project/source ${SOURCE}"
cp -r ${SOURCE_TREE}/debian/tests/data/project/source ${SOURCE}
if [ ! -d "${SOURCE}" ]; then
   echo "Error: failed to create ${SOURCE}"
   exit 1
fi
ls -ld ${SOURCE}
ls -l ${SOURCE}

# Create the project.gtp file, which requires an absolute include path
echo "Creating GTP file: ${GTP}..."
rm -f ${GTP}
echo "define INCLUDE_PATH ${CONFIG}" >> ${GTP}
echo "allsource" >> ${GTP}
if [ ! -r "${GTP}" ]; then
   echo "Error: failed to create ${GTP}"
   exit 1
fi
cat ${GTP}

# Create the GTML makefile
echo "Creating Makefile: ${MAKEFILE}..."
echo "${GTML} -M${MAKEFILE} ${GTP}"
${GTML} -M${MAKEFILE} ${GTP} 2>&1
if [ $? != 0 ]; then
   echo "Error: failed to create GTML makefile."
   exit 1
fi

# Check that the GTML makefile exists as expected
echo "Checking for existence of generated Makefile..."
if [ ! -r ${MAKEFILE} ]; then
   echo "Error: GTML apparently did not generate Makefile"
   ls -ld ${ADTTMP}
   ls -l ${ADTTMP}
   exit 1
fi
ls -l ${MAKEFILE}

# Remove the GTML variable so we can use our own path later
echo "Tweaking generated Makefile for test purposes..."
sed -i '/GTML = gtml/d' ${MAKEFILE} 
ls -l ${MAKEFILE}
cat ${MAKEFILE}

# Build the site
echo "Making the project...."
echo "GTML=${GTML} /usr/bin/make"
GTML=${GTML} /usr/bin/make 2>&1
if [ $? != 0 ]; then
   echo "GTML build failed."
   exit 1
fi

# Check that every expected file exists and matches expectations
echo "Confirming expected results..."
for EXPECTED in ${SOURCE_TREE}/debian/tests/data/project/expected/*.html; do
   ACTUAL="${SOURCE}/`basename ${EXPECTED}`"
   echo "diff -Naur ${EXPECTED} ${ACTUAL}"
   /usr/bin/diff -Naur ${EXPECTED} ${ACTUAL}
   if [ $? != 0 ]; then
      echo "Generated result differs."
      exit 1
   fi
done
echo "Actual results match expected results... success!"

# Close the test
echo ""

