#!/bin/sh

fc_put_help() {
cat <<_ZYXW_
[Usage] ./configure [option] [VAR=VALUE]...

Options:
  --help         display this help
  --debug        create for debug (CFLAGS='-g')
  --prefix=[PREFIX]  install directory
                     (default = /usr/local)

Optional flags:

Variables:
  CC       c compiler command
  CFLAGS   c compiler flags (-I<include_path> etc)
  LDFLAGS  linker flags (-L<library_path> etc)
  LIBS     libraries (-l<lib>)
_ZYXW_
exit 0
}

fc_check_command_err() {
	if ! type "$1" > /dev/null 2>&1;then
		echo "[!] '$1' command not found."
		if test -z "$2";then tmp=$1; else tmp=$2; fi
		echo "please install: $tmp"
		exit 1
	fi
}

if test "$1" = "--help";then
	fc_put_help
fi

fc_check_command_err ninja "ninja or ninja-build"
fc_check_command_err pkg-config "pkg-config or pkgconf"

echo "compiling configure.c..."
if ! cc -o configure_c configure.c -ldl;then
	exit 1
fi

if test ! -d build;then
	echo "mkdir ./build"
	mkdir build
fi

echo "configure..."
./configure_c "$@"
