#!/bin/sh
# WendzelNNTPd configure script by Steffen 'cdp_xe' Wendzel.
# Based on the Xyria:DNSd/cdpNNTPd configure script I wrote in 2004 or 2005.
# This script is basically unchanged since 2015 as I only added detail
# improvements.
# e-mail: wendzel (at) hs-worms (dot) de
# web:    http://www.wendzel.de
#
# WendzelNNTPd is distributed under the following license:
#
# Copyright (c) 2004-2021 Steffen Wendzel <wendzel (at) hs-worms (dot) de>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

VERSION="2.4 - Dec-21-2017"

MAKEFILE_INC="Makefile.inc"

USE_SOLARIS_NETLIBS="NO"
USE_GCCLOCAL_PTHREAD="NO"
NEED_GCCLOCAL_PTHREAD="NO"
USE_LIBPTHREAD="YES"
ORDER=""

OS=`uname -s`
RELEASE=`uname -r`

TABCPATH='src/'

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

out()
{
	if [ "$OS" = "SunOS" ]; then
		echo $1 '\c'
	else
		echo -n $1
	fi
}

check()
{
	RET_VAL=$?

	if [ "$4" != "" ]; then
		CHK_VAL=$4
	else
		CHK_VAL="0"
	fi
	if [ "$RET_VAL" = "$CHK_VAL" ]; then
		echo $1
	else
		echo "$2 (returned $RET_VAL, what is not eq to $CHK_VAL)"
		echo $3
		exit
	fi
}

############# WELCOME #########################

echo "This is the WendzelNNTPd configuration script v.${VERSION}"
echo
echo "Written by Steffen Wendzel"
echo "    mail: wendzel (at) hs-worms (dot) de"
echo "    https://www.wendzel.de"
echo

###### check some env #########
ADD_CFLAGS=$CFLAGS
ADD_LNKFLAGS=$LDFLAGS

# make /usr/local the default DESTDIR
if [ "$DESTDIR" = "" ]; then
	DESTDIR=/usr/local
fi

# make /usr/local/etc the default CONFDIR
if [ "$CONFDIR" = "" ]; then
	CONFDIR=/usr/local/etc
fi

if [ "$FAKECDIR" = "" ]; then
	FAKECDIR=$CONFDIR
fi

if [ "$1" != "" ]; then
	echo "This configure script takes some environment variables:"
	echo
	echo "1) Directory setup (can be used e.g. if you build a package/port):"
	echo 
	echo "DESTDIR  - The installation destination base dir (would be /usr"
	echo "           or /usr/local in 99% of all cases) and there is"
	echo "           usually little reason to change this variable."
	echo "           The default value is: /usr/local"
	echo "CONFDIR  - The installation destination of the config file"
	echo "           (would be /etc or /usr/local/etc in 99% of all cases)."
	echo "           The default value is: /usr/local/etc"
	echo "FAKECDIR - This is a fake directory to place the config file into"
	echo "           (for example copy to ./fakeroot-xyz/etc/ but the code"
	echo "           will use CONFDIR nevertheless). This is useful for"
	echo "           building ports and packages in Linux and *BSD. However,"
	echo "           use-cases will be pretty rare."
	echo "           The default value: same as CONFDIR"
	echo
	echo "2) Databases (options to disable support at compile-time):"
	echo
	echo "MYSQL=NO - Disables support for MySQL if you do not wish to"
	echo "           install mysqlclient and mysql development libraries"
	echo "           or if you do not wish to have WendzelNNTPd contain"
	echo "           any support for MySQL (use: 'MYSQL=NO ./configure')."
	echo "SQLITE   - Disables support for SQLITE if you do not wish to"
	echo "           install sqlite development libraries or if you do"
	echo "           not wish to have WendzelNNTPd contain any support"
	echo "           for SQLite (use: 'SQLITE=NO ./configure')."
	echo "Note: You can only deactivate *either* MYSQL *or* SQLITE but not"
	echo "both as least one database is required to operate WendzelNNTPd."
	exit 1
fi

############# OS DEPENDENT PART ###############

