#!/bin/sh
# Simple script to create host.cfg
#

if [ "x" = "x$1" -o "x" = "x$2" ]; then
    echo "Usage: $0 (-h|-m) <configfile>"
    echo "Writes the system specific things to <configfile>"
    echo "Either -h or -m must be specified. -h means to write"
    echo "a C header file with defines and -m means to write a"
    echo "config for make."
    exit 1
fi

type=`uname`
proc=`uname -m`

# Reasonable defaults
# $COMMON_CFLAGS - the compile-flags for objects that are a part of AROS.
# $APP_CFLAGS	 - the compile-flags for applications that will run under AROS
#		   (e.g. AROS/c/#?). If you do not define this, $COMMON_CFLAGS
#		   will be used.
CC="cc"
COMMON_CFLAGS="-g"
RANLIB="ranlib"
GUI_LIBDIR=""
GUI_INCDIR=""
GUI_LIBFLAGS=""
MKDEPEND="makedepend"
FLAVOUR="emulation"

# Flags to link AROS binaries (ie. binaries which are to be started
# inside AROS). What we need is something like a shared lib: All
# references resolved but no startup-code added.
ILDFLAGS="-nostartfiles -nostdlib"

# If you need a special kind of assembler (eg. as), set $AS. If $AS is not
# set, $CC is used.
# Special flags for $AS are taken from $COMMON_AFLAGS, if it exists or
# from $COMMON_CFLAGS otherwise.
# If X11 is installed in a directory of it's own, you can specify that
# directory in $GUI_BASEDIR. $GUI_LIBDIR will then point to $GUI_BASEDIR/lib
# and $GUI_LDFLAGS will contain -L$GUI_LIBDIR. The same applies to $GUI_INCDIR.
# If only one part is in a nonstandard place (eg. the libs), you
# can set one of the $GUI_LIBDIR/GUI_INCDIRs, too.

case "$type" in
"Linux" )
	# The main compiler
	CC="gcc"
	# The CFLAGS usually to use (in addition to the includes and use flags)
	COMMON_CFLAGS="-Wall -g -O0"
	# CFLAGS to be used to link applications
	ILDFLAGS="-nostartfiles -nostdlib -Xlinker -i"
	# Which OS ?
	ARCH="linux"
	# Which CPU ?
	if [ "$proc" = "m68k" ]; then
	    KERNEL="m68k-emul"
	    # How to generate dependency files
	    MKDEPEND="makedepend -D__GNUC__ -D__mc68000__"
	else
	    KERNEL="i386"
	    MKDEPEND="makedepend -D__GNUC__ -Di386"
	fi
	# Search this directory for GUI libs
	GUI_LIBDIR="/usr/X11R6/lib"
	# Link with these GUI libs
	GUI_LIBFLAGS="-lX11"
	# The flags usually passed to the assembler
	COMMON_AFLAGS="-x assembler-with-cpp"
	# Flags to compile objects for shared libs
	SHARED_CFLAGS="-fpic"
	# Command to generate shared libs
	SHARED_AR="ld -shared -o"
	;;
"HP-UX" )
	COMMON_CFLAGS="-Aa -g"
	RANLIB="true"
	ARCH="hppa"
	KERNEL="hppa-emul"
	;;
"OSF1" )
	ARCH="decunix"
	KERNEL="axp-emul"
	;;
"FreeBSD" )
	ARCH="freebsd"
	KERNEL="i386"
	COMMON_CFLAGS="-Wall -O2"
	ILDFLAGS="-nostartfiles -nostdlib -Xlinker -r -Xlinker -n -Xlinker -e -Xlinker _entry"
	GUI_BASEDIR="/usr/X11R6"
	GUI_LIBFLAGS="-lX11"
	AS="gcc"
	CC="gcc"
	COMMON_AFLAGS="-x assembler-with-cpp"
	SHARED_CFLAGS=""
	SHARED_AR=""
	MKDEPEND="makedepend -D__GNUC__ -Di386"
	;;
"AmigaOS" )
	CC="gcc"
	COMMON_CFLAGS="-Wall -Wno-uninitialized -O2 -m68020 -fomit-frame-pointer"
	ARCH="amiga"
	KERNEL="m68k-native"
	MKDEPEND="makedepend -D__GNUC__ -D_AMIGA -DAMIGA"
# Sometimes, a .asm file contains versions of a certain routine for
# different processors. Use -m68060 as a highest common denominator.
# It is up to the coder of these routines to keep track of their
# instructions.
	COMMON_AFLAGS="-m68060 -x assembler-with-cpp"
	FLAVOUR="native"
	;;
esac

