#!/bin/sh -
#
# @(#)$Id: mkconfig,v 1.1 2018/06/14 00:00:00 jneitzel $
#
# Create appropriate "config.h" and "mkconfig.tmp" files.
# ./configure runs this script on behalf of the user.
#
# Exit w/ a status of 0 on success.
# Exit w/ a status of 1 on error.
# --
# Jeffrey Allen Neitzel
#

CONFIG_H="config.h"
rm -f $CONFIG_H
trap 'status=$? ; rm -f $CONFIG_H ; exit $status' HUP INT QUIT TERM

#
# This function searches for the pathname of utility and defines
# constant w/ the resulting value.  If utility cannot be found,
# constant is defined as the empty string.
#
# usage: definePathnameConstant constant utility
#
definePathnameConstant()
{
	const="$1" ; util="$2"
	dirlist="/bin /sbin /usr/bin /usr/sbin /usr/libexec /usr/games"
	moderr="	  Modify value in \"$CONFIG_H\" if this is incorrect."
	modout="	/* Modify value if incorrect. */"
	for dir in $dirlist ; do
		if test -f "$dir/$util" -a -x "$dir/$util" ; then
			pname="$dir/$util" ; break
		else
			pname=""
		fi
	done

	#echo "$PATH" >&2
	wpname="`which $util </dev/null 2>/dev/null | grep -v 'not found'`"
	#echo "$wpname" >&2
	if test X"$pname" != X -a \( \
		X"$wpname" = X -o X"$wpname" = X"$pname" \
	\) ; then
		(echo "$PROGNAME: $const == \"$pname\"";echo "$moderr") >&2
		def="#define	$const	\"$pname\"$modout"
	elif test X"$wpname" != X ; then
		(echo "$PROGNAME: $const == \"$wpname\"";echo "$moderr") >&2
		def="#define	$const	\"$wpname\"$modout"
	else
		# This should rarely be true, but it is possible.
		(echo "$PROGNAME: $const == \"\"";echo "$moderr") >&2
		def="#define	$const	\"\"$modout"
	fi

	echo "$def"
}

#
# Define the SYS & DOT file macros for etsh.
#
defineSysAndDotFiles()
{
	in_ebn="$1"

	#
	# etsh no longer conflicts w/ omake & its osh, but we do not
	# want to install as "osh" anymore anyway; no worries.
	#
	if test -n "$in_ebn" ; then
		# install it as whatever was specified or etsh.
		echo '#define	PATH_SYSTEM_LOGIN	SYSCONFDIR/**/"'"/${in_ebn}.login"'"'
		echo '#define	PATH_SYSTEM_ETSHRC	SYSCONFDIR/**/"'"/${in_ebn}.${in_ebn}rc"'"'
		echo '#define	FILE_DOT_LOGIN		"'".${in_ebn}.login"'"'
		echo '#define	FILE_DOT_ETSHRC		"'".${in_ebn}rc"'"'
		echo '#define	PATH_SYSTEM_LOGOUT	SYSCONFDIR/**/"'"/${in_ebn}.logout"'"'
		echo '#define	FILE_DOT_LOGOUT		"'".${in_ebn}.logout"'"'
	else
		# default to etsh as a fallback.
		echo '#define	PATH_SYSTEM_LOGIN	SYSCONFDIR/**/"/etsh.login"'
		echo '#define	PATH_SYSTEM_ETSHRC	SYSCONFDIR/**/"/etsh.etshrc"'
		echo '#define	FILE_DOT_LOGIN		".etsh.login"'
		echo '#define	FILE_DOT_ETSHRC		".etshrc"'
		echo '#define	PATH_SYSTEM_LOGOUT	SYSCONFDIR/**/"/etsh.logout"'
		echo '#define	FILE_DOT_LOGOUT		".etsh.logout"'
	fi

	# Exceptions: The names of the history & prompt files cannot change.
	echo '#define	FILE_DOT_HISTORY	".etsh.history"'
	echo '#define	FILE_DOT_PROMPT		".etsh.prompt"'
	#
}

