#!/bin/bash
# Copyright 2013 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; version 2.1.
#
# 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
set -e

SCRIPTPATH=`dirname $0`
# -- actions --
if [[ ! -d debian ]]; then
  ${SCRIPTPATH}/qtc_project_packaging .
  ${SCRIPTPATH}/qtc_project_debuild
fi
QMLDEPS=`${SCRIPTPATH}/qtc_project_detect_qmldeps`

if [[ -d debian ]]; then
  awk "NR < `egrep "^Depends:" -in debian/control|sed "s/:.*//g"`+1" debian/control > debian/control.new
  awk "NR >= `egrep "^Description:" -in debian/control|sed "s/:.*//g"`" debian/control >> debian/control.new
  mv debian/control.new debian/control
fi
sed -i "s/^Depends:.*/${QMLDEPS}qmlscene/g" debian/control


# Detect if we have QML project only
H_FILES=`find . -name *.h`
CPP_FILES=`find . -name *.cpp`

if [[ -z ${H_FILES} && -z ${CPP_FILES} ]]; then
  echo "Detected: QML project"
  sed -i "s/^Architecture: any$/Architecture: all/g" debian/control
else
  echo "Detected: Hybrid project"
fi

dpkg-buildpackage -us -uc -nc
