#!/bin/sh
#
# XML Catalog Manager (xmlcatmgr)
# $Id: make.sh,v 1.3 2003/12/22 11:20:46 jmmv Exp $
#
# Copyright (c) 2003 Julio M. Merino Vidal.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

progname=`basename $0`

# -------------------------------------------------------------------------
# usage
# -------------------------------------------------------------------------

usage() {
    cat 1>&2 << EOF
usage: ./${progname} [-h] [-w dir] [all|clean|cleandir|install|release]

    -h        show this help message
    -w dir    set work directory
EOF
}

# -------------------------------------------------------------------------
# Main program
# -------------------------------------------------------------------------

WORKDIR=`pwd`/work

while [ $# -gt 0 ]; do
    echo $1 | grep '^-' >/dev/null || break

    case $1 in
        -h)
            usage
            exit 0
            ;;
        -w)
            WORKDIR=$2
            shift
            ;;
        *)
            echo "${progname}: unknown option \`$1'" 1>&2
            echo "type \`./${progname} -h' for more information" 1>&2
            exit 1
            ;;
    esac

    shift
done

target=${1:-all}

if [ ! -d ${WORKDIR} ]; then
    cat 1>&2 <<EOF
===========================================================================
BUILD OF \`${target}' TARGET STOPPED.
WORK DIRECTORY \`${WORKDIR}' NOT FOUND.

Cannot continue without a work directory.  It may not exist due to one
of the following reasons:

1) You have not configured the package yet.
   Execute \`./configure -h' for more information.

2) You have used a non-standard work directory.
   Rerun this script using the \`-w' flag to specify where is your work
   directory.

===========================================================================

EOF
    exit 1
fi

. ${WORKDIR}/pkgvars

case ${target} in
    all|clean|install)
        cd ${WORKDIR} && DESTDIR=${DESTDIR} ${MAKE} ${target}
        ;;
    cleandir)
        rm -rf ${WORKDIR}
        echo "${progname}: \`${WORKDIR}' removed"
        true
        ;;
    release)
        curdir=`pwd`
        curdir=`basename ${curdir}`
        if [ "${curdir}" = "${PKGNAME}-${PKGVERSION}" ]; then
            echo "${progname}: cannot make release (rename current directory)"
        else
            cd ..
            cp -rf ${curdir} ${PKGNAME}-${PKGVERSION}
            find ${PKGNAME}-${PKGVERSION} -name CVS | xargs rm -rf
            find ${PKGNAME}-${PKGVERSION} -name '.*' | xargs rm -rf
            rm -rf ${PKGNAME}-${PKGVERSION}/work
            tar czHf ${PKGNAME}-${PKGVERSION}.tar.gz ${PKGNAME}-${PKGVERSION}
            rm -rf ${PKGNAME}-${PKGVERSION}
            echo "${progname}: ../${PKGNAME}-${PKGVERSION}.tar.gz created"
        fi
        ;;
    *)
        echo "${progname}: unknown target \`${target}'" 1>&2
        false
        ;;
esac

exit

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
