#!/bin/sh
#******************************************************************************#
#                                                                              #
#   configure                                                                  #
#   | Project : librtc                                                         #
#                                                                              #
#   By: thomas <thomas@touhey.fr>                                              #
#   Last updated: 2016/05/22 23:13:42                                          #
#                                                                              #
#******************************************************************************#
# This program 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 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.        #
#******************************************************************************#
# Help
usage() {
cat <<EOF
Usage: $0 [OPTION]

Defaults for the options are specified in brackets.

Installation directories:
  --prefix=PREFIX         main installation prefix [/opt/sh3eb-elf]

Fine tuning of the installation directories:
  --libdir=DIR            library files of the linker [PREFIX/sh3eb-elf/lib]
  --includedir=DIR        include files for the compiler [PREFIX/sh3eb-elf/include]
  --mandir=DIR            man pages [PREFIX/share/man]

EOF
exit 0
}

# Defaults
prefix=/opt/sh3eb-elf
libdir='$(IPREFIX)/sh3eb-elf/lib'
includedir='$(IPREFIX)/sh3eb-elf/include'
mandir='$(IPREFIX)/share/man'

# Args parsing
for arg ; do
case "$arg" in
--help|-h) usage ;;
--prefix=*) prefix=${arg#*=} ;;
--libdir=*) libdir=${arg#*=} ;;
--includedir=*) includedir=${arg#*=} ;;
--mandir=*) mandir=${arg#*=} ;;
-*) echo "$0: unknown option $arg" ;;
*) echo "$0: are you drunk ?" ;;
esac
done

# If Makefile configuration already exists, ragequit
if [ -f Makefile.cfg ]; then
	echo "Configuration is already loaded, skipping."
	exit 0
fi

# Create Makefile configuration
exec 3>&1 1>Makefile.cfg

cat <<EOF
#!/usr/bin/make -f
# Makefile configuration generated by ./configure
IPREFIX = $prefix
ILIBDIR = $libdir
IINCDIR = $includedir
IMANDIR = $mandir
EOF

exec 1>&3 3>&-
chmod +x Makefile.cfg

# We're done
echo "Configuration loaded, you can make now."
