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

DEBEMAIL=`grep -G "^DEBEMAIL" ~/.bashrc`
DEBFULLNAME=`grep -G "^DEBFULLNAME" ~/.bashrc`

if [[ ! -z "$DEBEMAIL" ]]; then
  export $DEBEMAIL
else
  CMD=`grep -G "export DEBEMAIL=" ~/.bashrc|sed "s/\"//g"`
  if [[ ! -z $CMD ]]; then
    $CMD
  fi
fi
if [[ ! -z $DEBFULLNAME ]]; then
  export $DEBFULLNAME
else
  CMD=`grep -G "export DEBFULLNAME=" ~/.bashrc|sed "s/\"//g"|sed "s/export DEBFULLNAME=//g"`
  if [[ ! -z $CMD ]]; then
    export DEBFULLNAME="$CMD"
  fi
fi

set -e

PROJECT=$1

if [[ ! -d "$PROJECT" ]]; then
  echo "Project not found ($PROJECT)."
  exit 123
fi

cd $PROJECT
if [[ -d debian ]]; then
  echo "Packaging already exists for project. Skipping.."
  exit
fi

PACKAGENAME=`basename $PWD|tr '[A-Z]' '[a-z]'`

set +e
#detect project type
H_FILES=`find . -type f |grep .h$`
CPP_FILES=`find . -type f |grep .cpp$`
PRO_FILES=`find . -type f |grep .pro$`

PROJECTTYPE=""
PACKAGETYPE=""
if [[ -z ${H_FILES} && -z ${CPP_FILES} && -z ${PRO_FILES} ]]; then
  echo "Detected: QML project"
  PROJECTTYPE="QML"
  PACKAGETYPE="-s"
else
  echo "Detected CPP project"
  PROJECTTYPE="CPP"
  IS_LIB=`echo ${PRO_FILES}|xargs cat |grep "TEMPLATE"|grep "lib"`
  if [[ -z ${IS_LIB} ]]; then
    echo " - Application"
    PACKAGETYPE="-s"
  else
    echo " - QML Extension Library"
    PACKAGETYPE="-s"
  fi
fi

set -e

# create packaging

echo "\n" | dh_make -n -p ${PACKAGENAME}_0.1 ${PACKAGETYPE} --createorig

if [[ ! -d debian ]]; then
  echo "Debian folder was not created!"
  exit 2
fi

pushd debian
# remove unfilled files
rm *.ex *.EX README* docs
popd

# add installable files
INSTALL_PATH=/usr/share/${PACKAGENAME}
DESKTOPFILE=/usr/share/applications
find . -mindepth 1 -maxdepth 3 -printf "%p ${INSTALL_PATH}\n"|grep -v ".user\|debian\|.desktop" | sed "s/^\.\///g" > debian/install

if [[ -z ${IS_LIB} ]]; then
find *.desktop -printf "%p ${DESKTOPFILE}\n" >> debian/install
fi

# check if we have .pro files or cpp/h files, if not then change architecture to any
if [[ "$PROJECTTYPE" == QML ]]; then
  echo "Detected: QML project"
  sed -i "s/^Architecture: any$/Architecture: all/g" debian/control
  # update package dependencies
  SCRIPTPATH=`dirname $0`
  QMLDEPS=`${SCRIPTPATH}/qtc_project_detect_qmldeps`
  sed -i "s/^Depends:.*/${QMLDEPS}qmlscene/g" debian/control
else
  echo "Detected: CPP project"
fi

UBUNTU_RELEASE=`cat /etc/lsb-release|grep DISTRIB_CODENAME|sed 's/DISTRIB_CODENAME=//'`
sed -i "s/unstable; urgency=low/{$UBUNTU_RELEASE}; urgency=low/g" debian/changelog
