#!/bin/bash

#
# NetLabel Tools Configure Script
#
# Copyright (c) 2013 Red Hat <pmoore@redhat.com>
# Author: Paul Moore <pmoore@redhat.com>
#

#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# 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/>.
#

# configuration defaults
opt_prefix="/usr/local"
opt_libdir=""
opt_systemd="no"

# output files
cnf_mk_file="configure.mk"
cnf_h_file="configure.h"

####
# functions

function test_deps() {
	[[ -z "$1" ]] && return 0
	which "$1" >& /dev/null && return 0
	return 1
}

function verify_deps() {
	[[ -z "$1" ]] && return
	if ! test_deps "$1"; then
		echo "error: install \"$1\" and include it in your \$PATH"
		exit 1
	fi
}

function msg_usage() {
	cat << EOF
Configure the NetLabel Tools, for this system.

Usage:
  ./configure <options>

Options:
* general configuration
  -h, --help		display this help and exit
* installation configuration
  --prefix=PREFIX	installation base [/usr/local]
  --libdir=DIR		library directory [/usr/local/lib]
  --enable-systemd	enable systemd support
EOF
}

function msg_summary() {
	cat << EOF
 CONFIGURATION SUMMARY
  version:		${VERSION_MAJOR}.${VERSION_MINOR}
  installation base:	$opt_prefix
  library directory:	$opt_libdir
  systemd support:	$opt_systemd
EOF
}

function msg_error() {
	echo "error: $@"
}

function cnf_mk_header() {
	echo "# generated by configure on $(date -R)" >> $cnf_mk_file
	echo "#   options: \"$opt_str\"" >> $cnf_mk_file
	echo "" >> $cnf_mk_file
}

function cnf_mk_footer() {
	echo "" >> $cnf_mk_file
}

function cnf_mk_entry() {
	[[ $# -ne 2 ]] && return
	case "$2" in
	no)
		echo "$1 = 0" >> $cnf_mk_file
		;;
	yes)
		echo "$1 = 1" >> $cnf_mk_file
		;;
	*)
		echo "$1 = \"$2\"" >> $cnf_mk_file
	esac
}

function cnf_h_header() {
	echo "/* generated by configure on $(date -R) */" >> $cnf_h_file
	echo "/*   options: \"$opt_str\" */" >> $cnf_h_file
	echo "" >> $cnf_h_file
	echo "#ifndef _CONFIGURE_H" >> $cnf_h_file
	echo "#define _CONFIGURE_H" >> $cnf_h_file
	echo "" >> $cnf_h_file
}

function cnf_h_footer() {
	echo "" >> $cnf_h_file
	echo "#endif" >> $cnf_h_file
	echo "" >> $cnf_h_file
}

function cnf_h_entry() {
	[[ $# -ne 2 ]] && return
	case "$2" in
	no)
		echo "#undef $1" >> $cnf_h_file
		;;
	yes)
		echo "#define $1	1" >> $cnf_h_file
		;;
	*)
		echo "#define $1	$2" >> $cnf_h_file
	esac
}

function cnf_reset() {
	cat /dev/null > $cnf_mk_file
	cat /dev/null > $cnf_h_file
}

function cnf_header() {
	cnf_mk_header
	cnf_h_header
}

function cnf_entry() {
	cnf_mk_entry "$1" "$2"
	cnf_h_entry "$1" "$2"
}

function cnf_footer() {
	cnf_mk_footer
	cnf_h_footer
}

function tmpl_filter() {
	name="echo \$$1"
	val="$(eval $name)"
	cat - | sed -e 's/%%'"$1"'%%/'"${val//\//\\/}"'/g;'
}

####
# main

#
# setup
#

# verify script dependencies
verify_deps getopt

# parse the command line options
opt_str="$@"
opt=$(getopt -n "$0" --options "h" --longoptions "help,prefix:,libdir:,enable-systemd" -- "$@")
eval set -- "$opt"
while [[ $# -gt 0 ]]; do
	case "$1" in
	--prefix)
		opt_prefix="$2"
		shift 2
		;;
	--libdir)
		opt_libdir="$2"
		shift 2
		;;
	--enable-systemd)
		opt_systemd="yes"
		shift
		;;
	-h|--help)
		msg_usage
		exit 0
		;;
	--)
		shift
		;;
	*)
		msg_usage
		exit 1
	esac
done

# validate the options
if [[ -e "$opt_prefix" && ! -d "$opt_prefix" ]]; then
	msg_error "install prefix ($opt_prefix) is not a directory"
	exit 1
fi
if [[ -z $opt_libdir ]]; then
	opt_libdir="$opt_prefix/lib"
fi
if [[ -e "$opt_libdir" && ! -d "$opt_libdir" ]]; then
	msg_error "libdir ($opt_libdir) is not a directory"
	exit 1
fi

#
# automatic configuration
#

# generate the version files
. ./version_info
VERSION_RELEASE="${VERSION_MAJOR}.${VERSION_MINOR}"
rm -f ./version_info.mk
echo "# generated by configure on $(date -R)" >> ./version_info.mk
echo "VERSION_MAJOR=$VERSION_MAJOR" >> ./version_info.mk
echo "VERSION_MINOR=$VERSION_MINOR" >> ./version_info.mk
echo "VERSION_RELEASE=$VERSION_RELEASE" >> ./version_info.mk

#
# finish
#

# reset the configuration files
cnf_reset
cnf_header

# output the configuration files
cnf_mk_entry "CONF_INSTALL_PREFIX" "$opt_prefix"
cnf_mk_entry "CONF_INSTALL_LIBDIR" "$opt_libdir"
cnf_mk_entry "CONF_SYSTEMD" "$opt_systemd"

# configuration footer
cnf_footer

# display a summary and exit
msg_summary
exit 0
