#!/usr/bin/env bash

# This script is here for compatibility & may be removed in the future.

# get root directory
DIR_WORK="$(pwd)"
DIR_ROOT="$(dirname $0)"
cd "${DIR_ROOT}"
DIR_ROOT="$(pwd)"
DIR_SRC="${DIR_ROOT}/src"
DIR_INC="${DIR_SRC}/include"

if test -z "${PREFIX}"; then
	PREFIX=/usr/local
fi

if test -z "${EMBED_ICON}"; then
	EMBED_ICON=false
fi

if test ! -d "${DIR_SRC}"; then
	echo -e "\nERROR: could not find source directory: ${DIR_SRC}"
	exit 1
fi

TYPE="b"
STATIC=0

function usage {
    echo -e "\n    USAGE: ./configure [--help] [--type=TYPE] [--static]"
	echo -e "\n    Options:"
	echo -e "\t--help\t\tShow this help text"
    echo -e "\n\t--type=TYPE\tSet to \"b\" (default) for binary executable"
    echo -e "\t\t\t\"s\" for Python script"
    echo -e "\n\t--static\tCreate statically linked executable (binary"
	echo -e "\t\t\tonly)"
	echo -e "\n    Environment Variables:"
	echo -e "\tPREFIX\t\tSets install prefix for 'make install' target\n\t\t\tType: string (default: /usr/local)"
	echo -e "\n\tEMBED_ICON\tEmbeds icon resource in executable (Win32\n\t\t\tonly)\n\t\t\tType: bool (default: false)\n\n"
}

MAX_ARGS=2

if [ "${#}" -gt "${MAX_ARGS}" ]
then
    usage
    exit 1
fi

for A in $@; do
	if [[ "${A}" == *"="* ]]; then
		K=($(echo ${A} | tr  "=" " "))
		if [ "${K[0]}" != "--type" ]; then
			echo -e "\nERROR: Unknown argument: \"${K[0]}\""
			exit 1
		else
			if [[ "${K[1]}" == "b" || "${K[1]}" == "s" ]]; then
				TYPE="${K[1]}"
			else
				echo -e "\nERROR: Bad argument for option \"${K[0]}\"\n"
				exit 1
			fi
		fi
	else
		if [ "${A}" == "--help" ]; then
			usage
			exit 0
		elif [ "${A}" == "--type" ]; then
			echo -e "\nERROR: Argument ${A} requires a value: ${A}=TYPE"
			exit 1
		elif [ "${A}" == "--static" ]; then
			STATIC=1
		else
			echo -e "\nERROR: Unknown argument \"${A}\""
			exit 1
		fi
	fi
done

if [[ "${TYPE}" == "s" && "${STATIC}" -ne "0" ]]; then
	echo -e "\nERROR: --static can only be specified with type \"b\""
	exit 1
fi

FILES_C="$(echo `find "${DIR_SRC}" -type f -name "*.cpp"` | sed -e 's|\n| |g')"
if ${EMBED_ICON}; then
	FILES_C="${FILES_C} icon_resource.o"
fi

# create Makefile
if [ "${TYPE}" == "b" ]
then
	if [ "${STATIC}" -gt "0" ]
	then
		MAKE="bin2header: man ${FILES_C}\n\tg++ -std=c++11 -static -O2 -s ${FILES_C} -I${DIR_INC} -o bin2header"
	else
		MAKE="bin2header: man ${FILES_C}\n\tg++ -std=c++11 -O2 -s ${FILES_C} -I${DIR_INC} -o bin2header"
	fi
elif [ "${TYPE}" == "s" ]
then
	MAKE="bin2header: man src/bin2header.py\n\t@cp -v src/bin2header.py bin2header"
fi

# Install file
#INSTALL="#! /bin/bash\n\ncp bin2header /usr/local/bin\ncp ${DIR_ROOT}/man/bin2header.1.gz /usr/#share/man/man1"
#echo -e "${INSTALL}" > "install"
#chmod +x "install"

#MAKEINSTALL="install:\n\t./install"
MAKEMAN=".PHONY: man\nman: ${DIR_ROOT}/man/bin2header.1\n\tgzip -fk9 ${DIR_ROOT}/man/bin2header.1; \\
\n\tmv ${DIR_ROOT}/man/bin2header.1.gz ./"
MAKEINSTALL="install:\n\t@mkdir -vp \$(DESTDIR)${PREFIX}/bin \$(DESTDIR)${PREFIX}/share/man/man1\n\t@cp -v bin2header \$(DESTDIR)${PREFIX}/bin && cp -v bin2header.1.gz \$(DESTDIR)${PREFIX}/share/man/man1"
MAKEUNINSTALL="uninstall:\n\t@rm -vf \$(DESTDIR)${PREFIX}/bin/bin2header \$(DESTDIR)${PREFIX}/share/man/man1/bin2header.1.gz"
MAKEICON="icon_resource.o: ${DIR_ROOT}/icon_resource.rc\n\twindres ${DIR_ROOT}/icon_resource.rc icon_resource.o"
MAKECLEAN="clean:\n\t@rm -vf bin2header.1.gz bin2header *.o"
MAKEDISTCLEAN="distclean: clean"
MAKEFILE="${MAKE}\n${MAKEICON}\n${MAKEMAN}\n${MAKEINSTALL}\n${MAKEUNINSTALL}\n${MAKECLEAN}\n${MAKEDISTCLEAN}"
echo -e "${MAKEFILE}" > "${DIR_WORK}/Makefile"
echo -e "\nDone!"
echo -e "\nNow run \"make\" and \"make install\"\n"

exit 0
