#!/bin/bash
#
# Copyright (C) 2012-2015 SUSE Linux GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Testing DAPS: EPUB
#
#
# * Does EPUB correctly build?
# * Does the name retrieved with epub-name match the actual result?
# * Does the EPUB file validate with epubcheck?
# * Does the --css switch work?
# * Does the --check switch validate the epub file?
# * Is the --name option correctly implemented?
# * Does the --rootid option work correctly?
# * Do the --styleroot and --fb_styleroot options work?

_EPUB_NAME_PATH=""
_LOGFILE=""

source lib/common_functions

header "ePUB"

function oneTimeSetUp() {
    local _LOG_DIR

    # Clean up the build directory
    clean_build_dir all
    # get the profiling directory
    _EPUB_NAME_PATH=$($_DAPSEXEC -d $_DCFILE epub-name 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the path to the resulting ePUB 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_epub.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 EPUB correctly build?
# * Does the name retrieved with epub-name match the actual result?
#
function test_epub () {
    local _EPUB_BUILD_PATH
    _EPUB_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE epub 2>/dev/null)
    assertTrue \
        ' └─ The epub command itself failed' \
        "$?"
    assertTrue \
	" └─ The resulting file (${_EPUB_BUILD_PATH}) does not exist." \
	"[ -s $_EPUB_BUILD_PATH ]"
    assertEquals \
	" └─ The resulting filename does not match the one retrieved with --epub-name" \
	"$_EPUB_NAME_PATH" "$_EPUB_BUILD_PATH"
}

#--------------------------------
# * Does the EPUB file validate with epubcheck?

function test_epubValidate () {
    local CHECK_MESSAGE
    CHECK_MESSAGE=$(epubcheck $_EPUB_NAME_PATH)
    assertTrue \
        " └─ The generated ePUB file does not validate:\n;;;;;;;;;;\n${CHECK_MESSAGE}\n;;;;;;;;;;" \
        "$?"
}

#--------------------------------
# * Does the --check switch validate the epub file?
#

function test_epubCheck () {
    $_DAPSEXEC -d $_DCFILE epub --check >/dev/null 2>&1
    assertTrue \
        ' └─ The epub command with --check failed' \
        "$?"
    # Test for epubcheck
    #
    grep -iq "epubcheck" $_LOGFILE 2>/dev/null
    assertTrue \
        " └─ The ePUB was not checked with epubcheck" \
        "$?"
}

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

    clean_build_dir tmp

    # Testing Fallback Styleroot by specifying a wrong Styleroot dir
    #

    $_DAPSEXEC -d $_DCFILE --styleroot=${_DOC_DIR} --fb_styleroot=${_STANDARD_STYLES} --debug epub >/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 tmp

    # Testing automatic Fallback to _DB_STYLES by specifying a wrong
    # Styleroot dir with no Fallback
    #
    $_DAPSEXEC -d $_DCFILE --styleroot=${_DOC_DIR} --debug epub >/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 tmp

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


#--------------------------------
# * Does the --css switch work?
#

function test_epubCSS () {
    local _CSS_CMDL_NAME _CSS_CMDL_PATH _CSS_DC_NAME _CSS_STYLE_NAME
    local _EPUB_BUILD_PATH

    _CSS_CMDL_NAME="test.css"
    _CSS_CMDL_PATH="${_DOC_DIR}/xml/${_CSS_CMDL_NAME}"
    _CSS_DC_NAME="reference.css" # from $_DCFILE
    _CSS_STYLE_NAME="style.css"

    # Does the EPUB contain the css file specified in the DC file?
    #

    clean_build_dir tmp

    _EPUB_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE epub 2>/dev/null)

    unzip -l $_EPUB_BUILD_PATH 2>/dev/null | grep "$_CSS_DC_NAME" >/dev/null 2>&1
    assertTrue \
        " └─ The resulting ePUB does not include the CSS file specifed in $_DCFILE ($_CSS_DC_NAME)." \
        "$?"

    # Does the EPUB contain the default CSS file from the stylesheet
    # directory when no other CSS file is specified?
    #
    # fs 2015-04-17: Not sure whether we really should use a CSS from STYLEIMG
    # (not in the code anymore). Explicitly specifying the CSS file seems to
    # be the better solution. Therefore commenting this test

#    clean_build_dir tmp
#
#    _EPUB_BUILD_PATH=$($_DAPSEXEC --main $_MAINPATH --styleroot ${_STANDARD_STYLES} epub 2>/dev/null)
#    unzip -l $_EPUB_BUILD_PATH 2>/dev/null | grep "$_CSS_STYLE_NAME" >/dev/null 2>&1
#    assertTrue \
#        " └─ The resulting ePUB does not include the CSS file from the stylesheet directory ($_CSS_STYLE_NAME)." \
#        "$?"

    # Does the EPUB contain the css file specified on the command line?
    #

    clean_build_dir tmp

    _EPUB_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE epub --css=${_CSS_CMDL_PATH} 2>/dev/null)

    unzip -l $_EPUB_BUILD_PATH 2>/dev/null | grep "$_CSS_CMDL_NAME" >/dev/null 2>&1
    assertTrue \
        " └─ The resulting ePUB does not include the CSS file specified on the command line ($_CSS_CMDL_NAME)." \
        "$?"

    # Does the EPUB contain no css file with --css=none?
    #

    clean_build_dir tmp

    _EPUB_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE epub --css=none 2>/dev/null)

    unzip -l $_EPUB_BUILD_PATH 2>/dev/null | grep "\.css" >/dev/null 2>&1
    assertFalse \
        " └─ The resulting ePUB includes a CSS file, although --css=none ." \
        "$?"
}


