#!/bin/sh
#############################################################################
##
## Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
##
## This file is part of the Qt Enterprise Embedded Scripts of the Qt
## framework.
##
## $QT_BEGIN_LICENSE$
## Commercial License Usage Only
## Licensees holding valid commercial Qt license agreements with Digia
## with an appropriate addendum covering the Qt Enterprise Embedded Scripts,
## may use this file in accordance with the terms contained in said license
## agreement.
##
## For further information use the contact form at
## http://www.qt.io/contact-us.
##
##
## $QT_END_LICENSE$
##
#############################################################################

set -ex

while test -n "$1"; do
  case "$1" in
    "--help" | "-h")
      echo "Usage: $0 [--remove]"
      exit 0
      shift
      ;;
    "--remove")
      REMOVEONLY=1
      shift
      ;;
    *)
      CONFIG=$1
      shift
      ;;
  esac
done


####HARDCODE
MACHINE=nitrogen6x
WORKDIR=/home/aavit/workdir-yocto/sdk-nitrogen6
RELEASE=Yocto_5.4.1
PLATFORM=eLinux
COMPILER=arm-poky-linux-gnueabi
SYSROOT=cortexa9hf-vfp-neon-poky-linux-gnueabi
SDKTOOL=/home/aavit/Qt/Tools/QtCreator/bin/sdktool
#####



# default SDKTOOL "${DIR}/../../../../Tools/QtCreator/bin/sdktool"
if [ ! -x ${SDKTOOL} ]; then
    echo "Cannot find 'sdktool'"
    exit 1
fi
ABI="arm-linux-generic-elf-32bit"

BASEID="byos.${RELEASE}.${MACHINE}"

TOOLCHAINNAME="GCC (Custom Boot to Qt ${RELEASE} ${PLATFORM})"
QTNAME="Custom Boot to Qt ${RELEASE} ${PLATFORM} ${MACHINE}"
KITNAME="Custom Boot to Qt ${RELEASE} ${PLATFORM} ${MACHINE} Kit"

${SDKTOOL} rmKit --id ${BASEID}.kit 2>/dev/null || true
${SDKTOOL} rmQt --id ${BASEID}.qt || true
${SDKTOOL} rmTC --id ProjectExplorer.ToolChain.Gcc:${BASEID}.tc || true

if [ -n "${REMOVEONLY}" ]; then
    echo "Kit removed: ${KITNAME}"
    exit 0
fi

${SDKTOOL} addTC \
    --id "ProjectExplorer.ToolChain.Gcc:${BASEID}.tc" \
    --name "${TOOLCHAINNAME}" \
    --path "${WORKDIR}/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/${COMPILER}/${COMPILER}-g++" \
    --abi "${ABI}"

${SDKTOOL} addQt \
    --id "${BASEID}.qt" \
    --name "${QTNAME}" \
    --type "Boot2Qt.QtVersionType" \
    --qmake "${WORKDIR}/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/qt5/qmake"

${SDKTOOL} addKit \
    --id "${BASEID}.kit" \
    --name "${KITNAME}" \
    --qt "${BASEID}.qt" \
    --debuggerengine "1" \
    --debugger "${WORKDIR}/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/${COMPILER}/${COMPILER}-gdb" \
    --sysroot "${WORKDIR}/toolchain/sysroots/${SYSROOT}" \
    --devicetype "Boot2Qt.HwDevice" \
    --toolchain "ProjectExplorer.ToolChain.Gcc:${BASEID}.tc" \
    --icon ":/boot2qt/images/B2Qt_QtC_icon.png"


echo "Configured Qt Creator with new kit:"
echo "    ${KITNAME}"
