#!/bin/sh
#############################################################################
# "configure" script for Drunken Doors
# Version 0.1 - 10/Feb/2001
# by Rezine
#
# could still be some buggy... compiling and stuff by your own would
# surely be better at the moment. ;)
# If this script should fail... sorry... eheh
#############################################################################

# Some config vars
RELEASE_NAME="dRUNkENjOiN 1.0"
BINARY_NAME="djoin"
SOURCE_FILES="djoin.c parseme.c"
GRAFICS_FILES="djoinhdr.gfx djoinftr.gfx djoinhelp.gfx"
CONFIG_FILES="djoin.cfg djoin.aliases"

# Some "default" directories
DOORS_DIR="doors"
CONFIGS_DIR="configs"
DISPLAY_DIR="display/iso"

if [ -z $DAYDREAM ]; then
	echo "*** ERROR : Your '$DAYDREAM' variable is not set."
	exit
fi

echo "***********************************************************************"
echo "[dRUNKEN] Door installer V0.1 by Rezine/Drunken"
echo
echo "We will now try to install $RELEASE_NAME on your system."
echo "If some error occurs, chances are, that you have to install manually :)"
echo
echo "In order to get anything working, we need some basic informations"
echo "about your system. We will ask you some questions, and the default"
echo "answers are in square brackets ([]). If you want to use the defaults"
echo "(which are common for a default DD installation), just press ENTER"
echo "on that prompt."
echo
echo "If this script should fail, you're probably on your own ;)"
echo "***********************************************************************"
echo
echo -n "Do you want to continue installing $RELEASE_NAME [y] ? "
read WANT_INSTALL

if [ -z $WANT_INSTALL ]; then
	WANT_INSTALL="Y"
fi

if [ $WANT_INSTALL != "y" ] && [ $WANT_INSTALL != "Y" ]; then
	echo
	echo "*** ERROR : User aborted installation procedure."
	echo
	exit
fi

echo "Now checking your systems configuration. Hang on."
echo -n "Checking for DayDream Version ... "

# awk out DD version :)

DD_VER=$($DAYDREAM/daydream --help | grep DayDream | awk {'print $3'};)
echo $DD_VER

# Are we using a daydream version < 2.11?
# If yes, it can become some hairy.. hehe better upgrade =P

if [ $DD_VER != "2.11" ] && [ $DD_VER != "2.12" ]; then
	echo "Lib config script can not be used, trying alternative."
	echo "Looking for DD libraries ... "
	
	if [ -f /usr/lib/ddlib.so ]; then
		echo "Found libs in /usr/lib... GOOD!"
		LD_SWITCH="-L/usr/lib -ldd"
	elif [ -f /usr/local/lib/ddlib.so ]; then
		echo "Found libs in /usr/local/lib... GOOD!"
		LD_SWITCH="-L/usr/local/lib -ldd"
	elif [-f $DAYDREAM/lib/libdd.so ]; then
		echo "Found libs in $DAYDREAM/lib... GOOD!"
		LD_SWITCH="-L/home/bbs/lib -ldd"
	else
		echo "Found libs nowhere i could imagine in... BAD!"
		echo "You can enter the path to your libs now or simply press ENTER to abort"
		echo -n "Path to DD Libs : "
		read INP_LIBS
		if [ -f $INP_LIBS/libdd.so ]; then
			echo "Found libs in $INP_LIBS... GOOD!"
			LD_SWITCH="-L/$INP_LIBS -ldd"
		else
			echo "Please make a directory $DAYDREAM/libs and copy the libdd files in there."
			echo "After you have done that, run this script again."
			exit
		fi
	fi
	
	echo "Looking for DD header files ..."
	if [ -f /usr/include/dd.h ]; then
		echo "Header files seem to be in /usr/include... GOOD!"
		INCLUDE_DIR="-I/usr/include"
	elif [ -f /usr/local/include/dd.h ]; then
		echo "Header files seem to be in /usr/local/include... GOOD!"
		INCLUDE_DIR="-I/usr/local/include"
	elif [ -f $DAYDREAM/include/dd.h ]; then
		echo "Header files seem to be in $DAYDREAM/include... GOOD!"
		INCLUDE_DIR="-I$DAYDREAM/include"
	else
		echo "Found headers nowhere i could imagine in... BAD!"
		echo "You can enter the path to your header files now or simply press ENTER to abort"
		echo -n "Path to header files : "
		read INP_HEADER
		if [ -f $INP_HEADER/dd.h ]; then
			echo "Header files seem to be in $INP_LIBS... GOOD!"
			INCLUDE_DIR="-I/$INP_HEADER"
		else
			echo "Please make a directory $DAYDREAM/include and copy the header in there."
			echo "After you have done that, run this script again."
			exit
		fi
	fi
	
else
	echo "Using lib config script in $DAYDREAM/bin/daydream-config"
	echo -n "Compiler library switches... "
	LD_SWITCH=$($DAYDREAM/bin/daydream-config --libs)
	echo $LD_SWITCH
	echo -n "Compiler header file switches... "
	INCLUDE_DIR=$($DAYDREAM/bin/daydream-config --cflags)
	echo $INCLUDE_DIR
fi

# Uff... hehe. I hate Bourne shells :)

echo

echo "Where should i copy the door to?"
echo -n "[$DAYDREAM/$DOORS_DIR] : "
read INP_DOOR

if [ ! -z $INP_DOOR ]; then
	DOORS_DIR=$INP_DOOR
fi

echo "Where should the graphics files go?"
echo -n "[$DAYDREAM/$DISPLAY_DIR] : "
read INP_GFX

if [ ! -z $INP_GFX ]; then
	DISPLAY_DIR=$INP_GFX
fi

echo
echo "*** Creating ./Makefile..."

# hmm, now we are creating the makefile

echo "# Makefile for $RELEASE_NAME" > ./Makefile
echo "# Created by Drunken Door installer 0.1 ;)" >> ./Makefile
echo "" >> ./Makefile
echo "CC=gcc" >> ./Makefile
echo "TARGET=$BINARY_NAME" >> ./Makefile
echo "LDFLAGS=$LD_SWITCH $INCLUDE_DIR" >> ./Makefile
echo "SWITCHES=-Wall" >> ./Makefile
echo "" >> /.Makefile
echo "djoin :" >> ./Makefile
echo '		$(CC) $(LDFLAGS) $(SWITCHES) -o $(TARGET)' $SOURCE_FILES >> ./Makefile
echo '		@echo' >> ./Makefile
echo '		@echo "Now, to install '$RELEASE_NAME' please type make install"' >> ./Makefile
echo '		@echo' >> ./Makefile
echo "" >> ./Makefile
echo "install :" >> ./Makefile
echo "		cp -f $CONFIG_FILES $DAYDREAM/$CONFIGS_DIR" >> ./Makefile
echo "		cp -f $GRAFICS_FILES $DAYDREAM/$DISPLAY_DIR" >> ./Makefile
echo "		cp -f $BINARY_NAME $DAYDREAM/$DOORS_DIR" >> ./Makefile
echo '		@echo "Please make the door entry in daydream.cfg now and compile it" ' >> ./Makefile
echo "clean :" >> ./Makefile
echo '		rm -f *.o $(TARGET) *.core' >> ./Makefile
echo "" >> ./Makefile
echo "# Makefile EOF" >> ./Makefile

