#!/bin/sh
#
# Copyright (C) 2005-2020
#       pancake <pancake@nopcode.org>
#
# acr 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.
#
# acr 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 acr; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#===========================================================================
#
# ACR: AutoConf Replacement
#
# @license:  GPLv2
# @author:   pancake <pancake@nopcode.org>
# @homepage: http://github.com/radare/acr
# @version:  @VERSION@
#
# acr:
#   Main script that wraps the rest of acr scripts (mainly acr-sh)
#

DEBUG=0
EMBED=0
EXEC=0
NOPE=0
PBAR=0
STRIP=0
MAKEFILE=0
MAKEDIST=0
RECOVER=0
CONFIGURE="./configure"
ACRFILE="configure.acr"
TMP_FILE=""
ACR_BIN_DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
ACR_SH="${ACR_BIN_DIR}/acr-sh"
ACR_CAT="${ACR_BIN_DIR}/acr-cat"
ACR_VERSION="@VERSION@"

if [ ! -x "${ACR_SH}" ]; then
	ACR_SH=acr-sh
fi
if [ ! -x "${ACR_CAT}" ]; then
	ACR_CAT=acr-cat
fi

if [ "${ACR_VERSION}" = "@VERSION@" ] && [ -f "${ACR_BIN_DIR}/../configure.acr" ]; then
	ACR_VERSION=$(awk '/^VERSION[ \t]+/ {print $2; exit}' "${ACR_BIN_DIR}/../configure.acr")
fi

control_c() {
	echo
	echo "^C : interrupted execution."
	[ -n "${TMP_FILE}" ] && rm -f ${TMP_FILE}
	exit 1
}

trap control_c 2

show_version() {
	echo "acr: version ${ACR_VERSION}"
	exit 0
}

show_usage() {
	cat<<EOF
acr: Usage '$0' [-flags] [file] [args]
  -d         runs in debug mode.
  -D         creates the distribution tarball.
  -e         embed configure.acr into final script as comment.
  -h         show this help.
  -m         generate main Makefile.acr file.
  -n         do not generate the final configure script. (parse only)
  -o [file]  output file (default: configure).
  -p         show progress bar.
  -r         recovery mode. (runs acr-cat)
  -s         strip default code generation.
  -v         show version information.
  -w [num]   cats the configure.acr highlighting the [num] word.
  -x         execute the script directly."
EOF
	exit $1
}

ARGS=`getopt sdDehmno:prvw:x $*`
if [ $? -ne 0 ]; then
	show_usage 1
fi
set -- $ARGS
while [ $# -gt 0 ]; do
	case $1 in
	"-w"|"--word")
		WORD="$2"
		shift
		;;
	"-x"|"--exec")
		EXEC=1
		break;
		;;
	"-D"|"--dist")
		echo "[1/4] Parsing configure.acr." >&2
		PBAR=1
		MAKEDIST=1
		NOPE=1
		;;
	"-e"|"--embed")
		EMBED=1
		;;
	"-s"|"--strip")
		STRIP=1
		;;
	"-v"|"-V"|"--version")
		show_version
		;;
	"-d"|"--debug")
		DEBUG=1
		;;
	"-m"|"--makefile")
		MAKEFILE=1
		;;
	"-h"|"--help")
		show_usage 0
		;;
	"-r"|"--recover"|"--recovery")
		ACRFILE=${CONFIGURE}
		RECOVER=1
		;;
	"-p"|"--progress-bar")
		PBAR=1
		;;
	"-n"|"--do-nothing")
		NOPE=1
		;;
	"-o"|"--output")
		CONFIGURE=$2
		shift
		;;
	"--")
		;;
	*)
		ACRFILE="$1"
		;;
	esac
	shift
done

#if [ "`echo ${ACRFILE}|cut -c 1`" = "-" ]; then
#	echo "error: unknown flag '${ACRFILE}'." >&2
#	exit 1
#fi

if [ "${RECOVER}" = 1 ]; then
	exec "${ACR_CAT}" ${ACRFILE}
	# TODO manage output
	exit 1
fi

if [ ! -f "${ACRFILE}" ]; then
	echo "error: file ${ACRFILE} not found" >&2
	exit 1
fi

if [ -n "${WORD}" ]; then
	N=0
	WORDS=`cat ${ACRFILE}`
	NW=`echo $WORDS | wc -w `

	test $WORD -gt $NW &&\
		echo "Word number out of range (limit $NW)." &&\
		exit 1

	for A in ${WORDS} ; do
		N=`expr $N + 1`
		if [ $N = $WORD ]; then
			printf -- "\e[35m$A\e[0m "
		else
			printf -- "$A "
		fi
	done
	echo
	exit 1
fi

if [ "${EXEC}" = 1 ]; then
	shift
	TMP_FILE=`mktemp -t acr.XXXXXX`
	chmod 700 ${TMP_FILE}
	"${ACR_SH}" "${ACRFILE}" "${CONFIGURE}" > ${TMP_FILE}
	sh ${TMP_FILE} $@
	rm ${TMP_FILE}
else
	if [ "${MAKEFILE}" = 1 ]; then
		env MAKEDIST=${MAKEDIST} STRIP=${STRIP} MAKEFILE=1 PBAR=${PBAR} NOPE=${NOPE} \
		"${ACR_SH}" "${ACRFILE}" "${CONFIGURE}" > /dev/null
		RET=$?
	elif [ "${NOPE}" = 1 ]; then
		env MAKEDIST=${MAKEDIST} STRIP=${STRIP} NOPE=1 PBAR=${PBAR} DEBUG=${DEBUG} \
		"${ACR_SH}" "${ACRFILE}" "${CONFIGURE}" > /dev/null
		RET=$?
	else
		TMP_FILE=`mktemp -t acr.XXXXXX`
		env NOPE=0 DEBUG=${DEBUG} STRIP=${STRIP} PBAR=${PBAR} \
		"${ACR_SH}" "${ACRFILE}" "${CONFIGURE}" > ${TMP_FILE}
		RET=$?
		if [ "$?" = 0 ]; then
			mv ${TMP_FILE} "${CONFIGURE}"
			chmod +x "${CONFIGURE}"
			[ "${EMBED}" = 1 ] && \
				cat ${ACRFILE} | awk '{print "#ACR# " $0}' >> "${CONFIGURE}"
		else
			rm -f ${TMP_FILE}
		fi
	fi

	if [ "${RET}" = 0 ]; then
		[ "${NOPE}" = 0 ] && echo "acr: all done."
		exit 0
	else
		echo "acr: error handled by acr."
		exit 1
	fi
fi
