#!/bin/sh
#
# Anope IRC Services <https://www.anope.org/>
#
# Copyright (C) 2003-2026 Anope Contributors
#
# Anope is free software. You can use, modify, and/or distribute it under the
# terms of version 2 of the GNU General Public License. See docs/LICENSE.txt
# for the complete terms of this license and docs/AUTHORS.txt for a list of
# contributors.
#
# Based on the original code of Epona by Lara
# Based on the original code of Services by Andy Church
#
# SPDX-License-Identifier: GPL-2.0-only

SOURCE_DIR="$(cd "$(dirname "$0")" && pwd)"
. $SOURCE_DIR/src/version.sh

Load_Cache () {
	if [ -f $SOURCE_DIR/config.cache -a -r $SOURCE_DIR/config.cache -a ! "$IGNORE_CACHE" ] ; then
		echo "Using defaults from config.cache. To ignore, $SOURCE_DIR/Config -nocache"
		echo ""
		. $SOURCE_DIR/config.cache
		CAN_QUICK="yes"
	else
		CAN_QUICK="no"
	fi
}

Run_Build_System () {
	BUILD_DIR="${SOURCE_DIR}/build"
	CMAKE_COMMAND="${CMAKE:-cmake} -B ${BUILD_DIR} -S ${SOURCE_DIR}"

	if [ "$INSTDIR" != "" ] ; then
		CMAKE_COMMAND="${CMAKE_COMMAND} -D INSTDIR=$INSTDIR"
	fi

	if [ "$RUNGROUP" != "" ] ; then
		CMAKE_COMMAND="${CMAKE_COMMAND} -D RUNGROUP=$RUNGROUP"
	fi

	if [ "$UMASK" != "" ] ; then
		CMAKE_COMMAND="${CMAKE_COMMAND} -D DEFUMASK=$UMASK"
	fi

	if [ "$DEBUG" = "yes" ] ; then
		CMAKE_COMMAND="${CMAKE_COMMAND} -D CMAKE_BUILD_TYPE=Debug"
	else
		CMAKE_COMMAND="${CMAKE_COMMAND} -D CMAKE_BUILD_TYPE=Release"
	fi

	if [ "$EXTRA_INCLUDE_DIRS" != "" ] ; then
		CMAKE_COMMAND="${CMAKE_COMMAND} -D EXTRA_INCLUDE=$EXTRA_INCLUDE_DIRS"
	fi

	if [ "$EXTRA_LIB_DIRS" != "" ] ; then
		CMAKE_COMMAND="${CMAKE_COMMAND} -D EXTRA_LIBS=$EXTRA_LIB_DIRS"
	fi

	echo $CMAKE_COMMAND
	$CMAKE_COMMAND

	if [ $? -ne 0 ]; then
		echo "You should fix these issues and then run ./Config -quick to rerun CMake."
		exit 1
	fi

	echo ""
	echo "Now run 'make -C ${BUILD_DIR#"$PWD/"} install' to build and install Anope."
}

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

INSTDIR="${HOME}/anope-${VERSION_MAJOR}.${VERSION_MINOR}"
RUNGROUP=
UMASK=
DEBUG="no"
EXTRA_INCLUDE_DIRS=
EXTRA_LIB_DIRS=
EXTRA_CONFIG_ARGS=
CAN_QUICK="no"

###########################################################################
# Check out the options
###########################################################################

