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

SCRIPTPATH=`dirname $0`

PROJECTPATH=$1

if [[ -z ${PROJECTPATH} ]]; then
  PROJECTPATH=`pwd`
fi

echo
echo "Click packaging project in ${PROJECTPATH}."
echo

pushd ${PROJECTPATH} >> /dev/null
  echo
  echo "Searching for desktop file.."
  QMLPROJECT=`ls *.qmlproject|sed "s/\.qmlproject//"`

  DESKTOPFILE=$( ${SCRIPTPATH}/qtc_find_desktopfile ${QMLPROJECT} )
  if [[ ! $? -eq 0 ]]
  then
    echo " /!\\ no desktop file found /!\\"
    echo "  You will have to create one."
    exit 1
  fi

  echo "Found a desktopfile: ${DESKTOPFILE}"

  # lets check if the manifest.json exists
  if [[ -f manifest.json ]]; then
  	echo
  	echo "Found manifest.json."
  else
  	echo
  	echo " /!\\ manifest.json does not exist /!\\"
  	echo "  You will have to create one using the Packaging tab."
  	exit -1
  fi

  # determine MAINQML
 
  MAINQML=`cat ${QMLPROJECT}.qmlproject|grep mainFile|sed "s/.*\(\".*\"\).*/\1/"|sed "s/\"//g"`
  echo "Main file is ${MAINQML}"

  #------------------

  # lets copy the original desktop file
  cp ${DESKTOPFILE} /tmp/${DESKTOPFILE}

  #fix the comment in desktop file
  ${SCRIPTPATH}/qtc_fix_desktop_comment ${DESKTOPFILE}

  # read the arguments to qmlscene
  QMLSCENE_ARGS=`cat ${DESKTOPFILE} | grep -o "qmlscene.*\$\@" | sed "s/\(qmlscene\|\$\@\)//g"`

  # lets replace the Exec line
  sed -i "s/^Exec=[ a-z0-9A-Z\/]*qmlscene.*/Exec=qmlscene ${QMLSCENE_ARGS} \$@ ${MAINQML}/g" ${DESKTOPFILE}

  # lets build the package
  cd ..

  # copy project files to build directory

  RESULTPATH=`pwd`
  BUILDPATH=${PROJECTPATH}_build
  rm -Rf ${BUILDPATH}
  mkdir -p ${BUILDPATH}

  # read .excludes file from the projectpath
  PROJECT_EXCLUDES="--exclude .bzr --exclude .git --exclude .hg --exclude .svn --exclude *.qmlproject --exclude *.user --exclude tests --exclude Makefile --exclude .excludes"
  if [[ -f ${PROJECTPATH}/.excludes ]]; then
    for EXCLUDE in `cat ${PROJECTPATH}/.excludes`; do
    PROJECT_EXCLUDES+=" --exclude ${EXCLUDE}"
    done
  fi

  # we are printing also a filelist to the stdout which files are going to be inside the package
  echo
  rsync -avh ${PROJECT_EXCLUDES} ${PROJECTPATH}/* ${BUILDPATH}/

  # build the build path
  click build ${BUILDPATH} > /tmp/click.log 2> /tmp/click.err

  # lets check the results
  if [[ ${?} -eq 0 ]]; then
  	echo
  	echo "Package has been created to"
  	PACKAGENAME=`cat /tmp/click.log|sed "s/.*\('.*'\).*/\1/" | sed "s/'//g" | sed "s/.\///" | xargs -ICLICKPACKAGE echo ${RESULTPATH}/CLICKPACKAGE`
  	echo "$PACKAGENAME"
  else
  	echo
  	echo "There was some failure when creating the package."
  	echo "See /tmp/click.err for details."
  	echo
  fi
  cd - > /dev/null

  # restore the original desktop file
  mv /tmp/${DESKTOPFILE} ${DESKTOPFILE}
popd > /dev/null
