#!/bin/sh
# ----------------------------------------------------------------------------
# - aconf                                                                    -
# - aleph configure program                                                  -
# ----------------------------------------------------------------------------
# - This program is  free software;  you can  redistribute it and/or  modify -
# - it provided that this copyright notice is kept intact.                   -
# -                                                                          -
# - This  program  is  distributed in the hope  that it  will be useful, but -
# - without  any   warranty;  without  even   the   implied    warranty   of -
# - merchantability  or fitness for a particular purpose. In not event shall -
# - the copyright holder be  liable for  any direct, indirect, incidental or -
# - special damages arising in any way out of the use of this software.      -
# ----------------------------------------------------------------------------
# - copyright (c) 1999-2000 amaury darsch                                    -
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# - set default variables                                                    -
# ----------------------------------------------------------------------------

# the program name we are running
progname=$0
# the platform we are trying to compile
platform=unknown
# the root directory for the whole aleph system
topdir=
# the directory with all makefiles templates
makdir=
# the directory with all aleph bin utilities
utldir=
# the target directory for the site and rule file
trgdir=
# the directory where to install aleph
prefix=/usr/local
# compile the distribution in debug mode
enable_debug=no
# compile the distribution in optimized mode
enable_optimized=yes
# compile the distribution with profiling mode
enable_profiled=no
# whether or not we report the option
enable_report=no
# the base makefile
base_makefile=unknown
# the optional site makefile
sitefile=
# the site makefile
site_makefile=unknown
# the optional rule makefile
rulefile=
# the rule makefile
rule_makefile=unknown
# the compilation mode
ccmode=debug

# ----------------------------------------------------------------------------
# - local function always make life easier                                   -
# ----------------------------------------------------------------------------

# print a usage message
usage () {
    echo "usage: configure [options]"
    echo "       --help             print this help message"
    echo "       --prefix           set target directory to install"
    echo "       --enable-debug     compile and install in debug mode"
    echo "       --enable-optimized compile and install in optimized mode"
    echo "       --sitefile         force to use a given site makefile"
    echo "       --rulefile         force to use a given rule makefile"
    exit 0
}

# print an error message
error () {
    echo $1
    exit 1
}

# report configure options
report () {
    echo "top directory    = " $topdir
    echo "makdir           = " $makdir
    echo "utldir           = " $utldir
    echo "trgdir           = " $trgdir
    echo "prefix           = " $prefix
    echo "platform         = " $platform
    echo "base make file   = " $base_makefile
    echo "site make file   = " $site_makefile
    echo "rule make file   = " $rule_makefile
    echo "enable_debug     = " $enable_debug
    echo "enable_optimized = " $enable_optimized
    exit 0
}

# this function computes the various directories
mkpdir () {
    # compute top directory path
    if test -z "$progname"; then
	topdir=../..
    else
	topdir=`dirname $progname`/../..
    fi

    # compute make file template directory
    makdir=$topdir/cnf/mak
    if test ! -d "$makdir"; then
	error "cannot find template makefile directory" $makdir
    fi

    # compute configure binary utilities directory
    utldir=$topdir/cnf/bin
    if test ! -d "$utldir"; then
	error "cannot find configure utilities directory" $utldir
    fi

    # compute target directory
    trgdir=$topdir/cnf
    if test ! -d "$trgdir"; then
	error "cannot find target directory" $trgdir
    fi
}

# this function computes the platform
mkplat () {
    plt=$utldir/aguess
    if test ! -x "$plt"; then
	error "cannot find platform utility" $plt
    fi
    platform=`$plt`
}

# this function computes the make file path based on other path
mkpath () {
    # compute base make file
    base_makefile=$makdir/base.mak
    if test ! -f "$base_makefile"; then
	error "cannot find base makefile $base_makefile"
    fi

    # compute site make file
    if test -z "$sitefile"; then
	site_makefile=$makdir/site-${platform}.mak
    else
	site_makefile=$sitefile
    fi
    if test ! -f "$site_makefile"; then
	error "cannot find site makefile $site_makefile"
    fi

    # compute site make file
    if test -z "$rulefile"; then
	rule_makefile=$makdir/rule.mak
    else
	rule_makefile=$rulefile
    fi
    if test ! -f "$rule_makefile"; then
	error "cannot find rule makefile $rule_makefile"
    fi    
}

# this function computes the compilation mode
mkmode () {
    # check for debug mode
    if test "$enable_debug" = "yes"; then
	ccmode=debug
	enable_optimized=no
	enable_profiled=no
    fi

    # check for profiled mode
    if test "$enable_profiled" = "yes"; then
	ccmode=profiled
	enable_debug=no
	enable_optimized=no
    fi

    # check for optimized mode
    if test "$enable_optimized" = "yes"; then
	ccmode=optimized
	enable_debug=no
	enable_profiled=no
    fi
}

# this function generate the target makefile
mkfile () {
    # compute target site file
    site_file=$trgdir/site.mak
    /bin/rm -f $site_file

    # generate site make file
    echo "# this file was generated by the aleph configure" >  $site_file
    echo "# do not edit unless you know what you are doing" >> $site_file
    echo                                                    >> $site_file
    echo "PREFIX          = " $prefix                       >> $site_file
    echo "MODE            = " $ccmode                       >> $site_file
    echo "PLATFORM        = " $platform                     >> $site_file
    grep -v '^#' $base_makefile                             >> $site_file
    grep -v '^#' $site_makefile                             >> $site_file

    # compute rule makefile
    rule_file=$trgdir/rule.mak
    /bin/rm -f $rule_file

    # generate rule makefile
    cat $rule_makefile > $rule_file
}

# ----------------------------------------------------------------------------
# - parse options - this is where we really start                            -
# ----------------------------------------------------------------------------

cnf_prev=
for cnf_option
do
    # assign the previous option argument
    if test -n "$cnf_prev"; then
	eval "$cnf_prev=\$cnf_option"
	cnf_prev=
	continue
    fi

    # extract options
    case "$cnf_option" in
    -*=*) cnf_optarg=`echo "$cnf_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) cnf_optarg= ;;
    esac

    # process options now
    case "$cnf_option" in
    -h | --help)             usage ;;
    -r | --report)           enable_report=yes ;;
    -d | --enable-debug)     enable_debug=yes ;;
    -o | --enable-optimized) enable_optimized=yes ;;
    -p | --enable-profiled)  enable_profiled=yes ;;

    --prefix)                cnf_prev=prefix ;;
    --prefix=*)              prefix="$cnf_optarg" ;;

    --sitefile)              cnf_prev=sitefile ;;
    --sitefile=*)            sitefile="$cnf_optarg" ;;

    --rulefile)              cnf_prev=rulefile ;;
    --rulefile=*)            rulefile="$cnf_optarg" ;;

    *)                       error "configure: illegal option $cnf_option" ;;
    esac
done

# compute target directories
mkpdir
# check for supported platform
mkplat
# compute the makefile path
mkpath
# compute the compilation mode
mkmode

# check if we report the user option
if test "$enable_report" = "yes"; then
    report
fi

# generate here the makefiles
mkfile

# success
exit 0
