#!/bin/sh

pkg_name="gnunet"
pkg_default_features=""
pkg_optional_features="experimental monolith testbed exclude-manpages malicious benchmarks poisoning nse-histogram disable-autostart install-rpath heap-statistics logging coverage"
pkg_optional_dependencies=""

# DO NOT EDIT BELOW THIS LINE
standard_dirs="prefix exec_prefix bindir sbindir libexecdir sysconfdir sharedstatedir localstatedir runstatedir libdir includedir oldincludedir datarootdir datadir infodir localedir mandir docdir htmldir dvidir pdfdir psdir srcdir mesonbuilddir"
standard_utils="AR AS BISON CC CXX CPP FLEX INSTALL LD LDCONFIG LEX MAKE MAKEINFO RANLIB TEXI2DVI YACC CHGRP CHMOD CHOWN MKNOD RM NINJA MESON"
standard_flags="ARFLAGS BISONFLAGS CFLAGS CXXFLAGS CPPFLAGS FLEXFLAGS INSTALLFLAGS LDFLAGS LDCONFIGFLAGS LFLAGS MAKEFLAGS MAKEINFOFLAGS RANLIBFLAGS TEXI2DVIFLAGS YACCFLAGS CHGRPFLAGS CHMODFLAGS CHOWNFLAGS MKNODFLAGS"
standard_vars="INSTALL_DATA INSTALL_PROGRAM INSTALL_SCRIPT"
generated_comment="# This file was generated by configure. DO NOT edit it directly."

# Save arguments
cat > config.status <<EOF
#!/bin/sh
$generated_comment
$0 $*
EOF
chmod 755 config.status

# Parse arguments
oldifs="$IFS"
while [ "$#" -gt "0" ]; do
	arg="$1"
	shift
	argsave="$@"

	IFS="="
	set -- $arg
	IFS="$oldifs"
	arg="$1"
	shift
	argparam="$@"

	case "$arg" in
		--help|-h)
			cat <<-EOF
			Usage: $0 [options...]
			General options:

			<variable>=<value>       Overrides default utils and flags, e.g. CC=gcc
			EOF
      echo ""
      echo "Directory variables:"
			for sdir in $standard_dirs; do
				echo "--$sdir=<directory>"
			done
      echo ""
      echo "Options specific to $pkg_name:"
			for feat in $pkg_optional_features; do
				echo "--enable-$feat[=arg]"
			done
			for feat in $pkg_default_features; do
				echo "--disable-$feat"
			done
			for dep in $pkg_optional_dependencies; do
				echo "--with-$dep"
			done

			echo
			echo Other options will be ignored. Please see the README for additional information.
			exit 0
			;;
		--enable-*)
			for feat in $pkg_optional_features $pkg_default_features; do
        ft=$(echo $feat | tr - _)
				if [ "--enable-$feat" = "$arg" ] && [ -z "$argparam" ]; then
					eval "enable_$ft=true"
				elif [ "--enable-$feat" = "$arg" ]; then
          if [ "$argparam" = "yes" ]; then
					  eval "enable_$ft=\"true\""
          else
					  eval "enable_$ft=\"$argparam\""
          fi
				fi
			done
			;;
		--disable-*)
			argsave="--enable-${arg#--disable-}=false $argsave"
			;;
		--with-*)
			for dep in $pkg_optional_dependencies; do
				if [ "--with-$dep" = "$arg" ] && [ -z "$argparam" ]; then
					eval "with_$dep=yes"
				elif [ "--with-$dep" = "$arg" ]; then
					eval "with_$dep=\"$argparam\""
				fi
			done
			;;
		--without-*)
			argsave="--with-${arg#--without-}=no $argsave"
			;;
		--*)
			for dir in $standard_dirs; do
				[ "--$dir" = "$arg" ] && eval "var_$dir=\"$argparam\""
			done
			;;
		*)
			for flag in $standard_utils $standard_flags $standard_vars; do
				[ "$flag" = "$arg" ] && eval "$flag=\"$argparam\""
			done
			for flag in $standard_flags; do
				[ "$flag" = "$arg" ] && eval export "$flag"
			done
			;;
	esac

	set -- $argsave
done

# Set default values
for util in $standard_utils; do
	case "$util" in
		CPP) eval "$util=\"\${${util}:-\\\$(CC) -E}\"" ;;
		CXX) eval "$util=\"\${${util}:-g++}\"" ;;
		RM)  eval "$util=\"\${${util}:-rm -f}\"" ;;
		MESON)  eval "$util=\"\${${util}:-meson}\"" ;;
		NINJA)  eval "$util=\"\${${util}:-ninja}\"" ;;
		*)   eval "$util=\"\${${util}:-${util}}\"" ;;
	esac
done
for feat in $pkg_optional_features; do
  ft=$(echo $feat | tr - _)
	eval "enable_$ft=\${enable_$ft:-false}"
done
for feat in $pkg_default_features; do
	eval "enable_$feat=\${enable_$feat:-true}"
