#!/bin/bash
#*****************************************************************************
# gta5view Grand Theft Auto V Profile Viewer
# Copyright (C) 2018 Syping
#
# 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/>.
#*****************************************************************************

# Argbash generated code
die()
{
   local _ret=$2
   test -n "$_ret" || _ret=1
   test "$_PRINT_HELP" = yes && print_help >&2
   echo "$1" >&2
   exit ${_ret}
}
begins_with_short_option()
{
   local first_option all_short_options
   all_short_options='h'
   first_option="${1:0:1}"
   test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
_arg_prefix=
_arg_qmake=
print_help ()
{
   printf '%s\n' "gta5view Configure Script"
   printf 'Usage: %s [--prefix <arg>] [--qmake <arg>] [-h|--help]\n' "$0"
   printf '\t%s\n' "-h,--help: Prints help"
}
parse_commandline ()
{
   while test $# -gt 0
   do
      _key="$1"
      case "$_key" in
         --prefix)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_prefix="$2"
            shift
            ;;
         --prefix=*)
            _arg_prefix="${_key##--prefix=}"
            ;;
         --qmake)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_qmake="$2"
            shift
            ;;
         --qmake=*)
            _arg_qmake="${_key##--qmake=}"
            ;;
         -h|--help)
            print_help
            exit 0
            ;;
         -h*)
            print_help
            exit 0
            ;;
         *)
            _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
            ;;
      esac
      shift
   done
}
parse_commandline "$@"

# Initialise bash script - Step 1
if [ ! "${_arg_prefix}" == "" ]; then
  PREFIX=${_arg_prefix}
fi
if [ ! "${_arg_qmake}" == "" ]; then
  QMAKE_PATH=${_arg_qmake}
fi

# Initialise bash script - Step 2
set +e
_extra_args=

# Find Source Directory
SOURCE=${BASH_SOURCE[0]}
while [ -h "$SOURCE" ]; do
  SOURCE_DIR=$(cd -P "$(dirname "$SOURCE")" && pwd)
  SOURCE=$(readlink "$SOURCE")
  [[ $SOURCE != /* ]] && SOURCE="${SOURCE_DIR}/${SOURCE}"
done
SOURCE_DIR=$(cd -P "$(dirname "$SOURCE")" && pwd)

# Find Qt Installation
export QT_SELECT=qt5
if [ -x "${QMAKE_PATH}" ]; then
   QMAKE_PATH=${QMAKE_PATH}
   QT_VERSION=$(${QMAKE_PATH} -query "QT_VERSION")
elif [ -x "$(command -v qmake-qt5)" ]; then
   QMAKE_PATH=$(command -v qmake-qt5)
   QT_VERSION=$(${QMAKE_PATH} -query "QT_VERSION")
elif [ -x "$(command -v qmake)" ]; then
   QMAKE_PATH=$(command -v qmake)
   QT_VERSION=$(${QMAKE_PATH} -query "QT_VERSION")
else
   QMAKE_PATH=$(find /usr/ -executable -name qmake -type f 2> /dev/null | sed -n 1p)
   if [ "${QMAKE_PATH}" == "" ]; then
      echo "Qt qmake not found"
      exit 1
   fi
   QT_VERSION=$(${QMAKE_PATH} -query "QT_VERSION")
fi
echo "Found Qt ${QT_VERSION} with qmake located at ${QMAKE_PATH}" 

# Find Make Installation
if [ -x "${MAKE_PATH}" ]; then
   MAKE_PATH=${MAKE_PATH}
elif [ -x "$(command -v make)" ]; then
   MAKE_PATH=$(command -v make)
else
   MAKE_PATH=$(find /usr/ -executable -name make -type f 2> /dev/null | sed -n 1p)
   if [ "${MAKE_PATH}" == "" ]; then
      echo "Make not found"
      exit 1
   fi
fi

# Clean Makefile
if [ ! "${RUN_MAKE_CLEAN}" == "NO" ]; then
   if [ -f "Makefile" ]; then
      echo "${MAKE_PATH} distclean"
      ${MAKE_PATH} distclean
   fi
fi

# Set Prefix
if [ ! "${PREFIX}" == "" ]; then
   _extra_args="${_extra_args} GTA5SYNC_PREFIX=${PREFIX}"
fi

# Generating Makefile
echo "${QMAKE_PATH}${_extra_args} ${SOURCE_DIR}/gta5view.pro"
${QMAKE_PATH}${_extra_args} ${SOURCE_DIR}/gta5view.pro

# Make dependencies
if [ ! "${RUN_MAKE_DEPEND}" == "NO" ]; then
   echo "${MAKE_PATH} depend"
   ${MAKE_PATH} depend
fi
exit 0