#!/bin/sh
#$Id: configure,v 1.2 2002/04/02 19:57:54 jbcombes Exp $
################################################################################
#                                                                              #
#    Configuration script for building daCode documentation. We currently only #
# support docbook + jade on Linux. If someone could contribute for other ways, #
# that would really be appreciated                                             #
# Currently this script doesn't like if you have space and other "special"     #
# chars in the path to your docbook stylesheets.                               #
#                                                                              #
#123456789012345678901234567890123456789012346578901234567890123465798012346578#
################################################################################
##
# Languages known for documentation
##
LANGS="en fr"

################################################################################
#                      Parse command-line options.                             #
################################################################################

while [ $1 ]; do
    case $1 in

	--with-xml-dcl=*)
	    XML_DECLARATION=`echo $1 | sed s/--with-xml-dcl=//`
	    no_prompt_xml_dcl="true"
	    no_locate_xml_dcl="true"
	    ;;
	--with-xml-dcl)
	    no_prompt_xml_dcl="true"
	    ;;

	--with-dtd=*)
	    DOCBOOK_DTD=`echo $1 | sed s/--with-dtd=//`
	    no_prompt_dtd="true"
	    no_locate_dtd="true"
	    ;;
	--with-dtd)
	    no_prompt_dtd="true"
	    ;;

	--with-docbook=*)
	    STYLESHEET=`echo $1 | sed s/--with-docbook=//`
	    no_prompt_stylesheet="true"
	    no_locate_stylesheet="true"
	    ;;
	--with-docbook)
	    no_prompt_stylesheet="true"
	    ;;

	--with-collateindex=*)
	    COLLATEINDEX=`echo $1 | sed s/--with-collateindex=//`
	    no_prompt_collateindex="true"
	    no_locate_collateindex="true"
	    ;;
	--with-collateindex)
	    no_prompt_collateindex="true"
	    ;;

       --with-tidy=*) 
	    TIDY=`echo $1 | sed s/--with-tidy=//`
	    ;;

       --build-dir=*)
	    BUILD_DIR=`echo $1 | sed s/--build-dir=//`
	    ;;
       --use-logo=*)
	    logo=`echo $1 | sed s/--use-logo=//`
	    ;;
       --use-css=*)
	    css=`echo $1 | sed s/--use-css=//`
	    ;; 
       --copy-files)
	    copy="true"
	    echo "Not yet implemented"
	    ;;
       --help)
	    echo "Usage
configure [options]
  --with-xml-dcl=/path/to/xml.dcl: use this XML declaration
  --with-xml-dcl: use the first XML declaration found on your system
  --with-dtd=path/to/docbookx.dtd: use this version of the docbook XML DTD.
  --with-dtd: use the first fiule named docbookx.dtd found on the system
  --with-docbook=path/to/docbook.dsl: use this stylesheet for docbook conversion
  --with-docbook: use the first stylesheet found
  --help: this help message
  --with-tidy=/path/to/tidy use this tidy executable
  --build-dir=path: build the docs in this dir [build/]. All doc will be put in 
    build-dir/docname-lang/
  --use-logo=filename: use this logo instead of the default one.
  --use-css=filename: use this CSS file instead of default one.
  --copy-files: copy CSS and images into a subdir of the generated doc (TODO)"
	    exit
	    ;; 
       *)
	echo "Unknow option $1. Try --help for a list of valid options."
	exit 1
	;;
    esac
    shift
done

##
# Path to images & stylesheets.
# Docs will be build in $BUILD_DIR/docname-lang/
##
if [ -z $logo ]; then
    logo=common/images/logo.png
fi
 
if [ -z $css ]; then
    css=common/stylesheets/dacode-docbook.css
fi

if [ -z $BUILD_DIR ]; then
    BUILD_DIR=build/
fi
# adjust path to images
# protect from spaces
tmp=`echo $BUILD_DIR | sed 's/ /e/g'`
#cut the path into words
tmp=`echo $tmp | sed 's/\// /g'`
path=../
for i in $tmp; do
    path="$path../";
done

CSS="$path$css"
LOGO="$path$logo"
#FIXME: when copy is true, the path ain't the same.

################################################################################
#                          Docbook preparation                                 #
################################################################################
##
# Locate XML declaration
##

