#!/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: Zoltán Balogh <zoltan.balogh@canonical.com>
#         Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>

PROJECT_PATH=$1
if [[ -z ${PROJECT_PATH=} ]]; then
	PROJECT_PATH=./
fi

if [[ -d debian ]]; then
	OLD_DEPENDS=`grep -m 1 "^Depends:" debian/control -A\`echo \\\`cat debian/control |egrep "^Description:|^Depends:" -in|sed "s/:.*//g"|sort -r|tail -2|tr '\n' '-'\\\`1|bc\`|sed "s/[ ]*//g;s/,/\n/g;s/Depends://g"|sed "/^$/d"|egrep -v "qmlscene|qt-components-ubuntu|misc:Depends"`
fi 

QT_INSTALL_QML=`qmake -query QT_INSTALL_QML`
REGEXP_FOR_QML_MODULES=`find $PROJECT_PATH -name "*.qml"|xargs egrep "^[ \t]*import"|egrep -v "\""|sed -e 's/[ \t]*import/import/'|awk '{print $2 "\.*[0-9]*\/*[qmldir]*$|"}'|sort|uniq|tr -d '\n'|sed "s/|$//"`
DEPS_BY_PLUGINS=`find $QT_INSTALL_QML -name "qmldir"|xargs egrep -l "$REGEXP_FOR_QML_MODULES"|xargs  dpkg -S|awk '{print $1}'| sort|uniq |sed "s/:.*[:,]//g"`
DEPS_BY_QML_MODULES=`find $QT_INSTALL_QML -name "qmldir"|egrep "$REGEXP_FOR_QML_MODULES"|xargs dpkg -S|grep $QT_INSTALL_QML|awk '{print $1}'| sort|uniq |sed "s/:.*[:,]//g"`
echo -n Depends: \${misc:Depends},
echo -n $DEPS_BY_PLUGINS $DEPS_BY_QML_MODULES $OLD_DEPENDS|tr ' ' '\n'|sort|uniq|tr '\n' ','|sed "s/qtdeclarative5-ubuntu-ui-toolkit-plugin/qtdeclarative5-ubuntu-ui-toolkit-plugin|qt-components-ubuntu/"|sed "s/:,/,/g"

