#!/bin/bash
#
# Copyright (C) 2012-2015 SUSE Linux GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Testing DAPS: online-docs
#
#
# * Does online-docs correctly build?
# * Is the bigfile generated?
# * Is the image tarball generated? Does it include all images?
# * Are all output formats created?
# * Does the --rootid option work correctly?
# * DOes the --noset option work correctly?
# * Is the --name option correctly implemented?
# * Does the --export-dir function work correctly?
# * Does it work when draft mode is set in the DC file?
#

_OD_NAME_DIR=""
_OD_NAME_BIGFILE=""
_OD_TEMPDIR=""
_LOGFILE=""

source lib/common_functions

header "online-docs"

function oneTimeSetUp() {
    local _LOG_DIR

    # Clean up the build directory
    clean_build_dir all

    _OD_NAME_DIR=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=OD_EXPORT_BOOKDIR 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the path to the resulting locdrop directory failed. Skipping tests"
    fi

    _OD_NAME_BIGFILE=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=OD_BIGFILE 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the path to the resulting bigfile 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_online-docs.log

    # create temp dir
    #
    make_tempdir "_OD_TEMPDIR"
    _OD_TEMPDIR="${_OD_TEMPDIR}/onlinedocs"
    mkdir -p "$_OD_TEMPDIR" || exit_on_error " Creating a temporary directory failed. Skipping tests"

}

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

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

#--------------------------------
# *  * Does online-docs correctly build?
# * Is the bigfile generated?
#
function test_onlinedocs () {
    local _OD_BUILD_PATH

    _OD_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE online-docs --dbnoconv 2>/dev/null)
    assertTrue \
        ' └─ The online-docs command itself failed' \
        "$?"
    assertTrue \
	" └─ The resulting file (${_OD_BUILD_PATH}) does not exist." \
	"[ -d $_OD_BUILD_PATH ]"
    assertTrue \
        " └─ The bigfile ($_OD_NAME_BIGFILE) does not exist or has size 0" \
        "[[ -s $_OD_NAME_BIGFILE ]]"
}

#--------------------------------
# * Is the image tarball generated? Does it include all images?
#
function test_onlinedocsImages () {
    local _FILE _IMAGE _MISSING _OD_NAME_IMG_TAR
    
    _OD_NAME_IMG_TAR=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=OD_GRAPHICS 2>/dev/null)
    assertTrue \
        ' └─ The command to determine the name of the images tarball failed' \
        "$?"
    # clean tmp and unpack new archive
    [[ -d "${_OD_TEMPDIR}" ]] && rm -rf ${_OD_TEMPDIR}/*
    (cd $_OD_TEMPDIR; tar xfj $_OD_NAME_IMG_TAR)
    for _FILE in ${_SET_IMAGES[*]}; do
        _IMAGE=$(basename $_FILE)
        _IMAGE=${_IMAGE%.*}
        readlink -es ${_OD_TEMPDIR}/images/src/*/${_IMAGE}.* >/dev/null || _MISSING="$_MISSING $_FILE"
    done
    assertNull \
        " └─ File(s) '$_MISSING' is/are missing in the images tarball" \
        "$_MISSING"
}

#--------------------------------
# * Is the PDF generated?
#
function test_onlinedocsPDF () {
    local _OD_NAME_PDF
    
    _OD_NAME_PDF=$(basename $($_DAPSEXEC -v0 -d $_DCFILE pdf-name 2>/dev/null))
    assertTrue \
        ' └─ The command to determine the name of the PDF file failed' \
        "$?"
    assertTrue \
        " └─ The PDF file (${_OD_NAME_PDF})does not exist" \
        "[[ -s ${_OD_NAME_DIR}/${_OD_NAME_PDF} ]]"
}

