#!/bin/sh
# ----------------------------------------------------------------------------
# - aleph-setup                                                              -
# - aleph build tree configuration installer                                 -
# ----------------------------------------------------------------------------
# - 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-2003 amaury darsch                                    -
# ----------------------------------------------------------------------------

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

# the program name we are running
progname=$0
# the platform we are trying to compile
platform=unknown
# the platform source name
platname=unknown
# the root directory of the distribution
rootdir=
# the cnf directory
cnfdir=
# the source directory
srcdir=
# the installation directory prefix
prefix=/usr/local
# whether or not we report the option
verbose=no
# the default compiler
compiler=gcc
# the platform os name
platname=
# the platform os version
platvers=
platvmaj=0
platvmin=0
# the processor name
procname=
# the processor type
proctype=generic
# default compilation mode
ccmode=debug
# linking type
lktype=dynamic
# so linking mode
lkmode=generic

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

# print a usage message
usage () {
    echo "usage: aleph-setup [options]"
    echo "       -h                 print this help message"
    echo "       -v                 be verbose"
    echo "       -g                 set debug mode"
    echo "       -o                 set optimized mode"
    echo "       -s                 compile and link statically"

    echo "       --help             print this help message"
    echo "       --prefix           set target directory to install"
    echo "       --compiler         set default compiler"
    echo "       --proctype         set processor architecture"
    echo "       --soname           use soname during linking"
    echo "       --static           compile and link statically"
    exit 0
}

# print an error message
error () {
    echo "aleph-setup: $1"
    exit 1
}

# report options
report () {
    echo "root directory   = $rootdir"
    echo "cnf directory    = $cnfdir"
    echo "source directory = $srcdir"
    echo "prefix           = $prefix"
    echo "platform name    = $platname"
    echo "platform version = $platvers"
    echo "processor name   = $procname"
    echo "processor type   = $proctype"
    echo "compiler         = $compiler"
    echo "compilation mode = $ccmode"
    echo "so linking  type = $lktype"
    echo "so linking  mode = $lkmode"
}

# this function computes the various directories
getdir () {
    # compute top directory path
    if test -z "$progname"; then
	rootdir=../..
    else
	rootdir=`dirname $progname`/../..
    fi
    # compute the cnfdirectory
    cnfdir=$rootdir/cnf
    if test ! -d "$cnfdir"; then
	error "cannot find cnf directory" $cnfdir
    fi
    # compute the source directory
    srcdir=$rootdir/src
    if test ! -d "$srcdir"; then
	error "cannot find source directory" $srcdir
    fi
}

# compute the default values
getdef () {
    # get the query script
    query=$rootdir/cnf/bin/aleph-query
    # get the compiler version script
    vcomp=$rootdir/cnf/bin/aleph-vcomp
    # get compiler value
    compiler=`$query compiler gcc`
    # update compiler version
    compiler=`$vcomp $compiler`
    # get the linking mode
    lkmode=`$query lkmode generic`
}

# this function creates the build configuration directory
mkcnf () {
    # create the target cnf directory
    trgdir=$rootdir/bld/cnf
    $cnfdir/bin/aleph-mkdir $trgdir
    # compute platform information
    platname=`$cnfdir/bin/aleph-guess -n`
    if test $? != 0; then
        error "cannot determine platform name"
    fi
    platvers=`$cnfdir/bin/aleph-guess -v`
    if test $? != 0; then
        error "cannot determine platform version"
    fi
    platvmaj=`$cnfdir/bin/aleph-guess -M`
    if test $? != 0; then
        error "cannot determine platform major number"
    fi
    platvmin=`$cnfdir/bin/aleph-guess -m`
    if test $? != 0; then
        error "cannot determine platform minor number"
    fi
    procname=`$cnfdir/bin/aleph-guess -p`
    if test $? != 0; then
        error "cannot determine processor"
    fi
    machconf=$platname-$procname
    # install aleph-plat.mak file
    trgmak=$trgdir/aleph-plat.mak
    echo "# aleph-plat.mak"                > $trgmak
    echo "# aleph makefile configuration" >> $trgmak
    echo                                  >> $trgmak
    echo "# platform configuration"       >> $trgmak
    echo "PLATNAME         = $platname"   >> $trgmak
    echo "PLATVERS         = $platvers"   >> $trgmak
    echo "PLATVMAJ         = $platvmaj"   >> $trgmak
    echo "PLATVMIN         = $platvmin"   >> $trgmak
    echo "PROCNAME         = $procname"   >> $trgmak
    echo "PROCTYPE         = $proctype"   >> $trgmak
    echo "MACHCONF         = $machconf"   >> $trgmak
    echo                                  >> $trgmak
    echo "# installation options"         >> $trgmak
    echo "PREFIX           = $prefix"     >> $trgmak
    echo                                  >> $trgmak
    echo "# compilation mode"             >> $trgmak
    echo "CCMODE           = $ccmode"     >> $trgmak
    echo                                  >> $trgmak
    echo "# linking options"              >> $trgmak
    echo "LKTYPE           = $lktype"     >> $trgmak
    echo "LKMODE           = $lkmode"     >> $trgmak

    # get the appropriate compiler config
    compfile=$cnfdir/mak/aleph-${compiler}.mak
    if test ! -f "$compfile"; then
	error "cannot find compiler file" $compfile
    fi
    if test ! -f $trgdir/aleph-comp.mak; then
	ln -s ../../cnf/mak/aleph-${compiler}.mak $trgdir/aleph-comp.mak
    fi
}

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

# compute various directories
getdir

# compute the default values
getdef

# parse the options
lastopt=
for nextopt
do
    # assign the previous option argument
    if test -n "$lastopt"; then
	eval "$lastopt=\$nextopt"
	lastopt=
	continue
    fi

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

    # process options now
    case "$nextopt" in
    -h | --help)             usage ;;
    -v | --verbose)          verbose=yes ;;
    -g)                      ccmode=debug ;;
    -o)                      ccmode=optimized ;;
    -s | --static)           lktype=static ;;

    --soname)                lkmode=soname ;;

    --prefix)                lastopt=prefix ;;
    --prefix=*)              prefix="$argsopt" ;;

    --compiler)              lastopt=compiler ;;
    --compiler=*)            compiler="$argsopt" ;;

    --proctype)              lastopt=proctype ;;
    --proctype=*)            proctype="$argsopt" ;;    

    *)                       error "illegal option $nextopt" ;;
    esac
done

# create the build configuration
mkcnf

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

# success
exit 0