echo "operating system is...$OS"
echo "operating system release is...$RELEASE"

out 'checking for solaris...'
if [ "$OS" = "SunOS" ]; then
	echo "yes"
	USE_SOLARIS_NETLIBS="YES"
	TABCPATH=""
	out "byte_order..."
	if [ "`uname -m`" = "i86pc" ]; then
		ORDER=1
		echo "little endian"
	else
		ORDER=0
		echo "big endian"
		echo
		echo "NOTE: WendzelNNTPd was never tested on big endian platforms."
		echo
	fi
else
	echo "no"
fi

out "config for gcc-local -pthread..."
if [ "$OS" = "OpenBSD" ]; then
	USE_GCCLOCAL_PTHREAD="YES"
	NEED_GCCLOCAL_PTHREAD="YES"
	USE_LIBPTHREAD="NO"
fi
echo "done"

out "checking for linux sort parameters (-bg)..."
if [ "$OS" = "Linux" ]; then
	ADDSORT="-bg"
	echo "yes"
else
	echo "no"
fi

out "checking for 'install' command..."
install >/dev/null 2>&1; check "yes" "no" "you need the 'install' tool." 1 # == error because no useful parameter provided, but that is what we need, because if not present, return value would be 127==not found
#if [ "$?" = "127" ]; then
#	echo "no"
#else
#	echo "yes"
#fi

out "configuring for 'install' command parameters..."
if [ "$OS" = "SunOS" ]; then
	# OpenSolaris
	CMD_INSTALL_USEROPT="-u"
else
	# OpenBSD, FreeBSD, Linux
	CMD_INSTALL_USEROPT="-o"
fi
echo "done, using '$CMD_INSTALL_USEROPT root' for installation."

############# OS INDEPENDENT PART #############

out "checking for gcc..."; gcc -v >/dev/null 2>&1; check "yes" "no" "You need gcc (or set PATH)."

###############
out "checking for libc..."
cat << EOF >temp.c
#include <stdio.h>
int main() {
	printf("WendzelNNTPd\n");
	return 0;
}
EOF
gcc -o temp temp.c>/dev/null 2>&1; check "yes" "no" "You need libc."
rm -f temp temp.c


###############
out "checking for -fstack-protector..."
cat << EOF >temp.c
#include <stdio.h>
int main() {
        printf("WendzelNNTPd\n");
        return 0;
}
EOF
gcc -o temp -fstack-protector temp.c>/dev/null 2>&1
if [ "$?" = "1" ]; then
	echo "no (compiling without stack protection)"
	STACK_PROT=""
else
	echo "yes"
	STACK_PROT="-fstack-protector"
fi
rm -f temp temp.c
	
###############
out "checking for strftime()/ltime()..."
cat << EOF >temp.c
#include <time.h>
int main() {
	char unused[0xff] = {'\0'};
	time_t ltime;

	ltime = time(NULL);
	if (ltime == (time_t) - 1) {
		return;
	}
	strftime(unused, 0xff-1, "%a, %d %b %y %H:%M:%S", localtime(&ltime));
	return 0;
}
EOF
gcc -o temp temp.c >/dev/null 2>&1; check "yes" "no" "You need strftime() and ltime()."
rm -f temp temp.c

###############
out "checking for %z support in strftime()..."
cat << EOF >temp.c
#include <time.h>
int main() {
	char unused[0xff] = {'\0'};
	time_t ltime;

	ltime = time(NULL);
	if (ltime == (time_t) - 1) {
		return;
	}
	strftime(unused, 0xff-1, "%z", localtime(&ltime));
	return 0;
}
EOF
gcc -o temp temp.c >/dev/null 2>&1
if [ "$?" = "0" ]; then
	echo "yes"
else
	ADD_CFLAGS=`echo $ADD_CFLAGS " -DNOSUPPORT_STRFTIME_z_FLAG "`
	echo "no"
fi
rm -f temp temp.c

