#!/bin/sh
#
# Configuration script for Stats.
#
# Denora (c) 2004-2013 Denora Team
# Contact us at info@denorastats.org
#
# This program is free but copyrighted software; see the file COPYING for
# details.
#
# Based on the original code of Anope by Anope Team.
# Based on the original code of Thales by Lucas.
#
# 
#
###########################################################################


###########################################################################
# Functions
###########################################################################

echo2 () {
	$ECHO2 "$*$ECHO2SUF"	# these are defined later
}

Show_Banner () {
	if [ -f .BANNER ] ; then
		. ./version.in
		clear
		cat .BANNER | sed "s/CURVER/$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH $VERSION_EXTRA/" | sed "s/CURSHORTVER/$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH/" | more
	fi
	echo ""
	echo "Beginning Stats configuration."
	echo ""
}

Run_Config () {
	WITH_BIN=""
	WITH_DATA=""
	WITH_RUN=""
	WITH_PERM=""
	WITH_MYSQL=""
	WITH_MYSQL_BT=""
	WITH_MEMORYDEBUG=""
	WITH_ENCRYPT=""
	WITH_THREADING=""
   
	if [ "$BINDEST" != "" ] ; then
		WITH_BIN=" --with-bindir=$BINDEST"
		WITH_DATA=" --with-datadir=$DATDEST"
	fi
	
	if [ "$DATDEST" != "" ] ; then
		WITH_DATA=" --with-datadir=$DATDEST"
	fi
	
	if [ "$RUNGROUP" != "" ] ; then
		WITH_RUN="  --with-rungroup=$RUNGROUP"
	fi
	
	if [ "$UMASK" != "" ] ; then
		WITH_PERM=" --with-permissions=$UMASK"
	fi

	if [ "$MYSQL" = "no" ] ; then
		WITH_MYSQL=" --without-mysql"
	fi

	if [ "$MYSQL_BT" = "yes" ]; then
		if [ "$MYSQL" = "no" ] ; then
			echo "MySQL Backtrace disabled as denora will be compiled without mysql support"
		else
			WITH_MYSQL_BT=" --enable-mysqlbt"
		fi
	fi

	if [ "$DMEMORY" = "yes" ] ; then
		WITH_MEMORYDEBUG=" --enable-mtrace"
	fi
	if [ "$ENCRYPT" = "yes" ] ; then
		WITH_ENCRYPT=" --enable-crypt"
	fi
	if [ "$THREAD" = "yes" ] ; then
		WITH_THREADING=" --enable-threading"
	fi
  
	echo "./configure $WITH_BIN $WITH_DATA $WITH_RUN $WITH_PERM $WITH_MYSQL $WITH_MEMORYDEBUG $WITH_ENCRYPT $WITH_THREADING $WITH_MYSQL_BT"
	
	./configure $WITH_BIN $WITH_DATA $WITH_RUN $WITH_PERM $WITH_MYSQL $WITH_MEMORYDEBUG $WITH_ENCRYPT $WITH_THREADING $WITH_MYSQL_BT
	if [ "$REGSCRIPT" = "yes" ] ; then
		src/bin/register
	fi
}

exists () {			 # because some shells don't have test -e
	if [ -f $1 -o -d $1 -o -p $1 -o -c $1 -o -b $1 ] ; then
		return 0
	else
		return 1
	fi
}

Load_Cache () {
	if [ -f config.cache -a -r config.cache -a ! "$NOCACHE" ] ; then

		echo "Using defaults from config.cache. To ignore, rm config.cache"
		. ./config.cache


		CAN_QUICK="yes"
		if [ "$BINDEST" = "" ] ; then
			BINDEST=$HOME/stats
		fi
		if [ "$DATDEST" = "" ] ; then
			DATDEST=$HOME/stats
		fi
		if [ "$MYSQL" = "" ] ; then
			MYSQL="yes"
                fi
		if [ "$SQLITE" = "" ] ; then
			SQLITE="yes"
		fi
		if [ "$MYSQL_BT" = "" ] ; then
			MYSQL_BT="no"
		fi
		if [ "$DMEMORY" = "" ] ; then
			DMEMORY="no"
		fi
		if [ "$REGSCRIPT" = "" ] ; then
			REGSCRIPT="no"
		fi
		if [ "$ENCRYPT" = "" ] ; then
			ENCRYPT="no"
		fi
		if [ "$THREAD" = "" ] ; then
			THREAD="no"
		fi
	else
		CAN_QUICK="no"
	fi
}

###########################################################################
# Init values
###########################################################################