if [ -n "$FLAVOURS" ]; then
    echo "On your hardware, AROS is available in these flavours:"
    echo
    t=1
    for flavour in $FLAVOURS ; do
	echo "    $t. $flavour"
	t=`expr $t + 1`
    done
    echo
    echo "Which flavour do you want (enter number) ?"
    read flavour
#   echo $flavour
    flavour=`echo $FLAVOURS | cut "-d " -f$flavour`
    if [ -z $flavour ]; then
	echo "Invalid input."
	exit 10
    fi
    FLAVOUR=$flavour
fi

case "$1" in
"-m" )
    echo "# This file is automatically generated if it doesn't exist" > $2
    echo "# DO NOT EDIT" >> $2
    echo "# Edit \$(TOP)/configure instead and delete this file" >> $2
    echo >> $2
    echo "SYS_CC = $CC" >> $2
    echo "COMMON_CFLAGS = $COMMON_CFLAGS" >> $2
    echo "ILDFLAGS = \$(CFLAGS) $ILDFLAGS" >> $2
    echo "RANLIB = $RANLIB" >> $2
    echo "ARCH = $ARCH" >> $2
    echo "KERNEL = $KERNEL" >> $2
    echo "FLAVOUR = $FLAVOUR" >> $2

    if [ -z "$AS" ]; then
	echo "SYS_AS = $CC" >> $2
    else
	echo "SYS_AS = $AS" >> $2
    fi
    if [ -z "$COMMON_AFLAGS" ]; then
	echo "COMMON_AFLAGS = $COMMON_CFLAGS" >> $2
    else
	echo "COMMON_AFLAGS = $COMMON_AFLAGS" >> $2
    fi

    if [ -z "$APP_CFLAGS" ]; then
	echo "APP_CFLAGS = $COMMON_CFLAGS" >> $2
    else
	echo "APP_CFLAGS = $APP_CFLAGS" >> $2
    fi

    if [ -n "$GUI_BASEDIR" ]; then
	GUI_LIBDIR=$GUI_BASEDIR/lib
	GUI_INCDIR=$GUI_BASEDIR/include
    fi

    if [ -n "$GUI_LIBDIR" ]; then
	echo "GUI_LIBDIR = $GUI_LIBDIR" >> $2
	echo "GUI_LDFLAGS = -L$GUI_LIBDIR" >> $2
    else
	echo "GUI_LIBDIR = /usr/lib" >> $2
	echo "GUI_LDFLAGS =" >> $2
    fi
    if [ -n "$GUI_INCDIR" ]; then
	echo "GUI_INCDIR = $GUI_INCDIR" >> $2
	echo "GUI_CCFLAGS = -I$GUI_INCDIR" >> $2
    else
	echo "GUI_INCDIR = /usr/include" >> $2
	echo "GUI_CCFLAGS =" >> $2
    fi

    echo "GUI_LIBFLAGS = $GUI_LIBFLAGS" >> $2
    echo "MKDEPEND = $MKDEPEND" >> $2

    if [ -n "$SHARED_CFLAGS" ]; then
	echo "SHARED_CFLAGS = $SHARED_CFLAGS" >> $2
	echo "SHARED_AR = $SHARED_AR" >> $2
    fi

    if [ -d config ]; then
	cd config ; make TOP=.. CURDIR=./config setup
    fi
    ;;
"-h" )
    echo "#ifndef AROS_CONFIG_H" > $2
    echo "#define AROS_CONFIG_H" >> $2
    echo "/*" >> $2
    echo "    This file is automatically generated if it doesn't exist" >> $2
    echo "    DO NOT EDIT" >> $2
    echo "    Edit \$(TOP)/configure instead and delete this file" >> $2
    echo "*/" >> $2
    echo >> $2
    echo "#define AROS_FLAVOUR_NATIVE           2" >> $2
    echo "#define AROS_FLAVOUR_STANDALONE       3" >> $2
    echo "#define AROS_FLAVOUR_EMULATION        4" >> $2
    echo "#define AROS_FLAVOUR_LINKLIB          5" >> $2
    echo >> $2
    case "$FLAVOUR" in
    "native" )
	echo "#define AROS_FLAVOUR  AROS_FLAVOUR_NATIVE" >> $2
	;;
    "standalone" )
	echo "#define AROS_FLAVOUR  AROS_FLAVOUR_STANDALONE" >> $2
	;;
    "emulation" )
	echo "#define AROS_FLAVOUR  AROS_FLAVOUR_EMULATION" >> $2
	;;
    "linklib" )
	echo "#define AROS_FLAVOUR  AROS_FLAVOUR_LINKLIB" >> $2
	;;
    esac
    echo >> $2
    echo "#endif /* AROS_CONFIG_H */" >> $2
    ;;
esac

