#!/bin/sh
#
# xrick/config
#
# This source code file is part of the xrick distribution, and as such is
# copyrighted. Please read the README file in the root directory of the
# distribution for information about copyrights, trademarks, license,
# disclaimers, etc.
#

### YOU CAN EDIT THE OPTIONS BELOW ###

# Graphics
GFX=ST # [ST/PC]

# Joystick
JOYSTICK=YES # [YES/NO]

# Sound
SOUND=YES # [YES/NO]

# Cheats
CHEATS=YES # [YES/NO]

# Development tools
DEVTOOLS=NO # [YES/NO]

# Debug
# This activates a set of pre-defined options (more details in debug.h)
DEBUG=NO # [YES/NO]

### DO NOT EDIT BELOW THIS LINE ###

VERSION="010808"
TARGET=$(uname -s)

echo "Configure xrick version \"$VERSION\" for target \"$TARGET\""

ROOTDIR=`pwd`

CFLAGS=
LDFLAGS=

CFLAGS="$CFLAGS -g -ansi -pedantic -Wall -W -O2" # add -g to debug
CFLAGS="$CFLAGS -I $ROOTDIR/include"
CFLAGS="$CFLAGS -DVERSION=\\\"$VERSION\\\""

# SDL
SDLVER=`sdl-config --version 2>&1`
if [ $? -ne 0 ]; then
  echo "Error: SDL not found"
  echo "Abort"
  exit 1
else
  echo "SDL version $SDLVER detected"
  CFLAGS="$CFLAGS `sdl-config --cflags`"
  LDFLAGS="$LDFLAGS `sdl-config --libs`"
fi

POSDOT=`expr index $SDLVER '.'`
POSDOTp=`expr $POSDOT - 1`
POSDOTn=`expr $POSDOT + 1`
SDL_MAJ=`expr substr $SDLVER 1 $POSDOTp`
LEN=`expr length $SDLVER`
LEN=`expr $LEN - $POSDOT`
SDLVER=`expr substr $SDLVER $POSDOTn $LEN`
POSDOT=`expr index $SDLVER '.'`
POSDOTp=`expr $POSDOT - 1`
POSDOTn=`expr $POSDOT + 1`
SDL_MIN=`expr substr $SDLVER 1 $POSDOTp`
LEN=`expr length $SDLVER`
LEN=`expr $LEN - $POSDOT`
SDL_MIC=`expr substr $SDLVER $POSDOTn $LEN`

case "$TARGET" in
  cygwin* | CYGWIN*)
    # version 1.2.1 is required to compile under Cygwin
    SDL_MAJ_REQ=1
    SDL_MIN_REQ=2
    SDL_MIC_REQ=1
    ;;
  *)
    # version prior to 1.1.8 lack various things such as SDL_DISABLE
    SDL_MAJ_REQ=1
    SDL_MIN_REQ=1
    SDL_MIC_REQ=8
    ;;
esac

if [ $SDL_MAJ -lt $SDL_MAJ_REQ ]; then
  echo "Error: SDL version $SDL_MAJ_REQ.$SDL_MIN_REQ.$SDL_MIC_REQ or later is required"
  echo "Abort"
  exit 1
fi

if [ $SDL_MAJ -eq $SDL_MAJ_REQ -a \
     $SDL_MIN -lt $SDL_MIN_REQ ]; then
  echo "Error: SDL version $SDL_MAJ_REQ.$SDL_MIN_REQ.$SDL_MIC_REQ or later is required"
  echo "Abort"
  exit 1
fi

if [ $SDL_MAJ -eq $SDL_MAJ_REQ -a \
     $SDL_MIN -eq $SDL_MIN_REQ -a \
     $SDL_MIC -lt $SDL_MIC_REQ ]; then
  echo "Error: SDL version $SDL_MAJ_REQ.$SDL_MIN_REQ.$SDL_MIC_REQ or later is required"
  echo "Abort"
  exit 1
fi

# Problems w/BeOS and focus management? I've had reports that the game
# would constantly go to PAUSED under BeOS if focus management is enabled.
case "$TARGET" in
  BeOS*)
    # BeOS: do not enable focus management
    echo "BeOS detected, disable focus management"
    ;;
  *)
    # anything else: enable focus management
    CFLAGS="$CFLAGS -DENABLE_FOCUS"
    ;;
esac

# Graphics
cd src
if [ $GFX = PC ]; then
  echo "Graphics: IBM PC CGA"
  rm -f dat_sprites.c dat_tiles.c dat_pics.c xrick.ico
  ln -s dat_spritesPC.c dat_sprites.c
  ln -s dat_tilesPC.c dat_tiles.c
  ln -s dat_picsPC.c dat_pics.c
  ln -s xrickPC.ico xrick.ico
elif [ $GFX = ST ]; then
  echo "Graphics: Atari ST"
  rm -f dat_sprites.c dat_tiles.c dat_pics.c xrick.ico
  ln -s dat_spritesST.c dat_sprites.c
  ln -s dat_tilesST.c dat_tiles.c
  ln -s dat_picsST.c dat_pics.c
  ln -s xrickST.ico xrick.ico
else
  echo "Error: graphics $GFX is not supported"
  echo "Abort"
  cd ..
  exit 1
fi
cd ..
CFLAGS="$CFLAGS -DGFX$GFX"

# Enable/disable joystick support
if [ $JOYSTICK = YES ]; then
  echo "Joystick support is enabled"
  CFLAGS="$CFLAGS -DENABLE_JOYSTICK"
  XTRAOBJ="$XTRAOBJ sysjoy.o"
else
  echo "Joystick support is disabled"
fi

# Enable/disable sound support
if [ $SOUND = YES ]; then
  echo "Sound support is enabled"
  CFLAGS="$CFLAGS -DENABLE_SOUND"
  XTRAOBJ="$XTRAOBJ dat_snd.o syssnd.o"
else
  echo "Sound support is disabled"
fi

# Enable/disable cheats
if [ $CHEATS = YES ]; then
  echo "Cheats are enabled"
  CFLAGS="$CFLAGS -DENABLE_CHEATS"
else
  echo "Cheats are disabled"
fi

# Enable/disable development tools
if [ $DEVTOOLS = YES ]; then
  echo "Devtools are enabled"
  CFLAGS="$CFLAGS -DENABLE_DEVTOOLS"
  XTRAOBJ="$XTRAOBJ devtools.o"
fi

# Debug
if [ $DEBUG = YES ]; then
  echo "Debug is enabled (see include/debug.h for options)"
  CFLAGS="$CFLAGS -DDEBUG"
else
  echo "Debug is disabled"
fi

# On Windows, set icon
case "$TARGET" in
  cygwin* | mingw* | CYGWIN* | MINGW*)
    echo "Microsoft Windows detected, set resources file"
    XTRAOBJ="$XTRAOBJ xrick.res"
    ;;
  *)
    ;;
esac

# Create Makefile.global
echo "Create Makefile.global"
cat > Makefile.global << EOF
#
# xrick/Makefile.global
#
# This source code file is part of the xrick distribution, and as such is
# copyrighted. Please read the README file in the root directory of the
# distribution for information about copyrights, trademarks, license,
# disclaimers, etc.
#

ROOTDIR = $ROOTDIR
CC = gcc
CPP = gcc -E
CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS
XTRAOBJ = $XTRAOBJ

# eof
EOF

echo "Done"
echo "xrick is now configured ; run make to build"

# eof