done
for feat in $pkg_optional_dependencies; do
	eval "with_$feat=\${with_$feat:-no}"
done

# Find source files
if [ -z "$var_srcdir" ]; then
	[ -f ../Makefile.in ] && var_srcdir=..
	[ -f ./Makefile.in ]  && var_srcdir=.
fi
if [ -z "$var_srcdir" ]; then
	echo "Source files not found. Please specify a directory using --srcdir=..." >&2
	exit 1
fi

# Write Makefile
cat > Makefile <<EOF
$generated_comment
SHELL = /bin/sh
VPATH = ${var_srcdir}

mesonbuilddir = ${var_mesonbuilddir:-build}
srcdir = ${var_srcdir}
prefix = ${var_prefix:-/usr/local}
exec_prefix = ${var_exec_prefix:-\$(prefix)}
bindir = ${var_bindir:-\$(exec_prefix)/bin}
sbindir = ${var_sbindir:-\$(exec_prefix)/sbin}
libexecdir = ${var_libexecdir:-\$(exec_prefix)/libexec}
datarootdir = ${var_datarootdir:-\$(prefix)/share}
datadir = ${var_datadir:-\$(datarootdir)}
sysconfdir = ${var_sysconfdir:-\$(prefix)/etc}
sharedstatedir = ${var_sharedstatedir:-\$(prefix)/com}
localstatedir = ${var_localstatedir:-\$(prefix)/var}
runstatedir = ${var_runstatedir:-\$(localstatedir)/run}
includedir = ${var_includedir:-\$(prefix)/include}
oldincludedir = ${var_oldincludedir:-/usr/include}
docdir = ${var_docdir:-\$(datarootdir)/doc/"$pkg_name"}
infodir = ${var_infodir:-\$(datarootdir)/info}
htmldir = ${var_htmldir:-\$(docdir)}
dvidir = ${var_dvidir:-\$(docdir)}
pdfdir = ${var_pdfdir:-\$(docdir)}
psdir = ${var_psdir:-\$(docdir)}
libdir = ${var_libdir:-\$(exec_prefix)/lib}
lispdir = ${var_lispdir:-\$(datarootdir)/emacs/site-lisp}
localedir = ${var_localedir:-\$(datarootdir)/locale}
mandir = ${var_mandir:-\$(datarootdir)/man}
manext = .1
EOF

if [ -d ${var_mesonbuilddir:-build} ]; then
  echo "${var_mesonbuilddir:-build} already exists, deleting with"
  $RM -r ${var_mesonbuilddir:-build} || exit 1
fi

for var in $standard_utils $standard_flags $standard_vars; do
	eval "echo \"${var} = \$${var}\" >> Makefile"
done
echo >> Makefile

mesonfeatopts=""
for feat in $pkg_optional_features $pkg_default_features; do
  ft=$(echo $feat | tr - _)
	eval "echo \"enable_$ft=\${enable_$ft}\" >> Makefile"
  if [ "coverage" = $feat ]; then
    mfeat="b_$feat"
  else
    mfeat=$feat
  fi
  eval "mesonfeatopts=\"$mesonfeatopts -D$mfeat=\${enable_$ft}\""
done
for dep in $pkg_optional_dependencies; do
	eval "echo \"with_$dep=\${with_$dep}\" >> Makefile"
done
echo >> Makefile

cat "${var_srcdir}/Makefile.in" >> Makefile

# Create default targets, if not already present
default_target() {
	grep "^$1:" Makefile >/dev/null && return
	echo ".PHONY: $1" >> Makefile
	echo "$1: $2" >> Makefile
	cat >> Makefile
	echo >> Makefile
}

#default_target Makefile <<EOF
#	./config.status
#EOF
for format in html dvi pdf ps; do
	default_target "$format" </dev/null
	default_target "install-$format" "$format" </dev/null
done
default_target check </dev/null
default_target mostlyclean clean </dev/null
default_target distclean clean <<EOF
	\$(RM) Makefile config.status
EOF
default_target maintainer-clean distclean </dev/null
default_target install-strip <<EOF
	\$(MAKE) INSTALL_PROGRAM='\$(INSTALL_PROGRAM) -s' \\
		INSTALL_SCRIPT='\$(INSTALL_SCRIPT)' install
EOF

mesondiropts=""
for dr in $standard_dirs; do
  if [ $dr = "srcdir" ]; then
    continue
  fi
  if [ $dr = "mesonbuilddir" ]; then
    continue
  fi
  eval "vn=\${var_$dr}"
  if [ ! -z $vn ]; then
    eval "mesondiropts=\"$mesondiropts -D$dr=\${var_$dr}\""
  fi
done

${MESON} setup \
	-Ddefault_library=shared \
  $mesondiropts \
  $mesonfeatopts \
	${var_mesonbuilddir:-build} \
	${var_srcdir}

# Done
echo "Package $pkg_name configured successfully."
echo "Run 'make' to build and 'make install' to install the package"