BINDEST=$HOME/stats
DATDEST=$HOME/stats
RUNGROUP=
UMASK=
DMEMORY="no"
ENCRYPT="yes"
NOCACHE=""
SHOW_INTRO="yes"
CAN_QUICK="no"
REGSCRIPT="no"
MYSQL="yes"
MYSQL_BT="no"
THREAD="no"
SQLITE="yes"

###########################################################################
# Check out the options
###########################################################################
while [ $# -ge 1 ] ; do
	if [ $1 = "--help" ] ; then
		echo "Config utility for Denora"
		echo "-----------------------------"
		echo "Syntax:	./Config [options]"
		echo "-nocache	Ignore settings saved in config.cache"
		echo "-nointro	Skip intro (release notes, etc)"
		echo "-quick	Skip questions, go straight to configure"
		exit 0
	elif [ $1 = "-nocache" ] ; then
		NOCACHE="1"
	elif [ $1 = "-nointro" ] ; then
		SHOW_INTRO="no"
	elif [ $1 = "-quick" -o $1 = "-q" ] ; then
		Load_Cache
		if [ $CAN_QUICK = "yes" ] ; then
			Run_Config
		else
			echo "Can't find cache file, aborting..."
		fi
		exit 0
	fi
	shift 1
done

###########################################################################
# Load the cache
###########################################################################
if [ ! "$NOCACHE" ] ; then
	Load_Cache
fi

###########################################################################
# Show the banner
###########################################################################
if [ $SHOW_INTRO = "yes" ] ; then
	Show_Banner
else
	echo ""
fi

###########################################################################
# Ask the user anything we need to know ahead of time.
###########################################################################
export ok INPUT

####

if [ "`id -u`" = "0" ] ; then
	echo "ERROR: You can NOT run Config as root, nor run Denora as root"
	echo ""
	exit 0
fi


# need this file for configure
touch install-sh

ok=0
echo "Note: press Return for the default, or enter a new value."
echo "In what directory do you want the binaries to be installed?"
while [ $ok -lt 1 ] ; do
	echo "[$BINDEST] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$BINDEST
	fi
	if [ ! -d "$INPUT" ] ; then
		if exists "$INPUT" ; then
			echo "$INPUT exists, but is not a directory!"
		else
			echo "$INPUT does not exist.  Create it?"
			echo "[y] "
			read YN
			if [ "$YN" != "n" ] ; then
				if mkdir -p $INPUT ; then
					ok=1
				fi
			fi
		fi
	elif exists "$INPUT/include/denora.h" ; then
		echo "You cannot use the stats source directory as a target directory."
	else
		ok=1
	fi
done
BINDEST=$INPUT
DATDEST=$INPUT
echo ""

####

ok=0
echo "Where do you want the data files to be installed?"
while [ $ok -lt 1 ] ; do
	echo "[$DATDEST] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$DATDEST
	fi
	if [ ! -d "$INPUT" ] ; then
		if exists "$INPUT" ; then
			echo "$INPUT exists, but is not a directory!"
		else
			echo "$INPUT does not exist.  Create it?"
			echo "[y] "
			read YN
			if [ "$YN" != "n" ] ; then
				if mkdir -p $INPUT ; then
					ok=1
				fi
			fi
		fi
	elif exists "$INPUT/include/denora.h" ; then
		echo "You cannot use the stats source directory as a target directory."
	else
		ok=1
	fi
done
DATDEST=$INPUT
echo ""

####


OLD_RUNGROUP="$RUNGROUP"
if [ "$RUNGROUP" ] ; then
	echo "Which group should all Stats data files be owned by?  (If Stats"
	echo "should not force files to be owned by a particular group, type "\"none\"
	echo "(without the quotes) and press Return.)"
else
	echo "Which group should all Stats data files be owned by?  (If Stats"
	echo "should not force files to be owned by a particular group, just press"
	echo "Return.)"
fi
echo "[$RUNGROUP] "
if read INPUT ; then : ; else echo "" ; exit 1 ; fi
if [ "$INPUT" ] ; then
	lcfile=`echo $INPUT | tr A-Z a-z`
	if [ "$INPUT" = "none" ] ; then
		RUNGROUP=""
	elif [ $lcfile = "root" ] ; then
		RUNGROUP=""
		echo "Can not use root group to run denora"
		exit 0;
	else
		RUNGROUP="$INPUT"
	fi
fi
echo ""

####

if [ ! "$UMASK" -o "$RUNGROUP" != "$OLD_RUNGROUP" ] ; then
	if [ "$RUNGROUP" ] ; then
		UMASK=007
	else
		UMASK=077
	fi
fi

ok=0
echo "What should the default umask for data files be (in octal)?"
echo "(077 = only accessible by owner; 007 = accessible by owner and group)"
while [ $ok -lt 1 ] ; do
	echo "[$UMASK] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$UMASK
	fi
	if [ "$INPUT" = "000" ] ; then
		echo "Invalid UMASK please enter a valid Umask";
	elif [ `echo "$INPUT" | grep -c '[^0-7]'` -gt 0 ] ; then
		echo "$UMASK is not a valid octal number!"
	else
		if [ "`echo $INPUT | cut -c1`" != "0" ] ; then
			INPUT=0$INPUT
		fi
		ok=1
	fi
done
UMASK=$INPUT
echo ""

####

ok=0
DEF=yes
echo "Allow Denora to automatically check for mysql client development libraries?"
echo "unless you get errors with make, there is no need to change this setting."
echo "WARNING: You NEED the mysql client devel libraries installed on your system"
echo "         in order to compile Denora with SQL support."
echo "         On Debian/Ubuntu systems for example, you need to install the"
echo "         libmysqlclient15-dev package or similar."
while [ $ok -lt 1 ] ; do
	echo "[$MYSQL] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$MYSQL
	fi
	case $INPUT in
		n*|N*)
			MYSQL="no"
			ok=1
			;;
		y*|Y*)
			MYSQL="yes"
			ok=1
			;;
		*)
			echo "Please enter 'yes' or 'no'."
			;;
	esac