while [ $# -ge 1 ] ; do
	OPTION=$1
	while [ "${OPTION#-}" != "$OPTION" ]; do
		OPTION="${OPTION#-}"
	done

	if [ "$OPTION" = "--help" ] ; then
		echo "Config utility for Anope"
		echo "------------------------"
		echo "Syntax: ./Config [options]"
		echo "-nocache     Ignore settings saved in config.cache"
		echo "-nointro     Skip intro (disclaimer, etc)"
		echo "-quick       Skip questions, go straight to cmake"
		exit 0
	elif [ "$OPTION" = "devel" ] ; then
		DEBUG="yes"
		DEVEL="yes"
		INSTDIR="$SOURCE_DIR/run"
	elif [ "$OPTION" = "nocache" ] ; then
		IGNORE_CACHE="1"
	elif [ "$OPTION" = "nointro" ] ; then
		NO_INTRO="1"
	elif [ "$OPTION" = "quick" -o "$OPTION" = "q" ] ; then
		Load_Cache
		if [ "$CAN_QUICK" = "yes" ] ; then
			Run_Build_System
		else
			echo ""
			echo "Can't find cache file (config.cache), aborting..."
		fi
		exit 0
	fi
	shift 1
done

###########################################################################
# Check for CMake and (optionally) install it
###########################################################################

cmake --version 2>&1 > /dev/null
if [ $? -ne 0 ] ; then
	clear
	echo "Anope requires CMake 3.20 or newer, which can be downloaded at https://cmake.org/ or through your system's package manager."
	echo "If you have installed CMake already, ensure it is in your PATH environment variable."
	exit 0
fi

###########################################################################

if [ ! "$NO_INTRO" ] ; then
	export MORE='-e'
	cat $SOURCE_DIR/.BANNER | sed "s/CURVER/$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH$VERSION_EXTRA/" | sed "s@SOURCE_DIR@$SOURCE_DIR@" | more
	echo ""
else
	echo ""
fi

echo "Beginning Anope configuration."
echo ""

###########################################################################
# Load the cache
###########################################################################

if [ ! "$IGNORE_CACHE" ] ; then
	Load_Cache
fi

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

export ok INPUT

####

TEMP_YN="n"
if [ "$DEVEL" = "yes" ] ; then
	TEMP_YN="y"
fi
echo "You are building the 2.1 development branch. This branch is not as well tested"
echo "as the 2.0 stable branch and may have compatibility breaks without notice. Are"
echo "you sure you want to use this version?"
echo -n "[$TEMP_YN] "
read YN
if [ "$YN" ] ; then
	if [ "$YN" = "y" ] ; then
		DEVEL="yes"
	else
		DEVEL="no"
	fi
fi
echo ""

if [ "$DEVEL" != "yes" ] ; then
	echo "If you are building from Git you can run \`git checkout 2.0\` to get the latest"
	echo "stable code. Otherwise, you can download the latest 2.0 release tarball from"
	echo "https://github.com/anope/anope/releases/latest"
	exit 1
fi

####

ok=0
echo "In what directory should Anope be installed?"
while [ $ok -eq 0 ] ; do
	echo -n "[$INSTDIR] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$INSTDIR
	fi
	if [ ! -d "$INPUT" ] ; then
		if [ -e "$INPUT" ]; then
			echo "$INPUT exists, but is not a directory!"
		else
			echo "$INPUT does not exist.  Create it?"
			echo -n "[y] "
			read YN
			if [ "$YN" != "n" ] ; then
				if mkdir -p $INPUT ; then
					ok=1
				fi
			fi
		fi
	elif [ -e "$INPUT/include/services.h" ]; then
		echo "You cannot use the Anope source directory as a target directory."
	else
		ok=1
	fi
done
INSTDIR=$INPUT
echo ""

####

OLD_RUNGROUP="$RUNGROUP"
if [ "$RUNGROUP" ] ; then
	echo "Which group should all Anope data files be owned by?  (If Anope"
	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 Anope data files be owned by?  (If Anope"
	echo "should not force files to be owned by a particular group, just press"
	echo "Return.)"
fi
echo -n "[$RUNGROUP] "
if read INPUT ; then : ; else echo "" ; exit 1 ; fi
if [ "$INPUT" ] ; then
	if [ "$INPUT" = "none" ] ; then
		RUNGROUP=""
	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 -eq 0 ] ; do
	echo -n "[$UMASK] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$UMASK
	fi
	if [ `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 ""

####

TEMP_YN="n"
if [ "$DEBUG" = "yes" ] ; then
	TEMP_YN="y"
fi
echo "Would you like to build a debug version of Anope?"
echo -n "[$TEMP_YN] "
read YN
if [ "$YN" ] ; then
	if [ "$YN" = "y" ] ; then
		DEBUG="yes"
	else
		DEBUG="no"
	fi
fi
echo ""

####

echo "Are there any extra include directories you wish to use?"
echo "You may only need to do this if CMake is unable to locate"
echo "missing dependencies without hints."
echo "Separate directories with semicolons."
echo "If you need no extra include directories, enter NONE in all caps."
echo -n "[$EXTRA_INCLUDE_DIRS] "
if read INPUT ; then : ; else echo "" ; exit 1 ; fi
if [ "$INPUT" ] ; then
	if [ "$INPUT" = "NONE" ] ; then
		EXTRA_INCLUDE_DIRS=""
	else
		EXTRA_INCLUDE_DIRS=$INPUT
	fi
fi
echo ""

####

echo "Are there any extra library directories you wish to use?"
echo "You may only need to do this if CMake is unable to locate"
echo "missing dependencies without hints."
echo "Separate directories with semicolons."
echo "If you need no extra library directories, enter NONE in all caps."
echo -n "[$EXTRA_LIB_DIRS] "
if read INPUT ; then : ; else echo "" ; exit 1 ; fi
if [ "$INPUT" ] ; then
	if [ "$INPUT" = "NONE" ] ; then
		EXTRA_LIB_DIRS=""
	else
		EXTRA_LIB_DIRS=$INPUT
	fi
fi
echo ""

####

echo "Are there any extra arguments you wish to pass to CMake?"
echo "If you need no extra arguments to CMake, enter NONE in all caps."
echo -n "[$EXTRA_CONFIG_ARGS] "
if read INPUT ; then : ; else echo "" ; exit 1 ; fi
if [ "$INPUT" ] ; then
	if [ "$INPUT" = "NONE" ] ; then
		EXTRA_CONFIG_ARGS=""
	else
		EXTRA_CONFIG_ARGS=$INPUT
	fi
fi
echo ""

####

################################################################################
# Store values
################################################################################

echo -n "Saving configuration results in config.cache... "

cat <<EOT >$SOURCE_DIR/config.cache
INSTDIR="$INSTDIR"
RUNGROUP="$RUNGROUP"
UMASK=$UMASK
DEBUG="$DEBUG"
DEVEL="$DEVEL"
EXTRA_INCLUDE_DIRS="$EXTRA_INCLUDE_DIRS"
EXTRA_LIB_DIRS="$EXTRA_LIB_DIRS"
EXTRA_CONFIG_ARGS="$EXTRA_CONFIG_ARGS"
EOT
echo "done."


################################################################################
# Build the build system string
################################################################################

Run_Build_System