###############
#out "checking for g++..."; g++ -v >/dev/null 2>&1; check "yes" "no" "You need g++ (or set PATH)."
out "checking for flex..."; flex -V >/dev/null 2>&1; check "yes" "no" "You need flex."
out "checking for bison..."; bison -V >/dev/null 2>&1; check "yes" "no" "You need Bison."

################
# Database stuff
################

if [ "$SQLITE" = "NO" -a "$MYSQL" = "NO" ]; then
	echo "error: you cannot deactivate support for MySQL *and* SQlite as"
	echo "you need to integrate support for at least one database!";
	exit 1
fi

if [ "$SQLITE" = "NO" ]; then
	out "configuring without SQlite3 support ..."; echo
else
	out "checking for sqlite3..."; sqlite3 -version 2>/dev/null
	if [ "$?" = "127" ]; then
		echo "no (You need SQLite3 or newer (or disable SQLite3 support, cf. ./configure --help).)"
		exit 1
	fi

	out "checking for libsqlite3..."
	cat << EOF > temp.c
#include <sqlite3.h>
int main() {
  sqlite3 *db;
  int rc;
  rc = sqlite3_open("nothing", &db);
  sqlite3_close(db);
  return 0;
}
EOF
	gcc -o temp temp.c -lsqlite3 -I/usr/local/include -L/usr/local/lib >/dev/null 2>&1; check "yes" "no" "You need libSQLite3 development files and library (or disable SQLite3 support, cf. ./configure --help)."
	rm -f temp temp.c
fi


# libMySQL check
if [ "$MYSQL" = "NO" ]; then
	out "configuring without MySQL support ..."; echo
else
	out "checking for MySQL client library..."
	cat << EOF > temp.c
#include <mysql/mysql.h>
#include <stdio.h>

MYSQL *myhndl = NULL;

int main() {
  if ((myhndl = mysql_init(NULL)) == NULL) {
    fprintf(stderr, "mysql_init() error\n");
    return 1;
  }
  return 0;
}
EOF
	gcc -o temp temp.c -lmysqlclient -I/usr/local/include -L/usr/local/lib >/dev/null 2>&1; check "yes" "no" "You need the MySQL client library and their development files (or disable MySQL support, cf. ./configure --help)."
	rm -f temp temp.c
fi

###############
out "checking for libpthread..."
cat << EOF >temp.c
#include <pthread.h>
int main(){pthread_self();return 0;}
EOF
gcc -o temp temp.c -lpthread >/dev/null 2>&1; check "yes" "no" "You need libpthread."
rm -f temp temp.c

###############
out "checking for libmhash..."
cat << EOF >temp.c
#include <mhash.h>
int main(){MHASH td = mhash_init(MHASH_MD5);return 0;}
EOF
gcc -o temp temp.c -lmhash >/dev/null 2>&1; check "yes" "no" "You need libmhash."
rm -f temp temp.c

###############
out "checking for SHA-256..."
cat << EOF >temp.c
#include <mhash.h>
int main(){MHASH td = mhash_init(MHASH_SHA256);return 0;}
EOF
gcc -o temp temp.c -lmhash >/dev/null 2>&1; check "yes" "no" "You need a version of libmhash that supports SHA-256."
rm -f temp temp.c

###############
out "checking for strndup()..."
cat << EOF >temp.c
#include <stdio.h>
#include <string.h>
#ifdef __svr4__
   #include <strings.h>	
#endif
int main(){char *q = "test"; strndup(q, 1);return 0;}
EOF
gcc -o temp temp.c >/dev/null 2>&1
if [ "$?" = "0" ]; then
	echo "yes"
else
	echo "no [adding to ADD_CFLAGS]"
	ADD_CFLAGS=`echo $ADD_CFLAGS " -DNOSUPPORT_STRNDUP "`
fi
rm -f temp temp.c


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

#out "checking for openssl..."
#	openssl -v >/dev/null 2>&1; check "yes" "no" "You need openssl (or set PATH)."
#
#out "checking for libssl..."
#cat << EOF >temp.c
##include <openssl/ssl.h>
#int main(){SSL_library_init();return 0;}
#EOF
#gcc -o temp temp.c -lssl 2>&1; check "yes" "no" "You need libssl."
#rm -f temp temp.c

