#!/bin/bash

# Copyright (C) 2009-2010 Nokia Corporation. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


function usage() {
    spcr=$( echo "${SELF}" | sed 's/./ /g' )
    cat <<EOF
MaemoDoc toolchain version ${TOOLCHAIN_VERSION} (c) 2009-2010 Nokia Corporation.

Usage:
        ${SELF}  [ --docpath <document_tree_path> ]  
        ${spcr}  [ --out <output_path> ]
EOF
    exit 0
}

. $( dirname ${0} )/maemodoc-common

if [ ${COMMON_INCLUDED} != ${YES} ] ; then
    echo "${0}: $( dirname ${0} )/maemodoc-common not found."
    exit 42
fi


## Main
SCRIPT_PATH=$( dirname ${0} )
parse_args menu ${*}

allow_unset
[ -z "${PARM_DOC_PATH}" ] || PARMS="${PARMS} --docpath ${PARM_DOC_PATH}"
[ -z "${PARM_REL_PATH}" ] || PARMS="${PARMS} --out ${PARM_REL_PATH}"
restore_nounset


# Select action

exec 3>&1
mode=$( whiptail \
    --title "Maemo documentation" \
    --menu "Select action:" 20 76 12 \
    p "Generate PDF" \
    h "Generate HTML" \
    b "Generate PDF and HTML" \
    w "Generate HTML and wikify" \
    2>&1 1>&3 )
retval=${?}
exec 3>&-

if [ ${retval} -eq 1 ] || [ ${retval} -eq 255 ] ; then
    echo "Cancelled"
    exit 0
fi



# Select document

check_paths

docpath=${DOC_ORIG_PATH}
skelpath="${docpath}/skeletons"
docpath_sed=$( echo "${docpath}" | sed 's/\//\\\//g' )
skelpath_sed=$( echo "${skelpath}" | sed 's/\//\\\//g' )

for doctex in $(
    find ${docpath} -maxdepth 1 -type f -name '*.tex' \
	| sed "s/${docpath_sed}\/?//" ) ; do
    if find ${skelpath} \
	-maxdepth 1 \
	-type f \
	-wholename "${doctex/.tex/-skeleton.tex}" ; then
	documents="${documents} $( basename ${doctex%.tex} )"
    fi
done	    

document_menu=$(
    i=0
    for doc in ${documents} ; do
	echo "${doc} ${i}"
	(( i++ ))
    done
    )

exec 3>&1
document=$( whiptail \
    --title "Maemo documentation" \
    --menu "Select the document:" 20 76 12 \
    ${document_menu} \
    2>&1 1>&3 )
retval=${?}
exec 3>&-

if [ ${retval} -eq 1 ] || [ ${retval} -eq 255 ] ; then
    echo "Cancelled"
    exit 0
fi


# Select chapters

TEX="${DOC_ORIG_PATH}/${document}.tex"
get_chapter_list >/dev/null

chapter_menu=$(
    state="off"
    [ ${GENERATE_ALL} == ${YES} ] && state="on"

    echo "${ALL_CHAPTERS} A ${state}"
    i=1
    for chapter in ${CHAPTERS} ; do
	echo "${chapter} ${i} ${state}"
	(( i++ ))
    done
    )

exec 3>&1
selected_chapters=$( whiptail \
    --title "Maemo documentation" \
    --checklist "Select the chapters:" 20 76 12 \
    ${chapter_menu} \
    2>&1 1>&3 )
retval=${?}
exec 3>&1

selected_chapters=$( echo ${selected_chapters} | sed 's/"//g' )
    
if [ ${retval} -eq 1 ] || [ ${retval} -eq 255 ] ; then
    echo "Cancelled"
    exit 0
fi


# Call scripts

case ${mode} in
    p)
	echo "Generating PDF"
	$( dirname ${0} )/maemodoc-pdf \
	    ${document} ${selected_chapters} ${PARMS}
	;;
    h)
	echo "Generating HTML"
	$( dirname ${0} )/maemodoc-html \
	    ${document} ${selected_chapters} ${PARMS}
	;;
    b)
	echo "Generating PDF"
	$( dirname ${0} )/maemodoc-pdf \
	    ${document} ${selected_chapters} ${PARMS}
	echo
	echo "Generating HTML"
	$( dirname ${0} )/maemodoc-html \
	    ${document} ${selected_chapters} ${PARMS}
	;;
    w)
	echo "Generating HTML"
	$( dirname ${0} )/maemodoc-html \
	    ${document} ${selected_chapters} ${PARMS}
	echo
	echo "Wikifying"
	get_skeleton_list
	parse_title
	HTML_PATH="${REL_PATH}/${TITLE}/html"
	allow_unset
	[ -z "${PARM_REL_PATH}" ] || out="--out ${PARM_REL_PATH}"
	$( dirname ${0} )/maemodoc-wikify --in ${HTML_PATH} ${out}
	restore_nounset
esac

echo "All done."
