#!/bin/sh
# configure

# $Id: configure,v 1.5 2003/01/10 21:00:19 andy Exp $

# Script for setting build options.
 
# Copyright (C) 2002 Andy Goth <unununium@openverse.com>
# For more information visit http://ioioio.net/devel/install-log/
#
# 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 2 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, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA  02111-1307, USA.

# These are the options that can be set here
OPTS="BINDIR MANDIR LOGDIR LOGSELF EDITOR VERBOSITY CHROOTVAR INCLUDE EXCLUDE"
MKOPTS="BINDIR MANDIR LOGDIR LOGSELF"
RCOPTS="LOGDIR EDITOR VERBOSITY CHROOTVAR INCLUDE EXCLUDE"
MANOPTS="LOGDIR CHROOTVAR"
CFGOPTS="LOGDIR EDITOR VERBOSITY CHROOTVAR"

# Remember this now
ARGS="${@}"

# Some functions
say () { [ ${QUIET} -eq 0 ] && ([ ${#} -gt 0 ] && echo ${@} || cat) }
die () { [ ${#} -gt 0 ] && echo ${@} || cat; exit 1; }
do_help () {
	die <<EOF
Usage: ./configure [OPTION]...

Options:  (default value listed in brackets)
  -h, --help           Display this help and exit.
  -V, --version        Display version information and exit.
  -q, --quiet          Don't display anything.
  --prefix=PREFIX      Install in directories relative to $PREFIX [/usr/local].
  --bindir=DIR         User executables [$PREFIX/bin].
  --mandir=DIR         Section 1 man documentation [$PREFIX/share/man].
  --logdir=DIR         Installation log base [/var/log/install-log].
  --editor=NAME        Name of default editor [/usr/bin/vi].
  --verbosity=NUM      Verbosity of output [0].
  --chroot-var=NAME    Variable indicating to chroot [LFS].
  --include=DIR:...    Colon-separated list of directories to scan.
  --exclude=DIR:...    Colon-separated list of directories to skip.
  --enable-log-self    Log install-log's installation (default).
  --disable-log-self   Don't try to log install-log.
EOF
}
do_version () {
	die "install-log configure, version \
$(grep VERSION_STR version.h | cut -f2 -d\"), \
$(grep DATE_STR version.h | cut -f2 -d\")"
}
do_error () {
	die <<EOF
configure: error: unrecognized option: ${1}
Try './configure --help' for more information.
EOF
}

# Get defaults
QUIET=0
if [ -d /usr/local/bin -a ! -L /usr/local ]; then	PREFIX="/usr/local"
elif [ -d /usr/bin ]; then				PREFIX="/usr"
elif [ -d /static/bin ]; then				PREFIX="/static"
elif [ -d ${HOME}/usr/bin ]; then			PREFIX="${HOME}/usr"
elif [ -d ${HOME}/local/bin ]; then			PREFIX="${HOME}/local"
else							PREFIX="/usr"
fi
BINDIR="bin"
if [ -d ${PREFIX}/share/man/man1 ]; then	MANDIR="share/man/man1"
else						MANDIR="man/man1"
fi
if [ -d /var/install-logs ]; then	LOGDIR="/var/install-logs"
else					LOGDIR="/var/log/install-log"
fi
if [ -x /usr/bin/vi ]; then		EDITOR="/usr/bin/vi"
elif [ -x /usr/bin/vim ]; then		EDITOR="/usr/bin/vim"
elif [ -x /usr/bin/emacs ]; then	EDITOR="/usr/bin/emacs"
elif [ -x /usr/bin/less ]; then		EDITOR="/usr/bin/less"
elif [ -x /bin/less ]; then		EDITOR="/bin/less"
else					EDITOR="/bin/cat"
fi
VERBOSITY=0
CHROOTVAR="LFS"
LOGSELF=1
INCLUDE="/bin:/lib:/sbin:/usr"
[ -d /opt -a ! -L /opt ] && INCLUDE="${INCLUDE}:/opt"
[ -d /lib/modules ] && EXCLUDE=":/lib/modules" || EXCLUDE=""
for DIR in /usr /usr/local /opt; do
	[ -d "${DIR}" -a ! -L "${DIR}" ] || continue
	for SUB in info share/info; do
		[ -d "${DIR}/${SUB}" ] || continue
		[ -L "${DIR}/${SUB}" ] && continue
		[ -f "${DIR}/${SUB}/dir" ] || continue
		EXCLUDE="${EXCLUDE}:${DIR}/${SUB}/dir"
	done
	for SUB in src etc var tmp; do
		[ -d "${DIR}/${SUB}" ] || continue
		[ -L "${DIR}/${SUB}" ] && continue
		EXCLUDE="${EXCLUDE}:${DIR}/${SUB}"
	done
done
[ ${#EXCLUDE} -gt 0 ] && EXCLUDE=${EXCLUDE:1:(${#EXCLUDE} - 1)}

# Parse arguments
while [ "${1}" ]; do
	case "${1}" in
	-h|--help)		do_help;;
	-V|--version)		do_version;;
	-q|--quiet)		QUIET=1;;
	--prefix=*)		PREFIX=$(echo "${1}" | sed s/.*=//);;
	--bindir=*)		BINDIR=$(echo "${1}" | sed s/.*=//);;
	--mandir=*)		MANDIR=$(echo "${1}" | sed s/.*=//);;
	--logdir=*)		LOGDIR=$(echo "${1}" | sed s/.*=//);;
	--editor=*)		EDITOR=$(echo "${1}" | sed s/.*=//);;
	--verbosity=*)		VERBOSITY=$(echo "${1}" | sed s/.*=//);;
	--chroot-var=*)		CHROOTVAR=$(echo "${1}" | sed s/.*=//);;
	--include=*)            INCLUDE=$(echo "${1}" | sed s/.*=//);;
	--exclude=*)            EXCLUDE=$(echo "${1}" | sed s/.*=//);;
	--enable-log-self)	LOGSELF=1;;
	--disable-log-self)	LOGSELF=0;;
	*)			do_error ${1};;
	esac
	shift
done

# Fix relative paths
[ "${PREFIX:(-1)}" != / ] && PREFIX="${PREFIX}/"
[ "${BINDIR:0:1}" != / ] && BINDIR="${PREFIX}${BINDIR}"
[ "${MANDIR:0:1}" != / ] && MANDIR="${PREFIX}${MANDIR}"
[ "${LOGDIR:0:1}" != / ] && LOGDIR="${PREFIX}${LOGDIR}"

# Create configure.mk
say -n "Creating configure.mk..."
rm -f configure.mk
for OPT in ${MKOPTS}; do
	eval echo "${OPT} := \${${OPT}}" >> configure.mk
done
say " done."

# Create configure.h
say -n "Creating configure.h..."
cat > configure.h << EOF
/* configure.h; generated by configure on $(date) */

#ifndef SEEN_CONFIGURE_H
#define SEEN_CONFIGURE_H

EOF
for OPT in ${CFGOPTS}; do
	eval echo "\#define ${OPT} \\\"\${${OPT}}\\\"" >> configure.h
done
cat >> configure.h << EOF

#endif

/* EOF */
EOF
say " done."

# Create configure.c
say -n "Creating configure.c..."
cat > configure.c << EOF
/* configure.c; generated by configure on $(date) */

#include <stdlib.h>
#include "configure.h"

const char* default_include[] = {
$(echo "${INCLUDE}" | sed 's@^@"@;s@:@", "@g;s@$@", NULL@')
};

const char* default_exclude[] = {
$(echo "${EXCLUDE}" | sed 's@^@"@;s@:@", "@g;s@$@", NULL@')
};

/* EOF */
EOF
say " done."

# Create configure.log
say -n "Creating configure.log..."
echo "${0} ${ARGS}" > configure.log
for OPT in ${OPTS}; do
	eval echo "${OPT}=\${${OPT}}" >> configure.log
done
say " done."

# Create install-log.1
say -n "Creating install-log.1..."
SCR=""
for OPT in ${MANOPTS}; do
	SCR="${SCR}s@\$(${OPT})@$(eval echo '${'${OPT}'//-/\\\\-}')@;"
done
sed "${SCR}" install-log.1.in > install-log.1
say " done."

# Create install-log.rc
say -n "Creating install-log.rc..."
cat > install-log.rc << EOF
# install-log.rc; generated by configure on $(date)

EOF
for OPT in ${RCOPTS}; do
	eval echo "${OPT}=\${${OPT}}" >> install-log.rc
done
cat >> install-log.rc << EOF
EDIT=0
FORCE=0
QUIET=0

# EOF
EOF
say " done."

# Display the final configuration values
say < configure.log

# EOF