done
echo ""

####

if [ "$MYSQL" = "yes" ]; then
	ok=0
	DEF=no
	echo "Do you want to backtrace mysql queries?"
	echo "WARNING: This will make your logs grow hugely."
	while [ $ok -lt 1 ] ; do
		echo "[$MYSQL_BT] "
		if read INPUT ; then : ; else echo "" ; exit 1 ; fi
		if [ ! "$INPUT" ] ; then
			INPUT=$MYSQL_BT
		fi
		case $INPUT in
			n*|N*)
				MYSQL_BT="no"
				ok=1
				;;
			y*|Y*)
				MYSQL_BT="yes"
				ok=1
				;;
			*)
				echo "Please enter 'yes' or 'no'."
				;;
		esac
	done
	echo ""
fi

####

ok=0
DEF=no
echo "Would you like to register your network with Denora?"
echo "Doing this will allow us to better focus our project"
while [ $ok -lt 1 ] ; do
	echo "[$REGSCRIPT] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$REGSCRIPT
	fi
	case $INPUT in
		n*|N*)
			REGSCRIPT="no"
			ok=1
			;;
		y*|Y*)
			REGSCRIPT="yes"
			ok=1
			;;
		*)
			echo "Please enter 'yes' or 'no'."
			;;
	esac
done
echo ""

####

ok=0
DEF=Yes
echo "Allow Denora to attempt to build a crypt library so that admin passwords can be encrypted in the config and db files"
while [ $ok -lt 1 ] ; do
	echo "[$ENCRYPT] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$ENCRYPT
	fi
	case $INPUT in
		n*|N*)
			ENCRYPT="no"
			ok=1
			;;
		y*|Y*)
			ENCRYPT="yes"
			ok=1
			;;
		*)
			echo "Please enter 'yes' or 'no'."
			;;
	esac
done
echo ""

####

ok=0
DEF=no
echo "Allow Denora to attempt to build a threading library so that the code can be multithreaded for speed"
while [ $ok -lt 1 ] ; do
	echo "[$THREAD] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$THREAD
	fi
	case $INPUT in
		n*|N*)
			THREAD="no"
			ok=1
			;;
		y*|Y*)
			THREAD="yes"
			ok=1
			;;
		*)
			echo "Please enter 'yes' or 'no'."
			;;
	esac
done
echo ""

################################################################################
# Store values
################################################################################
echo "Saving configuration results in config.cache... "

cat <<EOT >config.cache
BINDEST="$BINDEST"
DATDEST="$DATDEST"
RUNGROUP="$RUNGROUP"
UMASK=$UMASK
MYSQL="$MYSQL"
MYSQL_BT="$MYSQL_BT"
REGSCRIPT="$REGSCRIPT"
DMEMORY="$DMEMORY"
ENCRYPT="$ENCRYPT"
THREAD="$THREAD"
EOT
echo "done."

###########################################################################
# Let's call the configure script
###########################################################################
Run_Config

# EOF