#--------------------------------
# * Is the --name option correctly implemented?
#
function test_epubNAME () {
    local _BUILD_SUBDIR_NAME _EPUB_BUILD_PATH _EPUB_NAME_PATH _NAME
    _NAME="testsuite"

    clean_build_dir tmp

    _EPUB_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE epub --name $_NAME 2>/dev/null)
    assertTrue \
	" └─ Building an EPUB document with --name failed " \
       "$?"

    _EPUB_NAME_PATH=$($_DAPSEXEC -v0 -d $_DCFILE epub-name --name $_NAME 2>/dev/null)
   assertTrue \
	' └─  Getting the filename for EPUB with --name failed ' \
       "$?"
   assertEquals \
       " └─ The resulting filename does not match the one retrieved with --epub-name: " \
       "$_EPUB_NAME_PATH" "$_EPUB_BUILD_PATH"

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

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


}

#--------------------------------
# * Does the --rootid option work correctly?
#
function test_epubRootid () {
    local _EPUB_BUILD_PATH _EPUB_FILENAME_BUILD _EPUB_FILENAME_EXPECTED
    local _ROOTID

    _ROOTID=appendix
    _EPUB_FILENAME_EXPECTED=${_ROOTID}_en.epub

    clean_build_dir tmp

    _EPUB_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE --debug epub --rootid $_ROOTID 2>/dev/null | tail -n 1)
    assertTrue \
	' └─ Building an EPUB with --rootid failed ' \
       "$?"
    egrep -q -- "--stringparam\s+\"rootid=${_ROOTID}\"" $_LOGFILE 2>/dev/null
    assertTrue \
	' └─ Stringparam for ROOTID is not correctly specified when generating the EPUB-file' \
	"$?"
    _EPUB_FILENAME_BUILD=$(basename $_EPUB_BUILD_PATH)
    assertEquals \
	' └─ The resulting EPUB filename does not match the ROOTID:' \
	"$_EPUB_FILENAME_EXPECTED" "$_EPUB_FILENAME_BUILD"
}

# source shUnit2 test
source $_SHUNIT2SRC