# try to locate XML declaration if $no_locate unset
if [ -z $no_locate_xml_dcl ];
    then
    XML_DECLARATION=`locate xml.dcl`;
fi

#Controls whether some stylesheets has been found, and die if not
if [ -z "$XML_DECLARATION" ]; then
    echo "No xml declaration found. You may have to specify the XML declaration
with --with-xml-dcl."
    exit 127
fi
#Compute the default stylesheet (i.e. the first one we found...)
DEFAULT_XML_DECLARATION=`echo $XML_DECLARATION | cut -f1 -d' '`

# And prompt the user in case we're not sure.
if [ -z $no_prompt_xml_dcl ];
    then
    #ask the user if he wants a stylesheet.
    echo "Which XML declaration do you want to use?"
    select i in $XML_DECLARATION; do
	XML_DECLARATION=$i
	if  [ $i ] ;
	then 
	    #echo Here -$i-
	    break;
	fi 
    done
    else 
	XML_DECLARATION=$DEFAULT_XML_DECLARATION;

fi
##
# Locate dtd
##

# try to locate docbook dtd if $no_locate unset
if [ -z $no_locate_dtd ];
    then
    DOCBOOK_DTD=`locate docbookx.dtd`;
fi

#Controls whether some stylesheets has been found, and die if not
if [ -z "$DOCBOOK_DTD" ]; then
    echo "No docbook XML dtd found. You will need to specifiy where the docbook XML
dtd, version 4.1.2 lies with --with-dtd=path"
    exit 127
fi
#Compute the default stylesheet (i.e. the first one we found...)
DEFAULT_DTD=`echo $DOCBOOK_DTD | cut -f1 -d' '`

# And prompt the user in case we're not sure.
if [ -z $no_prompt_dtd ];
    then
    #ask the user if he wants a stylesheet.
    echo "Which docbook dtd do you want to use?"
    select i in $DOCBOOK_DTD; do
	DOCBOOK_DTD=$i
	if  [ $i ] ;
	then 
	    #echo Here -$i-
	    break;
	fi 
    done
    else 
	DOCBOOK_DTD=$DEFAULT_DTD;

fi

##
# Locate collateindex.pl
##

# try to locate docbook collateindex if $no_locate unset
if [ -z $no_locate_collateindex ];
    then
    COLLATEINDEX=`locate collateindex.pl`;
fi

#Controls whether some collateindex has been found, and die if not
if [ -z "$COLLATEINDEX" ]; then
    echo "collateindex.pl script not found. You may have to specify its path 
with --with-collateindex."
    exit 127
fi
#Compute the default script (i.e. the first one we found...)
DEFAULT_COLLATEINDEX=`echo $COLLATEINDEX | cut -f1 -d' '`

# And prompt the user in case we're not sure.
if [ -z $no_prompt_collateindex ];
    then
    #ask the user which he wants to use.
    echo "Which collateindex do you want to use?"
    select i in $COLLATEINDEX; do
	COLLATEINDEX=$i
	if  [ $i ] ;
	then 
	    #echo Here -$i-
	    break;
	fi 
    done
    else 
	COLLATEINDEX=$DEFAULT_COLLATEINDEX;

fi

##
# localize stylesheets
##
# try to locate docbook stylesheets if $no_locate unset
if [ -z $no_locate_stylesheet ];
    then
    STYLESHEET=`locate docbook.dsl`;
fi

#Controls whether some stylesheets has been found, and die if not
if [ -z "$STYLESHEET" ]; then
    echo "No docbook stylesheet found. You will need to specifiy where Norman 
Walsh's modular stylesheets lie with --with-docbook=path"
    exit 127
fi
#Compute the default stylesheet (i.e. the first one we found...)
DEFAULT_STYLESHEET=`echo $STYLESHEET | cut -f1 -d' '`

# And prompt the user in case we're not sure.
if [ -z $no_prompt_stylesheet ];
    then
    #ask the user if he wants a stylesheet.
    echo "Which stylesheet do you want to use?"
    select i in $STYLESHEET; do
	STYLESHEET=$i
	if  [ $i ] ;
	then 
	    #echo Here -$i-
	    break;
	fi 
    done
    else 
	STYLESHEET=$DEFAULT_STYLESHEET;

fi


