#!/bin/sh
# ----------------------------------------------------------------------------
# - afnix-setup                                                              -
# - afnix 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-2007 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 build directory
blddir=
# the source directory
srcdir=
# the installation directory prefix
prefix=/usr/local
# the shared directory
shrdir=$prefix/share
# the alternate directory (for etc)
altdir=$prefix
# the sdk directory
sdkdir=
# whether or not we report the option
verbose=no
# the default compiler
compiler=
# the platform os version
platvers=
platvmaj=0
platvmin=0
# the processor name
procname=
# the processor type
proctype=
# default compilation mode
ccmode=debug
# linking type
lktype=
# linking mode
lkmode=

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

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

    echo "       --help             print this help message"
    echo "       --prefix           set target directory to install"
    echo "       --shrdir           set shared directory to install"
    echo "       --altdir           set alternate directory to install"
    echo "       --sdkdir           set system development kit directory"
    echo "       --compiler         set default compiler"
    echo "       --proctype         set processor architecture"
    echo "       --dynamic          compile and link dynamically"
    echo "       --static           compile and link statically"
    exit 0
}

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

# report options
report () {
    echo "root directory      = $rootdir"
    echo "cnf directory       = $cnfdir"
    echo "source directory    = $srcdir"
    echo "prefix              = $prefix"
    echo "shared directory    = $shrdir"
    echo "alternate directory = $altdir"
    echo "sdk directory       = $sdkdir"
    echo "platform name       = $platname"
    echo "platform version    = $platvers"
    echo "processor name      = $procname"
    echo "processor type      = $proctype"
    echo "compiler            = $compiler"
    echo "compilation mode    = $ccmode"
    echo "linking  type       = $lktype"
    echo "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
    # get the fix directory name
    fxdir=$rootdir/cnf/bin/afnix-fxdir
    rootdir=`$fxdir $rootdir`
    # compute the cnf directory
    cnfdir=$rootdir/cnf
    if test ! -d "$cnfdir"; then
	error "cannot find cnf directory" $cnfdir
    fi
    # compute the build directory
    blddir=$rootdir/bld
    # 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/afnix-query
    # get the compiler version script
    vcomp=$rootdir/cnf/bin/afnix-vcomp
    # get compiler value
    compiler=`$query compiler`
    # update compiler version
    compiler=`$vcomp --compiler=$compiler`
    # get the linking type
    if test -z "$lktype"; then
	lktype=`$query lktype`
        if test "$?" != "0"; then
	    error "cannot determine linking type"
	fi
    fi
    # get the linking mode
    lkmode=`$query --default=generic lkmode`
    if test "$?" != "0"; then
	error "cannot determine linking mode"
    fi
}

# fix the configuration 
fxcnf () {
    # force directory with darwin
    if test "$platname" = "darwin"; then
	pkgdir=$blddir/pkg
	prefix=$pkgdir/usr
	shrdir=$prefix/share
	altdir=$shrdir
	if test -z "$sdkdir"; then
	    sdkdir=/Developer/SDKs/MacOSX10.4u.sdk
	fi
	if test ! -d "$sdkdir"; then
	    error "cannot find sdk directory" $sdkdir
	fi
    fi
}

# this function creates the build configuration directory
mkdef () {
    # create the target cnf directory
    trgdir=$rootdir/bld/cnf
    $cnfdir/bin/afnix-mkdir $trgdir
    # compute platform information
    platname=`$cnfdir/bin/afnix-guess -n`
    if test "$?" != "0"; then
        error "cannot determine platform name"
    fi
    platvers=`$cnfdir/bin/afnix-guess -v`
    if test "$?" != "0"; then
        error "cannot determine platform version"
    fi
    platvmaj=`$cnfdir/bin/afnix-guess -M`
    if test "$?" != "0"; then
        error "cannot determine platform major number"
    fi
    platvmin=`$cnfdir/bin/afnix-guess -m`
    if test "$?" != "0"; then
        error "cannot determine platform minor number"
    fi
    procname=`$cnfdir/bin/afnix-guess -p`
    if test "$?" != "0"; then
        error "cannot determine processor"
    fi
    if test -z "$proctype"; then
	proctype=`$cnfdir/bin/afnix-guess -t`
        if test "$?" != "0"; then
	    error "cannot determine processor type"
        fi
    fi
    machconf=$platname-$procname
}

mkcnf () {
    # install afnix-plat.mak file
    trgmak=$trgdir/afnix-plat.mak
    echo "# afnix-plat.mak"                > $trgmak
    echo "# afnix 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 "SHRDIR           = $shrdir"     >> $trgmak
    echo "ALTDIR           = $altdir"     >> $trgmak
    echo "SDKDIR           = $sdkdir"     >> $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/afnix-${compiler}.mak
    if test ! -f "$compfile"; then
	error "cannot find compiler file" $compfile
    fi
    if test ! -f $trgdir/afnix-comp.mak; then
	ln -s ../../cnf/mak/afnix-${compiler}.mak $trgdir/afnix-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 ;;
    -d | --dynamic)          lktype=dynamic ;;

    --prefix)                lastopt=prefix ;;
    --prefix=*)              oldfix="$prefix" ;
                             prefix="$argsopt" ;
	                     if test "$shrdir" = "$oldfix/share"; then
                               shrdir="$prefix/share" ;
                             fi ;;

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

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

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

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

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

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

# create the definitions
mkdef

# fix the definitions
fxcnf

# create the build configuration
mkcnf

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

# success
exit 0
