#! /bin/sh
#
# opkbuild
# configure
# Configuration script to generate Makefile.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package_name='opkbuild'
package_version='3.0.0'
package_description='OPK Build System'

srcdir=$(dirname "${0}")
srcdir=$(cd ${srcdir} && pwd)
prefix='/usr/local'
bindir='$(prefix)/bin'
libdir='$(prefix)/lib'
datadir='$(prefix)/share'
mandir='$(datadir)/man'
localedir='$(datadir)/locale'
libopkbuild='$(libdir)/libopkbuild.1'

quiet=false
missing_deps=false
dep_cmds='
sh
opkg
'

long_opts_with_args='
srcdir
prefix
bindir
libdir
datadir
mandir
localedir
'

features="${dep_cmds}"

subst_vars="package_name
package_version
package_description
${long_opts_with_args}
libopkbuild
${dep_cmds}"

main()
{
	parse_options "${@}"

	for dep in ${dep_cmds}; do
		dep_val=$(eval echo \$\{"${dep}"\})
		if [ -z "${dep_val}" ]; then
			find_dep_cmd "${dep}"
		fi
	done

	if ${missing_deps}; then
		cat <<EOF

Some dependencies could not be found.
Please make sure all dependencies are installed and try again.

EOF
		exit 1
	fi

	write_makefiles

	printf '\nConfiguration complete!\n\n'
}

print_usage()
{
	printf 'Usage: %s [OPTION]...\n' "${0}"
}

print_help()
{
	cat <<EOF
\`configure' configures ${PACKAGE} ${VERSION} to adapt to many kinds of systems.

$(print_usage)

Configuration:
  -h, --help            display this help and exit
  -V, --version         display version information and exit
  -q, --quiet           do not print \`checking ...' messages
      --srcdir=SRCDIR   find the scripts in SRCDIR
                        default: configure dir

Installation directories:
  --prefix=PREFIX       install files under PREFIX
                        default: /usr/local
  --bindir=BINDIR       install scripts in BINDIR
                        default: PREFIX/bin
  --libdir=LIBDIR       install library scripts in LIBDIR
                        default: PREFIX/lib
  --datadir=DATADIR     expect to find data in DATADIR
                        default: PREFIX/share
  --mandir=MANDIR       install manual pages in MANDIR
                        default: DATADIR/man
  --localedir=LOCALEDIR install locales in LOCALEDIR
                        default: DATADIR/man
EOF
}

print_version()
{
	cat <<EOF
${package_name} ${package_version} configure
Not generated by GNU Autoconf

Copyright (C) 2011-2012 Patrick "P. J." McDermott
License: GNU GPL version 3 or later <http://www.gnu.org/licenses/gpl.html>.
This configure script is free software: you can redistribute and/or modify it.
There is NO WARRANTY, to the extent permitted by law.
EOF
}

parse_options()
{
	for _opt; do

		# Handle arguments of "--opt arg" options.
		if [ -n "${_prev}" ]; then
			eval "${_prev}=\$\{_opt\}"
			_prev=
			continue
		fi

		# Detect "--opt=arg" and "--opt arg" options.
		case "${_opt}" in
			*=*)
				_optarg="${_opt#*=}"
				_opt="${_opt%=*}"
				_optarg_set=true
				;;
			*)
				_optarg_set=false
				;;
		esac

		# Handle short and long options.
		case "${_opt}" in
			--)
				break
				;;
			-h|--help)
				print_help
				exit 0
				;;
			-V|--version)
				print_version
				exit 0
				;;
			-q|--quiet)
				quiet=true
				continue
				;;
		esac

		# Anything beyond this point should be a long option.
		case "${_opt}" in
			--with-*)
				_opt="${_opt#--with-}"
				_opts="${features}"
				_type=package
				;;
			--*)
				_opt="${_opt#--}"
				_opts="${long_opts_with_args}"
				_type=option
				;;
			*)
				printf 'unrecognized option: %s\n' "${_opt}" >&2
				exit 1
				;;
		esac

		grep "^${_opt}\$" >/dev/null 2>&1 <<EOF
"${_opts}"
EOF
		if [ ${?} -ne 0 ]; then
			printf 'invalid %s name: %s\n' "${_type}" "${_opt}" >&2
			exit 1
		fi
		if ${_optarg_set}; then
			eval ${_opt}=\$\{_optarg\}
		else
			_prev="${_opt}"
		fi

	done
}

find_dep_cmd()
{
	_dep="${1}"

	${quiet} || printf 'checking for %s... ' "${dep}"

	_old_ifs="${IFS}"
	IFS=:

	for _element in ${PATH}; do
		: ${_element=-.}
		if [ -f "${_element}/${_dep}" -a -x "${_element}/${_dep}" ]; then
			${quiet} || printf '%s/%s\n' "${_element}" "${_dep}"
			eval "${_dep}=${_element}/${_dep}"
			IFS="${_old_ifs}"
			return 0
		fi
	done

	IFS="${_old_ifs}"
	${quiet} || printf 'not found\n'
	missing_deps=true
	return 1
}

write_makefiles()
{
	# Make a script to edit input makefiles.
	_sed_script=
	for _var in ${subst_vars}; do
		_sed_script="${_sed_script}s&@${_var}@&$(eval echo \$\{"${_var}"\})&g;"
	done

	for _dir in . src lib locale man tests; do
		sed "${_sed_script}" "${srcdir}/${_dir}/Makefile.in" >"${_dir}/Makefile"
	done
}

main "${@}"