#!/bin/bash

#MINGW USERS:
#To make this script work under MinGW, change the
#top line to point to this: #!/bin/sh

#Make the non-argument config be Dead Souls
if ! [ $1 ]; then
ARG="ds"
else
ARG=$1
fi

if [ $ARG == "--help" ] ||  [ $ARG == "-h"  ]; then
echo "Available options:"
echo "A generic build:	./configure generic"
echo "For Dead Souls 2:	./configure ds"
echo "For Sapidlib:	./configure sapid"
echo "For Discworld: 	./configure dw"
echo "For Lima: 	./configure lima"
echo "For Skylib: 	./configure skylib"
echo "For TMI-2:	./configure tmi2"
echo "For Merentha:	./configure merentha"
echo "For Nightmare 3:	./configure nm3"
echo "For LPUniversity:	./configure lpuni"
echo "For Lil:		./configure lil"
echo "For Final Realms:	./configure fr"
echo "For Foundation I:	./configure foundation"
echo "For Foundation II:./configure foundation2"
echo "For Nightmare IV:	./configure nm4"
echo "----"
echo "With no options, this script defaults to Dead Souls."
exit
fi

echo "Option selected: $ARG $2"
sleep 1

./build.FluffOS $2

#if [ $ARG == "ds" ] || [ $ARG == "ds.debug" ]; then
#\cp -f ../extra/creremote/remote.c ../lib/lib/
#fi

if uname -a | grep -i "cygwin" | grep -v grep ; then
echo "Cygwin detected."
export WINDOWS='#define WINDOWS'
else
# Some stuff for Dead Souls not on Cygwin
echo "Moving around some DS specific files..."
if [ $ARG == "ds" ] || [ $ARG == "ds.debug" ]; then
if ! [ -f ../bin/mudos.bak ]; then
\rm -f ../bin/mudos.bak
\mv -f ../bin/mudos.cfg ../bin/mudos.bak
\cp -f ../bin/mudos.cfg.orig ../lib/secure/cfg/mudos.cfg
ln -s ../lib/secure/cfg/mudos.cfg ../bin/mudos.cfg
echo "File moves done"
fi
fi
fi

#start local_options check
if [ -f local_options.$ARG ]; then

if ! [ -f local_options ]; then
echo "Copying local_options.$ARG to local_options"
cp local_options.$ARG local_options
else
echo "local_options already exists. Not overwriting. Exiting."
echo "If you want to completely start over, type: make distclean"
exit
fi

else
echo "local_options.$ARG not found. Exiting."
fi
#end local_options check

if uname -a | grep "MINGW" | grep -v grep ; then
echo "MinGW detected. Adding appropriate config"
#echo " -lwsock32 -lws2_32 -lz" > system_libs
#\cp -f ../extra/mingw/configur* .
export WINDOWS='#define WINDOWS'
echo "#define MINGW" >> local_options
echo "#undef HAS_CONSOLE" >> local_options
else
echo "No MinGW config needed."
fi

if /sbin/ifconfig -a | grep "204\.209\.44" | grep -v grep ; then
echo "Wolfpaw detected."
echo "Running Wolfpaw-specific modification."
echo "When this is done, please enter the following command line:"
echo "*********"
echo " "
echo "make -j 1"
echo " "
echo "*********"
cp -f ../extra/wolfpaw/* . 
echo "#include \"../extra/wolfpaw/configure.h\"" >> local_options
else
echo "No Wolfpaw config needed."
fi

#This will be blank if there is no Cygwin or MinGW:
echo $WINDOWS >> local_options

ulimit -n 2048
export ULIMIT=`ulimit -n`
echo "#define ULIMIT "$ULIMIT >> local_options

echo "Configuration script complete."
