#!/bin/bash
is_exists(){
	echo -ne "Checking $1 "
	p=$(which "$1")
	if [ "$p" == "" ] ; then
		echo "(not found)"
		exit 1
	else
		echo "(yes)"
	fi
}
helpmsg(){
	echo "Usage ./configure  [options]"
	echo "  --prefix      : Use custom prefix location (default /usr)"
	echo "  --clear       : Remove alt fles"
	echo "  --help        : Show this message"
}
#defaults
export prefix="/usr/local"
export nsl=true
cp Makefile.in Makefile.ac

for arg in $*
do
	if echo $arg | grep "^--clear" &>/dev/null ; then
		[ -f Makefile.ac ] && rm -f Makefile.ac
		[ -f Makefile ] && rm -f Makefile
		[ -d build ] && rm -rf build
		exit 0
	elif echo $arg | grep "^--help" &>/dev/null ; then
		helpmsg
		exit 0
	elif echo $arg | grep "^--prefix=" &>/dev/null; then
		export prefix=$(echo "$arg" | sed "s/^.*=//g")
	fi
done
#Checking program
for item in msgfmt install python3 chown chmod
do
	is_exists $item
done
sed -i "s|@prefix@|$prefix|g" Makefile.ac
echo -e "    Prefix: $prefix"
echo -e "    Locale-Support: $nsl"
mv Makefile.ac Makefile
