#! /bin/sh

# chicken-config - by Douq Quale with modifications by felix

guiflags="-mwindows -e _mainCRTStartup"
usage="Usage: $0 options ...

Print compiler flags for compiling and linking the C output
from the chicken Scheme compiler version 1.63.

Options:  -cc           default C compiler
          -cxx          default C++ compiler
	  -cflags	C compiler flags
          -gui-cflags   C compiler flags for generating Win32 GUI binary (Cygwin + Mingw32 only)
	  -embedded     extra C compiler flags for generating embedded executable
	  -static       generate statically linked executable (specify before '-[...-]cflags and -[...-]libs)
          -shared       extra C compiler and link flags for shared library object (specify before '-[...-]cflags and -[...-]libs)
	  -libs		link flags for chicken library
          -gui-libs     link flags for linking Win32 GUI library (Cygwin + Mingw32 only)
	  -help		this text
	  -home		chicken home directory for installed .scm library files
          -lib-home     chicken extension library directory
 	  -prefix	chicken installation prefix directory
	  -unsafe       select unsafe libraries (specify before '-[...-]libs' option)
	  -version	chicken compiler version"

# We glob all the flags into here so that the output from
# chicken-config is on one line
FLAGS=""

# Default is shared compile or link.  When we have a libtool compile,
# we get a static library (ex. libstuffed-chicken.a) and a shared
# library (ex. libstuffed-chicken.so, libstuffed-chicken.dll.a, etc.).
# On most platforms, if we tried to link with -lstuffed-chicken for
# example, we would be linking against the shared library.  So for
# static links, we explicitly list out libstuffed-chicken.a.
PRE="-l"
SUF=""

# Generate an -Iincludedir flag only if chicken.h isn't in a standard
# include directory.
case "/usr/include" in
    /usr/include|""|/usr/local/include) INCLUDEDIR=;;
    *) INCLUDEDIR=-I/usr/include;;
esac

# Generate an -Llibdir flag only if the chicken libraries aren't in a
# standard library directory.
case "/usr/lib" in
    /usr/lib|"") LIBDIR=;;
    *) LIBDIR=-L/usr/lib;;
esac 

UNSAFE=""
SHARED=""
CCOPTS=""
LIBOPTS="" # probably should use something like "libtool -static"
MORELIBS="-ldl -lm  -ldl "

while :; do
  case "$1" in
    -cc|--cc)
        FLAGS="$FLAGS gcc ";;

    -cxx|--cxx)
        FLAGS="$FLAGS g++ ";;

    -c|--c|-cf|--cf|-cfl|--cfl|-cfla|--cfla|-cflag|--cflag|-cflags|--cflags)
	FLAGS="$FLAGS $CCOPTS -O2 -DC_STACK_GROWS_DOWNWARD=1 -DC_INSTALL_LIB_HOME="/usr/lib/chicken" -DC_USE_C_DEFAULTS $INCLUDEDIR ";;

    -gui-c|-gui-cflags|--gui-c|--gui-cflags)
	FLAGS="$FLAGS $CCOPTS -O2 -DC_STACK_GROWS_DOWNWARD=1 -DC_INSTALL_LIB_HOME="/usr/lib/chicken" -DC_USE_C_DEFAULTS $INCLUDEDIR -DC_WINDOWS_GUI ";;

    -em|-embed|-embedded|--embedded)
	FLAGS="$FLAGS -DC_EMBEDDED ";;

    -s|-sh|-shared|--shared)
	CCOPTS=" -DC_SHARED -fPIC -shared -DPIC "
	LIBOPTS=""
	PRE="-l"
	SUF=""
	SHARED=yes;;

    -st|-static|--static)
        CCOPTS=" -DC_NO_PIC_NO_DLL "
	PRE="/usr/lib/lib"
        SUF=".a"
	MORELIBS="-ldl -lm";;

    -l|--l|-li|--li|-lib|--lib|-libs|--libs)
	if test -z "$UNSAFE"; then
  	  FLAGS="$FLAGS $LIBOPTS $LIBDIR ${PRE}chicken${SUF} ${PRE}stuffed-chicken${SUF} ${PRE}srfi-chicken${SUF} ${MORELIBS} "
        else
          FLAGS="$FLAGS $LIBOPTS $LIBDIR ${PRE}uchicken${SUF} ${PRE}ustuffed-chicken${SUF} ${PRE}usrfi-chicken${SUF} ${MORELIBS} "
        fi;;	  

    -gui-l|--gui-l|-gui-libs|--gui-libs)
	if test -z "$UNSAFE"; then
  	  FLAGS="$FLAGS $guiflags $LIBOPTS $LIBDIR ${PRE}win32-gui-chicken${SUF} ${MORELIBS} "
        else
          FLAGS="$FLAGS $guiflags $LIBOPTS $LIBDIR ${PRE}uwin32-gui-chicken${SUF} ${MORELIBS} "
        fi;;	  

    -he|--he|-hel|--hel|-help|--help)
	echo "$usage"; exit 0;;

    -ho|--ho|-hom|--hom|-home|--home)
	FLAGS="CHICKEN_HOME=/usr/share/chicken $FLAGS";;

    -lib-ho|--lib-ho|-lib-hom|--lib-hom|-lib-home|--lib-home)
	FLAGS="CHICKEN_LIB_HOME=/usr/lib/chicken $FLAGS";;

    -v|--v|-ve|--ve|-ver|--ver|-vers|--vers|-versi|--versi|-versio|--versio|-version|--version|-build|--build)
	echo 1.63; exit 0;;
 
    -p|--p|-pr|--pr|-pre|--pre|-pref|--pref|-prefi|--prefi|-prefix|--prefix)
 	FLAGS="/usr $FLAGS";;

    -u|--u|-unsafe|--unsafe)
        UNSAFE=yes;;

    *)
	echo "$usage" 1>&2; exit 1;;
  esac
  shift
  if test $# -eq 0; then 
    echo $FLAGS
    exit 0; 
  fi
done