#--------------------------------
# * Is the EPUB generated?
#
function test_onlinedocsEPUB () {

    local _OD_NAME_EPUB
    
    _OD_NAME_EPUB=$(basename $($_DAPSEXEC -v0 -d $_DCFILE epub-name 2>/dev/null))
    assertTrue \
        ' └─ The command to determine the name of the EPUB file failed' \
        "$?"
    assertTrue \
        " └─ The EPUB file (${_OD_NAME_EPUB}) does not exist" \
        "[[ -s ${_OD_NAME_DIR}/${_OD_NAME_EPUB} ]]"
}

#--------------------------------
# * Is the single-html tarball generated?
#
function test_onlinedocsHTML () {
    
    local _OD_NAME_HTML
    
    # fs 2013-11-24: Currently there is no way to determine the
    # names the files resulting from package-pdf and package-html
    # This needs to be fixed
    #
    _OD_NAME_HTML=${_BOOKNAME}_en-single-html.tar.bz2
    assertTrue \
        " └─ The single-html tarball (${_OD_NAME_HTML}) does not exist" \
        "[[ -s ${_OD_NAME_DIR}/${_OD_NAME_HTML} ]]"
  
}

#--------------------------------
# * Does the --rootid option work correctly?
# * Does the --noset option work correctly?

function test_onlinedocsRootidNoset () {

    local _OD_BUILD_PATH
    _ROOTID=appendix

    clean_build_dir results

    $_DAPSEXEC -v0 -d $_DCFILE --debug online-docs --rootid "$_ROOTID" --noset --dbnoconv >/dev/null 2>&1
    assertTrue \
        ' └─ Building the the online-docs with --rootid failed.' \
        "$?"
    egrep -q -- "--stringparam\s+\"rootid=$_ROOTID\"" $_LOGFILE 2>/dev/null
    assertTrue \
	' └─ Stringparam for ROOTID is not correctly specified when generating online-docs' \
	"$?"
    assertTrue \
	' └─ The --noset option does not work correctly' \
	"[ -s ${_OD_NAME_DIR}/${_ROOTID}_en.xml ]"
}

#--------------------------------
# * Is the --name option correctly implemented?

function test_onlinedocsName () {

    local _OD_SUBDIR_NAME _NAME _OD_BUILD_PATH

    _NAME="testsuite"

    clean_build_dir results

    _OD_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE online-docs --name $_NAME --dbnoconv 2>/dev/null)
    assertTrue \
        ' └─ Building online-docs with --name failed' \
        "$?"

    _OD_SUBDIR_NAME=$(basename $(dirname $_OD_BUILD_PATH))
    assertEquals \
	" └─ The resulting directory name does not match the string specified with --name." \
	"$_NAME" "$_OD_SUBDIR_NAME"
}

#--------------------------------
# * Does the --export-dir function work correctly?

function test_onlinedocsExportdir () {
    local _BIGFILE _EXPORT_DIR _OD_BUILD_PATH
    local _DOCNAME

    _BIGFILE=$(basename $_OD_NAME_BIGFILE)

    _EXPORT_DIR="$_OD_TEMPDIR"
    rm -rf "$_EXPORT_DIR" && mkdir -p "$_EXPORT_DIR"

    clean_build_dir results

    _OD_BUILD_PATH=$($_DAPSEXEC -d $_DCFILE online-docs --export-dir $_EXPORT_DIR --dbnoconv 2>/dev/null)
    assertTrue \
        ' └─ Building the online-docs with --export-dir failed' \
        "$?"
    assertTrue \
        ' └─ The bigfile is not generated in the export directory' \
        "[ -s ${_OD_BUILD_PATH}/$_BIGFILE ]"
}

#--------------------------------
# * Does it work when draft mode is set in the DC file?

function test_onlinedocsDraft () {

    local _DCFILE_DRAFT
    _DCFILE_DRAFT="${_DOC_DIR}/DC-booktest_draft"

    $_DAPSEXEC -d $_DCFILE_DRAFT online-docs --dbnoconv >/dev/null 2>&1
    assertTrue \
        ' └─ Building online docs with DRAFT="yes" in the DC-file failed.' \
        "$?"
}

# source shUnit2 test
source $_SHUNIT2SRC
