#!/usr/bin/env 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=
_arg_telemetry_authid=
_arg_telemetry_authpw=
_arg_telemetry_pushurl=
_arg_telemetry_regurl=
_arg_telemetry_weburl=
print_help ()
{
   printf '%s\n' "gta5view Configure Script"
   printf 'Usage: %s [--prefix <arg>] [--qmake <arg>] [--telemetry-authid <arg>] [--telemetry-authpw <arg>] [--telemetry-pushurl <arg>] [--telemetry-regurl <arg>] [--telemetry-weburl <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=}"
            ;;
         --telemetry-authid)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_telemetry_authid="$2"
            shift
            ;;
         --telemetry-authid=*)
            _arg_telemetry_authid="${_key##--telemetry-authid=}"
            ;;
         --telemetry-authpw)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_telemetry_authpw="$2"
            shift
            ;;
         --telemetry-authpw=*)
            _arg_telemetry_authpw="${_key##--telemetry-authpw=}"
            ;;
         --telemetry-pushurl)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_telemetry_pushurl="$2"
            shift
            ;;
         --telemetry-pushurl=*)
            _arg_telemetry_pushurl="${_key##--telemetry-pushurl=}"
            ;;
         --telemetry-regurl)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_telemetry_regurl="$2"
            shift
            ;;
         --telemetry-regurl=*)
            _arg_telemetry_regurl="${_key##--telemetry-regurl=}"
            ;;
         --telemetry-weburl)
            test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
            _arg_telemetry_weburl="$2"
            shift
            ;;
         --telemetry-weburl=*)
            _arg_telemetry_weburl="${_key##--telemetry-weburl=}"
            ;;
         -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
if [ "${_arg_telemetry_authid}" != "" ] && [ "${_arg_telemetry_authpw}" != "" ] && [ "${_arg_telemetry_pushurl}" != "" ] && [ "${_arg_telemetry_regurl}" != "" ]; then
   _telemetry_args="${_telemetry_args} DEFINES+=GTA5SYNC_TELEMETRY"
   _telemetry_args="${_telemetry_args} DEFINES+=GTA5SYNC_TELEMETRY_AUTHID=\\\\\\\"${_arg_telemetry_authid}\\\\\\\""
   _telemetry_args="${_telemetry_args} DEFINES+=GTA5SYNC_TELEMETRY_AUTHPW=\\\\\\\"${_arg_telemetry_authpw}\\\\\\\""
   _telemetry_args="${_telemetry_args} DEFINES+=GTA5SYNC_TELEMETRY_PUSHURL=\\\\\\\"${_arg_telemetry_pushurl}\\\\\\\""
   _telemetry_args="${_telemetry_args} DEFINES+=GTA5SYNC_TELEMETRY_REGURL=\\\\\\\"${_arg_telemetry_regurl}\\\\\\\""
   if [ "${_arg_telemetry_weburl}" != "" ]; then
      _telemetry_args="${_telemetry_args} DEFINES+=GTA5SYNC_TELEMETRY_WEBURL=\\\\\\\"${_arg_telemetry_weburl}\\\\\\\""
   fi
else
   _telemetry_args=
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 qConf
if [ "${NO_QCONF}" != "YES" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_QCONF"
fi

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

# Set Build Type
if [ "${BUILDTYPE}" == "Alpha" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_BUILDTYPE_ALPHA"
elif [ "${BUILDTYPE}" == "Beta" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_BUILDTYPE_BETA"
elif [ "${BUILDTYPE}" == "Developer" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_BUILDTYPE_DEV"
elif [ "${BUILDTYPE}" == "Daily Build" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_BUILDTYPE_DAILY"
elif [ "${BUILDTYPE}" == "Release Candidate" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_BUILDTYPE_RC"
elif [ "${BUILDTYPE}" == "Release" ]; then
   _extra_args="${_extra_args} DEFINES+=GTA5SYNC_BUILDTYPE_REL"
fi

# Add Telemetry Args
if [ "${_telemetry_args}" != "" ]; then
   _extra_args="${_extra_args}${_telemetry_args}"
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}" == "YES" ]; then
   echo "${MAKE_PATH} depend"
   ${MAKE_PATH} depend
fi
exit 0
