#!/bin/bash

#
# This shell script attempts to detect certain features of the Linux
# distribution so that the ADMXRC2 API library will build correctly.
#

function showhelp()
{
    echo 'Usage: '$0' [option ...]'
    echo ''
    echo 'Where '"'"option"'"' can be one of the following:'
    echo '  -biarch <yes | no>              Specify whether or not to build'
    echo '                                  bi-architecture for a 64-bit platform.'
    echo '  -h or -help                     Show this help.'
    echo '  -sysroot <system root>          Specify the system root (for cross-'
    echo '                                  building).'
    echo '  -brief                          Do not display banners.'
    echo ''
    echo 'To perform a cross-build, set the environment variables ARCH and CROSS_COMPILE'
    echo 'before running this script. For example:'
    echo ''
    echo '  $ ARCH=powerpc'
    echo '  $ CROSS_COMPILE=powerpc-e600-linux-gnualtivec-'
    echo '  $ export ARCH CROSS_COMPILE'
    echo '  $ PATH=/path/to/toolchain:$PATH'
    echo '  $ ./configure'
    echo ''
}

CONFIG_FILE=".build_defs"

#
# Set up default values
#
BIARCH="no"
BRIEF="no"
SYSROOT=""

AR=${CROSS_COMPILE}ar
CC=${CROSS_COMPILE}cc
LD=${CROSS_COMPILE}ld
RANLIB=${CROSS_COMPILE}ranlib