#
# Define the EBN, TBN, and related macros.
#
defineBinNameMacros()
{
	in_ebn="$1"
	in_tbn="$2"

	if test -n "$in_ebn" ; then
		defineSysAndDotFiles "$in_ebn"
	else
		defineSysAndDotFiles
	fi

	# 1) ebn / tbn - all characters to lower-case equivalent
	ebn="`echo $in_ebn | awk '{ printf "%s\n", tolower($1); }'`"
	tbn="`echo $in_tbn | awk '{ printf "%s\n", tolower($1); }'`"
	# 2) ebn1 / tbn1 - capitalize - 1st character to upper-case equivalent
	ebn1="`echo $ebn | awk '{ ac = split($1, c, ""); for (i = 1; i <= ac; i++) { if (c[i] ~ /[0-9a-z]/ && i == 1) printf "%c", toupper(c[i]); else printf "%c", c[i]; }; printf "\n"; }'`"
	tbn1="`echo $tbn | awk '{ ac = split($1, c, ""); for (i = 1; i <= ac; i++) { if (c[i] ~ /[0-9a-z]/ && i == 1) printf "%c", toupper(c[i]); else printf "%c", c[i]; }; printf "\n"; }'`"
	# 3) ebnc / tbnc - all characters to upper-case equivalent
	ebnc="`echo $ebn | awk '{ printf "%s\n", toupper($1); }'`"
	tbnc="`echo $tbn | awk '{ printf "%s\n", toupper($1); }'`"

	if test $# -eq 2 -a X"$1" != X -a X"$2" != X ; then
		#echo debug: set macros as specified >&2
		echo "EBN=$ebn EBN1=$ebn1 EBNC=$ebnc TBN=$tbn TBN1=$tbn1 TBNC=$tbnc" | tr ' ' '\n' >mkconfig.tmp
	else
		#echo debug: else >&2
		#echo debug: set default macros >&2
		echo 'EBN=etsh EBN1=Etsh EBNC=ETSH TBN=tsh TBN1=Tsh TBNC=TSH' | tr ' ' '\n' >mkconfig.tmp
	fi
}

UNAME_S="`uname -s`"
UNAME_SRM="`uname -srm`"
PROGNAME="`basename $0`"
if test $# -ne 0 -a $# -ne 2 ; then echo 'usage: $(SHELL) ./'"$PROGNAME"' [$(EBN) $(TBN)]' >&2 ; exit 1 ; fi
if test X"$UNAME_S" = X -o X"$UNAME_SRM" = X ; then
	echo "$PROGNAME: Fatal uname(1) error" >&2 ; exit 1
fi

cat <<EOI >$CONFIG_H
/*
 * etsh - an   enhanced port of the Sixth Edition (V6) UNIX Thompson shell
 *  tsh - an unenhanced port of the Sixth Edition (V6) UNIX Thompson shell
 */
/*
 * _XOPEN_SOURCE and/or _BSD_SOURCE and/or _DEFAULT_SOURCE should be
 * defined only if needed to avoid compilation errors or warnings for
 * the etsh package on a given system.  The systems where these feature
 * test macros are known to be needed are defined in the mkconfig script.
 *
 * This includes only Linux and SunOS (Solaris/OpenIndiana)
 * at the present time.
 *
 *	Configured for: $UNAME_SRM
 */

#ifndef	CONFIG_H
#define	CONFIG_H

`definePathnameConstant PATH_LOGIN  login`
`definePathnameConstant PATH_NEWGRP newgrp`

`defineBinNameMacros "$1" "$2"`

#define	ETSH_UNAME_SRM	"$UNAME_SRM"

EOI

case "$UNAME_S" in
#OpenBSD)
#	echo '#define	_XOPEN_SOURCE	700'   >>$CONFIG_H
#	echo '#define	_BSD_SOURCE'           >>$CONFIG_H
#	;;
*BSD|Darwin|DragonFly)
	echo "/* $UNAME_S: No need for _XOPEN_SOURCE or _BSD_SOURCE or _DEFAULT_SOURCE */" \
		>>$CONFIG_H
	;;
Linux)
	# Check if building on Ubuntu 16.xx or 17.xx.
	egrep '^(DISTRIB_ID=Ubuntu|DISTRIB_RELEASE=(16\.(04|10)|17\.04))$' /etc/lsb-release 2>/dev/null | wc -l | tr -d ' ' | grep '^2$' >/dev/null
	if test $? -eq 0 ; then
		# Ubuntu 16.xx or Ubuntu 17.xx
		( \
			echo '#define	CONFIG_BROKEN' ; \
			echo \
		) >>$CONFIG_H
	fi
	echo '#define	_XOPEN_SOURCE	700'   >>$CONFIG_H
	echo '#define	_BSD_SOURCE'           >>$CONFIG_H
	echo '#define	_DEFAULT_SOURCE'       >>$CONFIG_H
	echo '#define	CONFIG_LINUX'          >>$CONFIG_H
	;;
SunOS)
	( echo '#define	CONFIG_SUNOS' ; echo ) >>$CONFIG_H
	echo '#define	_XOPEN_SOURCE	600L'  >>$CONFIG_H
	;;
*)
	#
	# This may or may not cause a compilation error.
	# Simply try it to see if it works or not.
	#
	echo "$PROGNAME: WARNING: Check \"$CONFIG_H\" if compilation fails." >&2
	cat <<EOI >>$CONFIG_H
/*
 * WARNING: $UNAME_SRM: Unknown system
 *
 * Please report this result to the developer if possible.
 */
EOI
	;;
esac

echo                             >>$CONFIG_H
echo '#endif	/* !CONFIG_H */' >>$CONFIG_H
