#!/bin/sh

# Narval setup script - (c) 2000-01 Logilab S.A. (France) - All Rights Reserved.
# http://www.logilab.com - Distributed under the GNU Public Licence v2.0

# DEFINES ######################################################################

# for version
VERSION=`date +"%Y%m%d"`
PYTHONLIB=python2.1/site-packages
function redefine_version() {
    PROGDIR=Narval-$VERSION
}
redefine_version

# for package
DOCBOOKDSL=/usr/lib/sgml/stylesheets/nwalsh-modular/html/docbook.dsl
DOCBOOK_XSLT=/home/logilab/docbook/xslt/html/docbook.xsl
DESTDIR=`pwd` # built packages will end up in DESTDIR
function redefine_package() {
    WORKDIR=/tmp/$PROGDIR-build
}
redefine_package

# for (un)install of tarball
PREFIX=/usr
function redefine_install() {
    BINDIR=$PREFIX/bin
    DATADIR=$PREFIX/share
    LIBDIR=$PREFIX/lib
    DOCDIR=$PREFIX/doc
    CONFDIR=/etc
}
redefine_install

# for documentation
DOCDIRS="narval_user_manual narval_technical_manual modules_programmer_handbook tutorial"

# FUNCTIONS TO PACKAGE THE SOFTWARE ############################################

# clean: remove temp build dir
function cleanbuild() {
    echo "--- Removing temp dir ($WORKDIR)..."
    rm -rf $WORKDIR
}

# checkout Narval
function checkout() {
    echo "--- Creating temp dir ($WORKDIR)..."
    mkdir $WORKDIR
    cd $WORKDIR
    cvs export -D `date +"%Y%m%d"` Narval > /dev/null
    mv Narval $PROGDIR
    #rm $PROGDIR/setup.py # temporary
}

# make html doc ($1 = lang)
function make_htmldoc() {
    echo "--- Generating html doc ($1)..."

    # manuals
    echo "    generating manuals ($1)..."
    pushd $PROGDIR/doc/manuals > /dev/null
    for manual in $DOCDIRS ; do
	cd $manual
	for manual_lang in *.$1.sgml
	    do echo "  $manual_lang"
	    jade -d $DOCBOOKDSL -t xml -V use-id-as-filename $manual_lang
	done
	cd ..
    done
    echo "--- Removing SGML source and unwanted images..."
    find . -name "*eps" -exec rm -f {} \;
    find . -name "*sda" -exec rm -f {} \;
    find . -name "*sgml" -exec rm -f {} \;
    find . -name "*lyx" -exec rm -f {} \;
    for l in en fr ; do
	if [ "$l" != "$1" ] ; then 
	    find . -name "*.$l.png" -exec rm -f {} \;
	fi
    done
    popd > /dev/null

    # howtos
    echo "    generating howtos ($1)..."
    pushd $PROGDIR/doc/howtos > /dev/null
    for howto in *$1.xml ; do
        echo "  $howto"
	4xslt $howto $DOCBOOK_XSLT > `basename $howto .xml`.html
    done
    rm -f *xml
    popd > /dev/null
}

# remove doc
function rm_doc() {
    echo "--- Removing doc..."
    rm -rf $PROGDIR/doc/manuals
    rm -rf $PROGDIR/doc/UML
    rm -rf $PROGDIR/doc/howtos
}

# make printable doc
function make_printdoc() {
    echo "--- Generating printable doc..."
    pushd $PROGDIR/doc/manuals > /dev/null
    for manual in $DOCDIRS ; do
	cd $manual
	for manual_lang in *.sgml ; do
	    echo "  $manual_lang"
	    jade -d ~ocy/tiroir/MyDssl.dsl -t tex $manual_lang
#	    jade -d $DOCBOOKDSL -t tex $manual_lang
	    base=`basename $manual_lang sgml`
	    for k in 1 2 3 ; do
		echo "jadetex ${base}tex (pass $k/3)"
		jadetex ${base}tex > /dev/null
	    done
            dvipdf ${base}dvi > /dev/null
	    dvips ${base}dvi -o ${base}ps > /dev/null
	    gzip *.ps
	    mv *pdf *ps.gz $DESTDIR
	done
	cd ..
    done
    popd > /dev/null
}