############# rc.d script ADJUSTMENT ##########

echo -n "patching scripts/startup/init.d_script_raw to scripts/startup/init.d_script..."
echo -n $DESTDIR | sed 's/\//\\\//g' >ignore
PATCH_DESTDIR=`cat ignore`
rm ignore
cat scripts/startup/init.d_script_raw | sed 's/REPLACETHIS\_WITH\_DAEMONPATH/'$PATCH_DESTDIR'\/sbin\/wendzelnntpd/' > scripts/startup/init.d_script
echo "done."

############# MAKEFILE.INC CREATE #############

out "creating Makefile.inc..."

cat /dev/null > $MAKEFILE_INC

echo "SORT=sort $ADDSORT" >> $MAKEFILE_INC

echo "CMD_INSTALL_USEROPT=$CMD_INSTALL_USEROPT" >> $MAKEFILE_INC

if [ "$USE_SOLARIS_NETLIBS" = "YES" ]; then
	echo 'SOLNETLIBS=-lnsl -lsocket'>> $MAKEFILE_INC
else
	echo 'SOLNETLIBS='>> $MAKEFILE_INC
fi

if [ "$NEED_GCCLOCAL_PTHREAD" = "YES" ]; then
	echo 'GCCLOCALPTHREAD=-pthread'>> $MAKEFILE_INC
else
	echo 'GCCLOCALPTHREAD='>> $MAKEFILE_INC
fi

if [ "$USE_LIBPTHREAD" = "YES" ]; then
	echo 'LIBPTHREAD=-lpthread'>> $MAKEFILE_INC
else
	echo 'LIBPTHREAD='>> $MAKEFILE_INC
fi

if [ "$MYSQL" = "NO" ]; then
	echo 'MYSQLLIB='>> $MAKEFILE_INC
	echo 'MYSQLOBJ='>> $MAKEFILE_INC
	ADD_CFLAGS=`echo $ADD_CFLAGS " -DNOMYSQL "`
else
	echo 'MYSQLLIB=-lmysqlclient'>> $MAKEFILE_INC
	echo 'MYSQLOBJ=db_mysql.o'>> $MAKEFILE_INC
fi

if [ "$SQLITE" = "NO" ]; then
	echo 'SQLITELIB='>> $MAKEFILE_INC
	echo 'SQLITEOBJ='>> $MAKEFILE_INC
	ADD_CFLAGS=`echo $ADD_CFLAGS " -DNOSQLITE "`
	# mysql does not run a command for setup but sqlite
	# requires it in 'make install'. Thus, let make know
	# when no sqlite is there ...
	echo 'SQLITEINST=NO'>> $MAKEFILE_INC
else
	echo 'SQLITELIB=-lsqlite3'>> $MAKEFILE_INC
	echo 'SQLITEOBJ=db_sqlite3.o'>> $MAKEFILE_INC
fi

echo "LIBMHASH=-lmhash">> $MAKEFILE_INC

echo "TABCPATH=$TABCPATH">> $MAKEFILE_INC
echo "ORDER=-DORDER=$ORDER">> $MAKEFILE_INC
echo >> $MAKEFILE_INC
echo "STACK_PROT=$STACK_PROT">> $MAKEFILE_INC
echo "ADD_CFLAGS=$ADD_CFLAGS">> $MAKEFILE_INC
echo "ADD_LNKFLAGS=$ADD_LNKFLAGS">> $MAKEFILE_INC
echo >> $MAKEFILE_INC
echo "DESTDIR=$DESTDIR">> $MAKEFILE_INC
echo "CONFDIR=$CONFDIR">> $MAKEFILE_INC
echo "FAKECDIR=$FAKECDIR">> $MAKEFILE_INC

echo '
SRC=src
LIBDIRS=-L/usr/local/lib

INCDIRS=-I. -I./src -I./src/include -I/usr/local/include
CPP=g++
CC=gcc
LEX=flex
YACC=bison

' >> $MAKEFILE_INC

echo "done."
echo "(you can now run make (or gmake on OpenSolaris) ...)"

