#!/bin/bash
#
# Copyright (C) 2012-2015 SUSE Linux GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Testing DAPS: PDF
#
# In a PDF it's impossible to find out whether e.g. a certain XSLTPARAM
# option or the draft mode is working without actually opening the PDF
# in a viewer and looking at it.
# For testing we will therefore run DAPS in debug mode and grep for parameters
# in the log file. Thus we will at least know whether the xslt processor
# was called with correct options.
#
# * Does the PDF-file correctly build?
# * Does the name retrieved with *-name match the actual result?
# * Is the .fo still present after the PDF has been built?
# * Do the --styleroot and --fb_styleroot options work?
# * Does the automatic fallback to the DoBook stylesheets work?
# * Is the --grayscale option corrctly supported?
# * Is the --cropmarks option corrctly supported (XEP only)?
# * Is the draft watermark image included when using the --draft option?
# * Are remarks displayed when using the --remarks option?
# * Is the meta information displayed when using the --meta option?
# * Is the --name option correctly implemented?
# * Does the --rootid option work correctly?
# * Are parameters pass with --xsltparam correctly processed?

_PDF_NAME_PATH=""
_LOGFILE=""

source lib/common_functions

header "pdf with $_FOPROC"

function oneTimeSetUp() {
    local _LOG_DIR

    # Clean up the build directory
    clean_build_dir all
    # get the profiling directory
    _PDF_NAME_PATH=$($_DAPSEXEC -d $_DCFILE pdf-name 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the path to the resulting PDF failed. Skipping tests"
    fi
    _LOG_DIR=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=LOG_DIR 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the LOG file failed. Skipping tests"
    fi
    _LOGFILE=${_LOG_DIR}/make_pdf.log
}

# Post
# this function is run _after_ the tests are executed
#
function oneTimeTearDown() {
    stats
    # Clean up the build directory
    clean_build_dir all
}

#---------------------------------------------------------------
# TESTS
#---------------------------------------------------------------

#--------------------------------
# * Does the PDF-file correctly build?
# * Does the name retrieved with *-name match the actual result?
#
function test_pdf () {
    local _PDF_BUILD_PATH
    _PDF_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE pdf --formatter=$_FOPROC 2>/dev/null)
    assertTrue \
        ' └─ The pdf command itself failed' \
        "$?"
    assertTrue \
	" └─ The resulting filename (${_PDF_BUILD_PATH}) does not exist" \
	"[ -s $_PDF_BUILD_PATH ]"
    assertEquals \
	' └─ The resulting filename does not match the one retrieved with pdf-name' \
	"$_PDF_NAME_PATH" "$_PDF_BUILD_PATH"
}
#--------------------------------
# * Is the .fo still present after the PDF has been built?
#
function test_pdfFO () {
    local _FO_FILENAME _FO_TMPDIR
    _FO_TMPDIR=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=TMP_DIR 2>/dev/null)
   assertTrue \
       ' └─ Getting the value of the TMP_DIR variable failed' \
       "$?"
    _FO_FILENAME=$(basename ${_PDF_NAME_PATH} 2>/dev/null)
    if [[ "pdf" == $_PDFCMD ]]; then
	_FO_FILENAME=${_FO_TMPDIR}/${_FO_FILENAME%-print_en.pdf}-${_FOPROC}-print_en.fo
    else
	_FO_FILENAME=${_FO_TMPDIR}/${_FO_FILENAME%_en.pdf}-${_FOPROC}_en.fo
    fi
    assertTrue \
	" └─ The FO file (${_FO_FILENAME}) does not exist anymore after having built the PDF" \
	"[ -s $_FONAME ]"
}

#--------------------------------
# * Do the --styleroot and --fb_styleroot options work?
# * Does the automatic fallback to the DoBook stylesheets work?
#
function test_pdfStyleroot () {

     clean_build_dir fo

    # Testing Fallback Styleroot by specifying a wrong Styleroot dir
    #

    $_DAPSEXEC -d $_DCFILE --styleroot=${_DOC_DIR} --fb_styleroot=${_STANDARD_STYLES} --debug pdf >/dev/null 2>&1
    egrep -q -- "--stylesheet /.*/${_STANDARD_STYLES}" $_LOGFILE 2>/dev/null
    assertTrue \
	" └─ The Fallback Styleroot specified on the command line was not used." \
	"$?"

    clean_build_dir fo

    # Testing automatic Fallback to _DB_STYLES by specifying a wrong
    # Styleroot dir with no Fallback
    #
    $_DAPSEXEC -d $_DCFILE --styleroot=${_DOC_DIR} --debug pdf >/dev/null 2>&1
    grep -q -- "--stylesheet ${_DB_STYLES}" $_LOGFILE 2>/dev/null
    assertTrue \
	" └─ The automatic DocBook Fallback Styleroot was not used." \
	"$?"

    clean_build_dir fo

    # Testing Styleroot specified with --styleroot
    #
    $_DAPSEXEC -d $_DCFILE --styleroot ${_STANDARD_STYLES} --debug pdf >/dev/null 2>&1
    egrep -q -- "--stylesheet /.*/${_STANDARD_STYLES}" $_LOGFILE 2>/dev/null
    assertTrue \
	" └─ The Styleroot specified on the command line was not used." \
	"$?"
}

