#! /bin/sh

### See http://people.gnome.org/~walters/docs/build-api.txt
# buildapi-variable-no-builddir

# Show help if requested.
#
for var in "$@" ; do
	if test "${var}" = "--help" ; then
		cat <<-EOF
		usage: $0 [--options]
		Available options:

		  --help          This help message.
		  --prefix=PATH   Installation path prefix [default: /]
		  --datadir=PATH  Path for runtime data files [default: prefix/lib/]
		  --with-module-name=NAME
		                  Specify an alternate name for the module [default: altfiles]
		  --with-types=TYPE1,TYPE2,...
		                  Specify the list of NSS types that the module will
		                  handle. Use "all" to support all types.
		                  [default: pwd,grp]

		Available NSS types and their corresponding data files:

		  rpc        \$DATADIR/rpc
		  proto      \$DATADIR/protocols
		  hosts      \$DATADIR/hosts
		  network    \$DATADIR/networks
		  service    \$DATADIR/services
		  pwd        \$DATADIR/passwd
		  grp        \$DATADIR/group
		  spwd       \$DATADIR/shadow
		  sgrp       \$DATADIR/gshadow

		Also, the following relevant environment variables can be set:

		  CFLAGS    Additional command line flags to be passed to the C compiler
		  CXXFLAGS  Additional command line flags to be passed to the C++ compiler

		NOTE: This script tries to mimic the typical usage for configure scripts
		generated by autotools, hence it will silently ignore unrecognized
		command line options.
		EOF
		exit
	fi
done

# Inspect arguments and generate config.mk
#
echo "# autogenerated by: $0 $*" > config.mk
echo EXTRA_CFLAGS= >> config.mk
echo EXTRA_CXXFLAGS= >> config.mk

for var in "$@" ; do
	case ${var} in
		CFLAGS=* | CXXFLAGS=*)
			echo "Setting EXTRA_${var}"
			echo "EXTRA_${var}" >> config.mk
			;;
		--prefix=*)
			var=`echo "${var}" | sed 's/^--prefix=//'`
			echo "Setting PREFIX=${var}"
			echo "PREFIX := ${var}" >> config.mk
			;;
		--libdir=*)
			var=`echo "${var}" | sed 's/^--libdir=//'`
			echo "Setting LIBDIR=${var}"
			echo "LIBDIR := ${var}" >> config.mk
			;;
		--datadir=*)
			var=`echo "${var}" | sed 's/^--datadir=//'`
			echo "Setting DATADIR=${var}"
			echo "DATADIR := ${var}" >> config.mk
			;;
		--with-module-name=*)
			var=`echo "${var}" | sed 's/^--with-module-name=//'`
			echo "Setting MODULE_NAME=${var}"
			echo "MODULE_NAME := ${var}" >> config.mk
			;;
		--with-types=*)
      var=`echo "${var}" | sed 's/^--with-types=//'`
      if [ "${var}" = "all" ] ; then
				# Pick all supported types from the conditionals in the Makefile
				var=`sed 's/.\+$(HANDLE_\([a-z]\+\)).*/\1/p;d' Makefile | paste -s -d ','`
			fi
      echo "Setting TYPES=${var}"
			# By default pwd and grp are enabled, so clear their values before
			# adding the ones passed to --with-types=
			echo "HANDLE_pwd :=" >> config.mk
			echo "HANDLE_grp :=" >> config.mk
			echo ",${var}" | sed 's/,\([^,]\+\)/HANDLE_\1 := 1\n/g' >> config.mk
			;;
		*)
			true
			;;
	esac
done

# Inherit the CFLAGS/CXXFLAGS from the environment, if defined.
#
if test -n "${CFLAGS}"; then
    echo "Setting EXTRA_CFLAGS+=${CFLAGS} (from environment)"
    echo "EXTRA_CFLAGS+=${CFLAGS}" >> config.mk
fi
if test -n "${CXXFLAGS}"; then
    echo "Setting EXTRA_CXXFLAGS+=${CXXFLAGS} (from environment)"
    echo "EXTRA_CXXFLAGS+=${CXXFLAGS}" >> config.mk
fi

echo "config.mk written"
