#!/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 .
fi

${SCRIPTPATH}/qtc_project_debuild

QMLDEPS=`${SCRIPTPATH}/qtc_project_detect_qmldeps`

if [[ -d debian ]]; then
  awk "NR < `egrep "^Depends:" -in debian/control|sed "s/:.*//g"|head -1`+1" debian/control|sed "s/^Depends:.*/${QMLDEPS}qmlscene/g" > debian/control.new
  awk "NR >= `egrep "^Description:" -in debian/control|sed "s/:.*//g"|head -1`" 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
set +e
H_FILES=`find . -type f |grep .h$`
CPP_FILES=`find . -type f |grep .cpp$`
PRO_FILES=`find . -type f |grep .pro$`
set -e

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

dpkg-buildpackage -us -uc -nc
