#!/bin/bash -x
#
# Copyright (C) 2011,2012 Frank Sundermeyer <fsundermeyer@opensuse.org>
#
# Author:
# Frank Sundermeyer <fsundermeyer@opensuse.org>
#
# Initialize a working directory to get started with daps
#

# ---------
# Verbose error handling
#
function exit_on_error () {
    ccecho "error" "ERROR: ${1}" >&2
    exit 1;
}
function tempfile () {
    TEMPFILE=$(mktemp /tmp/daps-init-temp.XXXXXXX 2>/dev/null)
}
function moving_file () {
    mv $TEMPFILE  ${DOC_DIR}/xml/MAIN-${BOOK}.xml   
}
function usage () {
    echo "
$ME -d <PATH TO DIRECTORY> [-r (article|book)] 

Create a working environment for DAPS with an example DocBook 4 document.
Specify the path to your documentation directory with -d. The example 
document's doc config (DC) file will be located there. 
(Sub)Directories will be created if they do not exist already.

Options:

-r, --rootelement  DocBook root element of the example document. Currently
                   supported are \"book\" (default) and \"article\".
"
    exit 0
}
#################################################
# MAIN
#

# show help when called with no arguments
[[ -z "$1" ]] && usage

ME=$(basename $0)
CONT=
DC_FILE="DC-daps-example"
DAPSROOT="/usr/share/daps" # the default

# user config file
USER_CONFIG="$HOME/.daps/config"

# source config file to possibly get a DAPSROOT
source "/etc/daps/config"
if [[ -f $USER_CONFIG ]]; then
    source $USER_CONFIG
fi

ARGS=$(getopt -o d:hr: -l dapsroot:,docdir:,help,rootelement:,title:,date:,productname:,productnumber: -n $ME -- "$@")

eval set -- "$ARGS"

while true ; do
    case "$1" in
        --dapsroot)
            # undocumented devel feature
            if [[ -d $2 ]]; then
                DAPSROOT="$2"
            else
                exit_on_error "dapsroot \"$2\" is not a valid directory."
            fi
            shift 2
            ;;
        -d|--docdir)
            DOC_DIR=$2
            shift 2
            ;;
        -h|--help)
            usage
	    ;;
	-r|--rootelement)
            if [[ article = $2 || book = $2 ]]; then
                ROOTELEMENT="$2"
            else
                exit_on_error "Rootelement must be either \"article\" or \"book\""
            fi
	    shift 2
	    ;;
	--title)
	    if [[ -z $2 ]]; then
		exit_on_error "Title must be set"
	    else
		TITLE=$2
	    fi
	    shift 2
	    ;;
	--date)
	    if [[ -z $2 ]]; then 
		exit_on_error "Date must be set"
	    else
		DATE=$2
		date -d $DATE >> /dev/null 2>&1 || echo "Wrong date format. Use YYYY-MM-DD"
	    fi
	    shift 2 
	    ;;
	--productname)
	    if [[ -z $2 ]]; then
		exit_on_error "Productname must be set"
	    else
		PRODUCTNAME=$2
	
	    fi
	    shift 2
	    ;;
	--productnumber)
	    if [[ -z $2 ]]; then
		exit_on_error "Productnumber must be set"
	    else 
		PRODUCTNUMBER=$2
	    fi
	    shift 2
	    ;;
        --) shift ; break ;;
        *) exit_on_error "Wrong parameter: $1" ;;
    esac
done



[[ -z "$DOC_DIR" ]] && exit_on_error "You must specify a directory with -d. See \"$ME -h for details"
# default ROOTELEMENT is book
[[ -z "$ROOTELEMENT" ]] && ROOTELEMENT=book

# ----------
# Process DOCCONFIG and create directories
#

if [[ ! -d $DOC_DIR ]]; then
    while [[ y != $CONT && n != $CONT ]] ; do
        read -p "$DOC_DIR does not exist. Create it? [y/n] " CONT
    done
    if [[ y = $CONT ]]; then
        mkdir -p $DOC_DIR || exit_on_error "Cannot create $DOC_DIR" 
    else
        exit_on_error "Aborted by user"
    fi
fi

# Create needed subdirs in DOC_DIR
mkdir -p ${DOC_DIR}/images/src/{dia,fig,png,svg,eps,pdf} ${DOC_DIR}/xml || exit_on_error "Cannot create needed subdirectories in $DOC_DIR" 

# ----------
# copy the templates
#
BOOK="${DC_FILE#${CONF_PREFIX}*}"

# DC-file
cp ${DAPSROOT}/init_templates/DC-file.template ${DOC_DIR}/$DC_FILE && \
        sed -i s/§§MAIN§§/MAIN-$BOOK.xml/g ${DOC_DIR}/$DC_FILE
# MAIN XML file
cp ${DAPSROOT}/init_templates/MAIN.$ROOTELEMENT.template ${DOC_DIR}/xml/MAIN-${BOOK}.xml &&
       
	

if [[ -z $TITLE ]]; then
    sed -i 's/§§TITLE§§/Book Template/g' ${DOC_DIR}/xml/MAIN-${BOOK}.xml
else 
    sed -i s/§§TITLE§§/$TITLE/g ${DOC_DIR}/xml/MAIN-${BOOK}.xml
fi

if [[ -z $DATE ]]; then 
    sed -i 's/§§DATE§§/<?dbtimestamp format="B d, Y"?>/g' ${DOC_DIR}/xml/MAIN-${BOOK}.xml
else 
    sed -i s/§§DATE§§/$DATE/g ${DOC_DIR}/xml/MAIN-${BOOK}.xml
fi

if [[ -n $PRODUCTNAME ]]; then
    tempfile
    if [[ $? -eq 0 ]]; then
	xml ed -a /book/bookinfo/title -t elem -n productname -v $PRODUCTNAME ${DOC_DIR}/xml/MAIN-${BOOK}.xml > $TEMPFILE && \ 
	moving_file
    else 
	ccecho "warn" "Productname could not be created"
    fi
fi


if [[ -n $PRODUCTNUMBER ]]; then
    tempfile
    if [[ $? -eq 0 ]]; then
	xml ed -a /book/bookinfo/title -t elem -n productnumber -v $PRODUCTNUMBER ${DOC_DIR}/xml/MAIN-${BOOK}.xml > $TEMPFILE && \ 
        moving_file
    else 
	ccecho "warn" "Productnumber could not be created"
    fi
fi


    

# graphics
cp ${DAPSROOT}/init_templates/example{1,2}.png ${DOC_DIR}/images/src/png/
  
ccecho "result" "Successfully created a working environment at ${DOC_DIR}"
echo "To build a PDF or HTML version of the example document enter
daps -d ${DOC_DIR}/$DC_FILE color-pdf
or
daps -d ${DOC_DIR}/$DC_FILE html"

exit 0;