#--------------------------------
# * Is the --grayscale option corrctly supported?
# * Is the --cropmarks option corrctly supported (XEP only)?

function test_pdfGrayscaleCropmarks () {
    # There is no (easy) way to tell whether a PDF has grayscale images, so
    # we just check whether it correctly builds and whether grayscale images
    # have been created
    local _IMGDIR _PDF_NAME_PATH _PAGE_SIZE_BUILD _PAGE_SIZE_EXPECTED

    clean_build_dir fo

    # If built with cropmarks, the pagesite in the PDF is larger (than A4) 
    _PAGE_SIZE_EXPECTED="643.276x889.89"

    _PDF_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE --debug pdf --grayscale --cropmarks --formatter=$_FOPROC 2>/dev/null | tail -n 1)
   assertTrue \
       ' └─ Building the PDF with --grayscale --cropmarks failed' \
       "$?"
    assertTrue \
	" └─ The resulting file (${_PDF_BUILD_PATH}) does not exist" \
	"[ -s $_PDF_BUILD_PATH ]"

   _PDF_NAME_PATH=$($_DAPSEXEC -v0 -d $_DCFILE pdf-name --grayscale --cropmarks --formatter=$_FOPROC 2>/dev/null)
   assertTrue \
       ' └─ Getting the PDF-name with --grayscale --cropmarks failed' \
       "$?"
   assertEquals \
       " └─ The resulting filename does not match the one retrieved with pdf-name: " \
       "$_PDF_NAME_PATH" "$_PDF_BUILD_PATH"

   # Check for the format.print parameter
   egrep -q -- "--param\s+\"format\.print=1\"" $_LOGFILE 2>/dev/null
   assertTrue \
       ' └─ Param format.print=1 is not specified when generating the FO-file' \
       "$?"

    # Check for grayscale images (just check whether files have been created)
   _IMGDIR=$($_DAPSEXEC -d $_DCFILE showvariable VARIABLE=IMG_GENDIR 2>/dev/null)
    assertTrue \
	' └─ Getting the image directory name failed' \
	"$?"
    assertTrue \
	" └─ No grayscale images have been generated in ${_IMGDIR}/grayscale" \
	"[ -n \"$(ls -A ${_IMGDIR}/grayscale 2>/dev/null)\" ]"

    # Check cropmarks (for XEP only)
    [[ fop == $_FOPROC ]] && startSkipping

    egrep -q -- "--param\s+\"crop\.marks=1\"" $_LOGFILE 2>/dev/null
    assertTrue \
       ' └─ Param crop.marks=1 is not specified when generating the FO-file' \
       "$?"   

    _PAGE_SIZE_BUILD=$(pdfinfo $_PDF_BUILD_PATH 2>/dev/null | awk '/^Page size:/ {print $3 $4 $5}')
    assertEquals \
	' └─ PDF seems to be build without cropmarks' \
	"$_PDF_SIZE_EXPECTED" "$_PDF_SIZE_BUILD"

    endSkipping

}
#--------------------------------
# * Is the draft watermark image included when using the --draft option?
# * Are remarks displayed when using the --remarks option?
# * Is the meta information displayed when using the --meta option?
#
function test_pdfRemarksDraftMeta () {
    local _PDF_BUILD_PATH _PDF_NAME_PATH _REMARK_COUNT_ACTUAL
    local  _REMARK_COUNT_EXPECTED
    declare -a _XMLFILES

   _REMARK_COUNT_ACTUAL=0
   _REMARK_COUNT_EXPECTED=0
   _XML_FILES=( $_SET_FILES )

    clean_build_dir fo

   _PDF_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE --debug pdf --draft --remarks --meta --formatter=$_FOPROC 2>/dev/null | tail -n 1)
   assertTrue \
       ' └─ Building the PDF with --draft --remarks --meta failed' \
       "$?"

   _PDF_NAME_PATH=$($_DAPSEXEC -v0 -d $_DCFILE pdf-name --draft --remarks --meta 2>/dev/null)
   assertTrue \
       ' └─ Getting the PDF-name with --draft --remarks --meta failed' \
       "$?"
   assertEquals \
       " └─ The resulting filename does not match the one retrieved with pdf-name: " \
       "$_PDF_NAME_PATH" "$_PDF_BUILD_PATH"

   # checking DRAFT mode
   #
   egrep -q -- "--stringparam\s+\"draft\.mode=yes\"" $_LOGFILE 2>/dev/null
   assertTrue \
       ' └─ Stringparam for DRAFT mode is not correctly specified when generating the FO-file' \
       "$?"
   expr "$_PDF_BUILD_PATH" : '.*\(_draft\)' >/dev/null 2>&1
   assertTrue \
       ' └─ String "_draft" does not appear in the PDF filename' \
       "$?"

   # checking META
   #
   # fs 2015-04-16: Commenting since this is SVN dependent and we no longer
   # use SVN