while (($# > 0)) ; do
    if [ "$1" == "-help" ] || [ "$1" == "-h" ] ; then
        showhelp
        exit 0   
    elif [ "$1" == "-biarch" ] ; then
        if [ "$2" != "yes" ] && [ "$2" != "no" ] ; then
            echo "*** $2 is not a valid choice for the -biarch option."
            echo
            showhelp
            exit 1
        fi
         BIARCH="$2"
        shift 2
    elif [ "$1" == "-sysroot" ] ; then
        SYSROOT="$2"
        shift 2
    elif [ "$1" == "-brief" ] ; then
        BRIEF="yes"
        shift 1
    else
        echo "*** Unknown option: $1"
        exit 1
    fi
done

if [ "$BRIEF" == "no" ] ; then
    echo "ADMXRC2 API library configuration script"
    echo
fi

LIBDIR="$SYSROOT/usr/lib"

if [ "$BIARCH" == "yes" ] ; then
    if [ -d "${LIBDIR}64" ] && [ -d "${LIBDIR}32" ] ; then
        # x64 Gentoo-style layout:
        #   Native libs -> $SYSROOT/usr/lib64 (and there may exist a symlink lib -> lib64)
        #   32-bit libs -> $SYSROOT/usr/lib32
        INSTALL_PATH="${LIBDIR}64"
        INSTALL_PATH32="${LIBDIR}32"
    elif [ -d "${LIBDIR}64" ] && [ -d "${LIBDIR}" ] ; then
        # x64 Fedora/RedHat-style layout:
        #   Native libs -> $SYSROOT/usr/lib64
        #   32-bit libs -> $SYSROOT/usr/lib
        INSTALL_PATH="${LIBDIR}64"
        INSTALL_PATH32="${LIBDIR}"
    elif [ -d "${LIBDIR}" ] && [ -d "${LIBDIR}32" ] ; then
        # x64 Ubuntu-style layout:
        #   Native libs -> $SYSROOT/usr/lib
        #   32-bit libs -> $SYSROOT/usr/lib32
        INSTALL_PATH="${LIBDIR}"
        INSTALL_PATH32="${LIBDIR}32"
    else
        echo "*** Cannot determine paths of system shared library directories for native and 32-bit libraries."
        echo "    Please try '-biarch no' instead."
        exit 1
    fi;
else
    if [ -d "${LIBDIR}64" ] ; then
        # x64 Fedora/RedHat-style layout:
        #   Native libs -> $SYSROOT/usr/lib64
        INSTALL_PATH="${LIBDIR}64"
    elif [ -d "${LIBDIR}" ] ; then
        # Ubuntu-style OR x86 Linux layout:
        #   Native libs -> $SYSROOT/usr/lib
        INSTALL_PATH="${LIBDIR}"
    else
        echo "*** Neither ${SYSROOT}/usr/lib nor ${SYSROOT}/usr/lib64 appear to exist, so cannot proceed."
        exit 1
    fi;
fi

if [ "$BIARCH" == "yes" ] ; then
    if [ "$ARCH" == "" ] ; then
        ARCH=`uname -m`
    fi
    if [ "$ARCH" == "x86_64" ] ; then
        ARCH32="elf_i386"
        ARCH64="elf_x86_64"
    elif [ "$ARCH" == "ppc64" ] || [ "$ARCH" == "powerpc" ] ; then
        ARCH32="elf32ppc"
        ARCH64="elf64ppc"
    else
        echo "*** Don't know how to build bi-arch for architecture '$ARCH'"
        exit 1
    fi
fi

#
# Write out the config file
#
rm -f $CONFIG_FILE
echo "#" >>$CONFIG_FILE
echo "# .build_defs" >>$CONFIG_FILE
echo "#" >>$CONFIG_FILE
echo "# Generated automatically by configure - do not modify (unless" >>$CONFIG_FILE
echo "# you know what you're doing!)" >>$CONFIG_FILE
echo "#" >>$CONFIG_FILE
echo "" >>$CONFIG_FILE

#
# Emit the paths
#
echo "System root:                       $SYSROOT"
if [ "$SYSROOT" != "" ] ; then
    echo "# The following is the system root filesystem, usually only required for" >>$CONFIG_FILE
    echo "# cross-compilation" >>$CONFIG_FILE
    echo "SYSROOT=$SYSROOT" >>$CONFIG_FILE
    echo "" >>$CONFIG_FILE
fi
if [ "$BIARCH" == "yes" ] ; then
    echo "Native library install directory:  $INSTALL_PATH"
    echo "# Specifies path where native shared object is installed when \"make install\" is run." >>$CONFIG_FILE
    echo "INSTALL_PATH=$INSTALL_PATH" >>$CONFIG_FILE
    if [ -d $INSTALL_PATH ] ; then
        echo -n
    else
        echo "+++ Warning: native library install directory does not exist"
    fi
    echo "32-bit library install directory:  $INSTALL_PATH32"
    echo "# Specifies path where 32-bit compatibility mode shared object is installed" >>$CONFIG_FILE
    echo "# when \"make install\" is run." >>$CONFIG_FILE
    echo "INSTALL_PATH32=$INSTALL_PATH32" >>$CONFIG_FILE
    if [ -d $INSTALL_PATH ] ; then
        echo -n
    else
        echo "+++ Warning: 32-bit library install directory does not exist"
    fi
    echo "" >>$CONFIG_FILE
else
    echo "Library install directory:         $INSTALL_PATH"
    echo "# Specifies path where shared object is installed when \"make install\" is run." >>$CONFIG_FILE
    echo "INSTALL_PATH=$INSTALL_PATH" >>$CONFIG_FILE
    if [ -d $INSTALL_PATH ] ; then
        echo -n
    else
        echo "+++ Warning: library install directory does not exist"
    fi
    echo "" >>$CONFIG_FILE
fi

echo "# The following sets up the compiler tools to use" >>$CONFIG_FILE
echo "Archiver:                          $AR"
echo "C compiler:                        $CC"
echo "Linker:                            $LD"
echo "Ranlib:                            $RANLIB"
echo "AR=$AR" >>$CONFIG_FILE
echo "CC=$CC" >>$CONFIG_FILE
echo "LD=$LD" >>$CONFIG_FILE
echo "RANLIB=$RANLIB" >>$CONFIG_FILE
echo "" >>$CONFIG_FILE

echo "# Specifies whether or not a 32-bit compatibility library is required" >>$CONFIG_FILE
echo "# (generally \"yes\" only when targetting a natively 64-bit architecture)" >>$CONFIG_FILE
echo "Bi-architecture build:             $BIARCH"
echo "BIARCH=$BIARCH" >>$CONFIG_FILE
if [ "$BIARCH" == "yes" ] ; then
    echo "  32-bit architecture:             $ARCH32"
    echo "ARCH32=$ARCH32" >>$CONFIG_FILE
    echo "  Native architecture:             $ARCH64"
    echo "ARCH64=$ARCH64" >>$CONFIG_FILE
fi
echo "" >>$CONFIG_FILE

if [ "$BRIEF" == "no" ] ; then
    echo
    echo "Done, output in $CONFIG_FILE".
    echo
    echo 'Now do a ''make clean all''.'
    echo
fi
