#!/bin/sh -
#
# @(#)$Id: e705c994f17128fac3e49f145614bc678aa0c108 $
#
# 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 osh, and return true (0) or false (1).
#
defineSysAndDotFiles()
{
	in_obn="$1"

	if test X"$in_obn" != X ; then
		etsh=true
	else
		etsh=false
	fi

	if $etsh || test \
	   -x '/usr/pkg/bin/omake' -o \
	   -x '/usr/local/bin/omake' -o \
	   -x '/usr/bin/omake' ; then
		# install osh as etsh or whatever was specified in this case.
		etsh=true
		echo '#define	PATH_SYSTEM_LOGIN	SYSCONFDIR/**/"'"/${in_obn}.login"'"'
		echo '#define	PATH_SYSTEM_OSHRC	SYSCONFDIR/**/"'"/${in_obn}.${in_obn}rc"'"'
		echo '#define	FILE_DOT_LOGIN		"'".${in_obn}.login"'"'
		echo '#define	FILE_DOT_OSHRC		"'".${in_obn}rc"'"'
		echo '#define	PATH_SYSTEM_LOGOUT	SYSCONFDIR/**/"'"/${in_obn}.logout"'"'
		echo '#define	FILE_DOT_LOGOUT		"'".${in_obn}.logout"'"'
	else
		# omake was not found; assume there is no conflict.
		etsh=false
		echo '#define	PATH_SYSTEM_LOGIN	SYSCONFDIR/**/"/osh.login"'
		echo '#define	PATH_SYSTEM_OSHRC	SYSCONFDIR/**/"/osh.oshrc"'
		echo '#define	FILE_DOT_LOGIN		".osh.login"'
		echo '#define	FILE_DOT_OSHRC		".oshrc"'
		echo '#define	PATH_SYSTEM_LOGOUT	SYSCONFDIR/**/"/osh.logout"'
		echo '#define	FILE_DOT_LOGOUT		".osh.logout"'
	fi

	# Exception: The name of the history file does not change.
	echo '#define	FILE_DOT_HISTORY	".etsh.history"'
	#

	if $etsh ; then status=0 ; else status=1 ; fi
	return $status
}

#
# Define the OBN, SBN, and related macros.
#
defineBinNameMacros()
{
	in_obn="$1"
	in_sbn="$2"

	if test X"$in_obn" != X ; then
		defineSysAndDotFiles "$in_obn"
	else
		defineSysAndDotFiles
	fi
	test $? -eq 0 && etsh=true || etsh=false

	# 1) obn / sbn - all characters to lower-case equivalent
	obn="`echo $in_obn | awk '{ printf "%s\n", tolower($1); }'`"
	sbn="`echo $in_sbn | awk '{ printf "%s\n", tolower($1); }'`"
	# 2) obn1 / sbn1 - capitalize - 1st character to upper-case equivalent
	obn1="`echo $obn | awk '{ ac = split($1, c, ""); for (i = 1; i <= ac; i++) { if (c[i] ~ /[0-9a-z]/ && i == 1) printf toupper(c[i]); else printf c[i]; }; printf("\n"); }'`"
	sbn1="`echo $sbn | awk '{ ac = split($1, c, ""); for (i = 1; i <= ac; i++) { if (c[i] ~ /[0-9a-z]/ && i == 1) printf toupper(c[i]); else printf c[i]; }; printf("\n"); }'`"
	# 3) obnc / sbnc - all characters to upper-case equivalent
	obnc="`echo $obn | awk '{ printf "%s\n", toupper($1); }'`"
	sbnc="`echo $sbn | awk '{ printf "%s\n", toupper($1); }'`"

	if test $# -eq 2 -a X"$1" != X -a X"$2" != X ; then
		echo "OBN=$obn OBN1=$obn1 OBNC=$obnc SBN=$sbn SBN1=$sbn1 SBNC=$sbnc" | tr ' ' '\n' >mkconfig.tmp
	else
		if $etsh ; then
			echo 'OBN=etsh OBN1=Etsh OBNC=ETSH SBN=tsh SBN1=Tsh SBNC=TSH' | tr ' ' '\n' >mkconfig.tmp
		else
			echo 'OBN=osh OBN1=Osh OBNC=OSH SBN=sh6 SBN1=Sh6 SBNC=SH6' | tr ' ' '\n' >mkconfig.tmp
		fi
	fi
}

UNAME_S="`uname -s`"
UNAME_SRM="`uname -srm`"
PROGNAME="`basename $0`"
if test $# -ne 0 -a $# -ne 1 -a $# -ne 2 ; then echo 'usage: $(SHELL) ./'"$PROGNAME" >&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
/*
 * osh - an   enhanced port of the Sixth Edition (V6) UNIX Thompson shell
 * sh6 - 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 osh 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	OSH_UNAME_SRM	"$UNAME_SRM"

EOI

case "$UNAME_S" in
*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
		# Ubuntu 17.xx
		( \
			echo '#define	CONFIG_BROKEN' ; \
			echo \
		) >>$CONFIG_H
	fi
	echo '#define	_XOPEN_SOURCE	600L'  >>$CONFIG_H
	echo '#define	_BSD_SOURCE'           >>$CONFIG_H
	echo '#define	_DEFAULT_SOURCE'       >>$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