#   egrep -q -- "--param\s+\"use\.meta=1\"" $_LOGFILE 2>/dev/null
#   assertTrue \
#       ' └─ Param for META is not correctly specified when generating the FO-file' \
#       "$?"
#   expr "$_PDF_BUILD_PATH" : '.*\(_meta\)' >/dev/null 2>&1
#   assertTrue \
#       ' └─ String "_meta" does not appear in the PDF filename' \
#       "$?"


   # checking REMARKS
   # --param "show.comments=1" needs to occur once for each XML file on
   # profiling and another time when creating the fo-file
   #
   _REMARK_COUNT_ACTUAL=$(egrep -c -- "--param\s+\"show\.comments=1\"" $_LOGFILE)
   let "_REMARK_COUNT_EXPECTED=${#_XML_FILES[@]} + 1"
   assertEquals \
       " └─ --param \"show.comments=1\" appears less often in the log file than expected" \
       "$_REMARK_COUNT_EXPECTED" "$_REMARK_COUNT_ACTUAL"

   expr "$_PDF_BUILD_PATH" : '.*\(_remarks\)' >/dev/null
   assertTrue \
       ' └─ String "_remarks" does not appear in the PDF filename' \
       "$?"
}

#--------------------------------
# * Is the --name option correctly implemented?
#
function test_pdfName () {
    local _BUILD_SUBDIR_NAME _NAME _PDF_BUILD_PATH _PDF_NAME_PATH
    _NAME="testsuite"

    clean_build_dir fo

    _PDF_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE pdf --name $_NAME --formatter=$_FOPROC 2>/dev/null)
    assertTrue \
	' └─ Building a pdf with --name failed ' \
       "$?"

   _PDF_NAME_PATH=$($_DAPSEXEC -v0 -d $_DCFILE pdf-name --name $_NAME 2>/dev/null)
   assertTrue \
	' └─  Getting the filename for pdf with --name failed ' \
       "$?"

   assertEquals \
       " └─ The resulting filename does not match the one retrieved with pdf-name: " \
       "$_PDF_NAME_PATH" "$_PDF_BUILD_PATH"

   # expr match does not work with Variables as search term, needs regexp

   _BUILD_SUBDIR_NAME=$(basename $(dirname "$_PDF_BUILD_PATH"))
   echo "$(basename $_PDF_BUILD_PATH)" | grep -q $_NAME
   assertTrue \
       " └─ String passed with --name ($_NAME) does not appear in the PDF filename" \
       "$?"
   assertEquals \
       " └─ The build subdirectory does not have the name supplied with --name: " \
       "$_NAME" "$_BUILD_SUBDIR_NAME"
}


#--------------------------------
# * Does the --rootid option work correctly?
# * Are parameters pass with --xsltparam correctly processed?
#
function test_pdfRootidXsltparam () {
    local _PAGECOUNT _PAGECOUNT_EXPECTED _PDF_FILENAME_EXPECTED
    local _PDF_FILENAME_BUILD _PDF_BUILD_PATH _ROOTID _XSLTPARAM


    _ROOTID=appendix
    _PAGECOUNT_EXPECTED=1

    _PDF_FILENAME_EXPECTED=${_ROOTID}_color_en.pdf

    _XSLTPARAM="'--param hyphenate.verbatim=0'"

    clean_build_dir fo

    _PDF_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE --debug pdf --formatter=$_FOPROC --rootid $_ROOTID --xsltparam="$_XSLTPARAM" 2>/dev/null | tail -n 1)
    assertTrue \
	' └─ Building a pdf with --rootid and --xsltparam failed ' \
       "$?"
    egrep -q -- "--stringparam\s+\"rootid=$_ROOTID\"" $_LOGFILE 2>/dev/null
    assertTrue \
	' └─ Stringparam for ROOTID is not correctly specified when generating the FO-file' \
	"$?"

    _PDF_FILENAME_BUILD=$(basename $_PDF_BUILD_PATH)
    assertEquals \
	' └─ The resulting PDF filename does not match the ROOTID:' \
	"$_PDF_FILENAME_EXPECTED" "$_PDF_FILENAME_BUILD"

    # check PDF page count
    _PAGECOUNT=$(pdfinfo $_PDF_BUILD_PATH | grep "Pages:" | sed 's/.*:  *//' 2>/dev/null)
    assertEquals \
	' └─ PDF has wrong page count:' \
	"$_PAGECOUNT_EXPECTED" "$_PAGECOUNT"

    # check xsltparam
    egrep -q -- "$_XSLTPARAM" $_LOGFILE 2>/dev/null
    assertTrue \
	' └─ Param for XSLTPARAM is not correctly specified when generating the FO-file' \
	"$?"
}

# source shUnit2 test
source $_SHUNIT2SRC