# make sure its clean
function distclean() {
    echo "--- Make sure tree is clean..."
    find $PROGDIR -name "*pyc" -exec rm -f {} \;
    find $PROGDIR -name "*~" -exec rm -f {} \;
}

# make documentation tarball
function make_doc_tarball() {
    echo "--- Create doc tarball ($1)..."
    TARBALL=narval-doc-$VERSION.$1.tar.gz
    tar cvzf $TARBALL $PROGDIR/doc > tar.doc.log
    cp $TARBALL $DESTDIR
}

# make code tarball
function make_code_tarball() {
    echo "--- Create code tarball..."
    TARBALL=narval-$VERSION.tar.gz
    tar cvzf $TARBALL $PROGDIR > tar.code.log
    cp $TARBALL $DESTDIR
}

# create RPM
function make_rpm() {
    echo "--- Create RPM..."

    # create working directory
    mkdir rpm

    echo "  write spec file"

    # %defines section
    (echo "%define ver $VERSION" ;\
    echo "%define python_lib %{_libdir}/$PYTHONLIB" ;\
    ) > rpm/spec-define

    # %install section
    (echo "%install" ;\
    echo "cp -R share \$RPM_BUILD_ROOT/%{_datadir}/narval" ;\
    echo "cp -R narval \$RPM_BUILD_ROOT/%{python_lib}/narval" ;\
    echo "ln -s %{python_lib}/narval/Engine.py \$RPM_BUILD_ROOT/%{_bindir}/narval" ;\
    echo "ln -s %{python_lib}/narval/Horn.py   \$RPM_BUILD_ROOT/%{_bindir}/X11/horn" ;\
    echo "ln -s %{python_lib}/narval/npm.py    \$RPM_BUILD_ROOT/%{_bindir}/npm" ;\
    echo "chmod +x \$RPM_BUILD_ROOT/%{python_lib}/narval/Engine.py" ;\
    echo "chmod +x \$RPM_BUILD_ROOT/%{python_lib}/narval/Horn.py" ;\
    ) > rpm/spec-install

    # %files section: build list of files
    (echo "%files" ;\
    echo "%{_bindir}/narval" ;\
    echo "%{_bindir}/X11/horn" ;\
    echo "%{_bindir}/npm" ;\
    echo "%{python_lib}/narval" ;\
    echo "%{_datadir}/narval" ;\
    ) > rpm/spec-files

    # %doc section
    (echo "%doc doc/";\
    ) > rpm/spec-doc

    # %dir section
    # %config section

    # create spec file
    spec="narval-$VERSION-1.spec"
    cat rpm/spec-define > rpm/$spec
    #if [ -n "$TEST" ] ; then
    #	cat ~/Narval/tools/pkg/narval.spec >> rpm/$spec
    #else
	cat $PROGDIR/tools/pkg/narval.spec >> rpm/$spec
    #fi
    cat rpm/spec-install >> rpm/$spec
    cat rpm/spec-files >> rpm/$spec
    cat rpm/spec-doc >> rpm/$spec

    echo "  build"

    # prepare build tree
    mkdir rpm/BUILD rpm/RPMS rpm/SOURCES rpm/SPECS rpm/SRPMS
    cp narval-$VERSION.tar.gz rpm/SOURCES/
    mkdir rpm/test ; pushd rpm/test > /dev/null ; fake_tree ; popd > /dev/null
    TOPDIR="`pwd`/rpm"
    rpm -ba rpm/$spec --define "_topdir $TOPDIR" --buildroot $TOPDIR/test \
	> rpm.log 2> rpm-err.log
    cp rpm/RPMS/*/*rpm $DESTDIR
    cp rpm/SRPMS/*rpm $DESTDIR
    # rm -rf rpm
}

# create DEB
function make_deb() {
    echo "--- Create DEB..."
    echo "NOT IMPLEMENTED"
}

# create fake tree to test install
function fake_tree() {
    mkdir usr ; cd usr
    mkdir bin sbin libexec share etc com var lib include man doc
    mkdir bin/X11 lib/python2.1 lib/python2.1/site-packages
    cd -
}

# do all the packaging
function package_htmldoc() {
    cleanbuild
    checkout
    make_htmldoc $1
    distclean
    make_doc_tarball $1
}

# do all the packaging
function package() {
    package_htmldoc en
    package_htmldoc fr
    # code
    rm_doc
    make_code_tarball
    make_rpm
}

# create a tarball containing a cvs snapshot of the code and no doc
function snapshot() {
    cleanbuild
    checkout
    distclean
    rm_doc
    make_code_tarball
}

# make printable documentation: *.pdf and *.ps.gz
function printdoc() {
    cleanbuild
    checkout
    distclean
    make_printdoc
}

# (UN)INSTALL TARBALL DISTRIBUTION #############################################

function amiroot() {
    if [ "$(whoami)" != 'root' ]; then
        echo "root privileges are required to setup Narval system-wide"
        exit 1
    fi
}

function install() {
    # root ?
    amiroot

    # python lib
    echo "--- Install python code in $LIBDIR/$PYTHONLIB..."
    cp -R narval $LIBDIR/$PYTHONLIB/narval
    chmod +x $LIBDIR/$PYTHONLIB/narval/Engine.py
    chmod +x $LIBDIR/$PYTHONLIB/narval/Horn.py

    # bin
    echo "--- Installing executables in $BINDIR..."
    ln -s $LIBDIR/$PYTHONLIB/narval/Engine.py $BINDIR/narval
    ln -s $LIBDIR/$PYTHONLIB/narval/Horn.py $BINDIR/X11/horn
    ln -s $LIBDIR/$PYTHONLIB/narval/npm.py $BINDIR/npm

    # share
    echo "--- Installing shared data in $DATADIR..."
    cp -R share $DATADIR/narval

    # doc
    echo "--- Installing documentation in $DOCDIR..."
    cp -R doc $DOCDIR/narval

    # conf
    echo "--- Installing configuration file in $CONFDIR..."
    cp conf/narval.conf $CONFDIR/
}

function uninstall() {
    # root ?
    amiroot

    # bin
    echo "--- Removing executables from $BINDIR..."
    rm -f $BINDIR/narval
    rm -f $BINDIR/X11/horn 
    rm -f $BINDIR/npm

    # python lib
    echo "--- Removing python code from $LIBDIR/$PYTHONLIB..."
    rm -rf $LIBDIR/$PYTHONLIB/narval

    # share
    echo "--- Removing shared data from $DATADIR..."
    rm -rf $DATADIR/narval

    # doc
    echo "--- Removing documentation from $DOCDIR..."
    rm -rf $DOCDIR/narval

    # conf
    echo "--- Removing configuration from $CONFDIR..."
    rm -f $CONFDIR/narval.conf
}


# MAIN #########################################################################

if [ "$1" = "install" ] ; then
    install
elif [ "$1" = "uninstall" ] ; then
    uninstall
elif [ "$1" = "package" ] && [ "$2" != "" ]; then
    VERSION=$2
    redefine_version
    redefine_package
    package
elif [ "$1" = "pkgdoc" ] && [ "$2" != "" ]; then
    VERSION=$2
    redefine_version
    redefine_package
    package_htmldoc en
    package_htmldoc fr
elif [ "$1" = "pkgcode" ] && [ "$2" != "" ]; then
    VERSION=$2
    redefine_version
    redefine_package
    snapshot
    make_rpm
elif [ "$1" = "snapshot" ]; then
    VERSION=cvs`date +"%Y%m%d"`
    redefine_version
    redefine_package
    snapshot
elif [ "$1" = "printdoc" ]; then
    printdoc
else
    echo
    echo "Usage: setup.sh [install|uninstall|package|snapshot|printdoc]"
    echo
    echo "install         - install the tarball distribution"
    echo "uninstall       - uninstall the tarball distribution"
    echo "package version - create packages (from cvs. for maintainers)"
    echo "pkgdoc  version - create html doc (tarballs)"
    echo "pkgcode version - create package (code only, no doc)"
    echo "snapshot        - create a cvs snapshot (code only, no doc)"
    echo "printdoc        - make printable doc (ps and pdf)"
    echo
fi

################################################################################