#Now we can generate the stylesheet.
#We guess there are no fu** shell meta-chars in filenames...
#First off protect /s against misinterpretation by sed
STYLESHEET=`echo $STYLESHEET | sed 's|/|\\\/|g'`
LOGO=`echo $LOGO | sed 's|/|\\\/|g'`
CSS=`echo $CSS | sed 's|/|\\\/|g'`

echo -n Creating stylesheet...
cat common/stylesheets/daCode.dsl.in | sed s/@@DOCBOOK-STYLESHEET@@/$STYLESHEET/g \
    > common/stylesheets/daCode.dsl.in.tmp
cat common/stylesheets/daCode.dsl.in.tmp | sed s/@@LOGO@@/$LOGO/g \
    > common/stylesheets/daCode.dsl.in.tmp1
cat common/stylesheets/daCode.dsl.in.tmp1 | sed s/@@CSS@@/$CSS/g \
    > common/stylesheets/daCode.dsl
rm -f common/stylesheets/daCode.dsl.in.tmp*

echo " done."

################################################################################
#                                                                              #
#   Generating doctranslator.pl                                                #
#                                                                              #
################################################################################
echo -n Generating doctranslator.pl...
DOCBOOK_DTD=`echo $DOCBOOK_DTD | sed 's|/|\\\/|g'`
LANG_RE=`echo $LANGS | sed 's/ /|/g'`
cat common/scripts/doctranslator.pl.in | sed s/@@DOCBOOK_DTD@@/$DOCBOOK_DTD/g \
    > common/scripts/doctranslator.pl.in.tmp
cat common/scripts/doctranslator.pl.in.tmp | sed s/@@LANG@@/$LANG_RE/g \
    > common/scripts/doctranslator.pl
rm -f common/scripts/doctranslator.pl.in.tmp
chmod a+x common/scripts/doctranslator.pl

echo " done."

################################################################################
#                                                                              #
#   Testing programs to generate makefiles                                     #
#                                                                              #
################################################################################
if [ -z "$TIDY" ]; then
    TIDY=`which tidy`;
fi
if [ -z "$TIDY" ]; then
    echo "************************************************************
* Warning: tidy has not been found in your path. Make sure *
* you have installed the software and specify its location *
* with --with-tidy=path.                                   *
************************************************************";

fi


################################################################################
#                                                                              #
#   Generating makefiles....                                                   #
#                                                                              #
################################################################################
echo -n Generating makefiles...

# Doing escape on paths
BUILD_DIR=`echo $BUILD_DIR | sed 's|/|\\\/|g'`
XML_DECLARATION=`echo $XML_DECLARATION | sed 's|/|\\\/|g'`
COLLATEINDEX=`echo $COLLATEINDEX | sed 's|/|\\\/|g'`
TIDY=`echo $TIDY | sed 's|/|\\\/|g'`
# generate main makefile.

cat Makefile.in | sed s/@@BUILD@@/$BUILD_DIR/g > Makefile

# generate  makefiles for  docbook compilation
for i in user webmaster; do
    lang_dist=
    for lang in $LANGS; do
	lang_dist="$lang_dist $i-$lang"
    done
    for j in $lang_dist xml; do
	echo $i/$j
	cat $i/$j/Makefile.in | sed s/@@XML_DECLARATION@@/$XML_DECLARATION/ > \
	    $i/$j/Makefile.in.tmp

	cat $i/$j/Makefile.in.tmp | sed s/@@COLLATEINDEX@@/$COLLATEINDEX/ > \
	    $i/$j/Makefile.in.tmp1

	cat $i/$j/Makefile.in.tmp1 | sed s/@@TIDY@@/$TIDY/ > \
	    $i/$j/Makefile

	rm -f $i/$j/Makefile.in.tmp*
    done
done
i=developer
j=doc-writing-en
	cat $i/$j/Makefile.in | sed s/@@XML_DECLARATION@@/$XML_DECLARATION/ > \
	    $i/$j/Makefile.in.tmp

	cat $i/$j/Makefile.in.tmp | sed s/@@COLLATEINDEX@@/$COLLATEINDEX/ > \
	    $i/$j/Makefile.in.tmp1

	cat $i/$j/Makefile.in.tmp1 | sed s/@@TIDY@@/$TIDY/ > \
	    $i/$j/Makefile

	rm -f $i/$j/Makefile.in.tmp*


echo " done."


#make clean