#!/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.."
  DESKTOPFILE_COUNT=`ls *.desktop|wc -l`
  QMLPROJECT=`ls *.qmlproject|sed "s/\.qmlproject//"`
  DESKTOPFILE=""
  if [[ ${DESKTOPFILE_COUNT} -eq 1 ]]; then
  	echo " * found a desktop file."
  	DESKTOPFILE=`ls *.desktop`
  elif [[ ${DESKTOPFILE_COUNT} -eq 0 ]]; then
  	echo " /!\\ no desktop file found /!\\"
  	echo "  You will have to create one."
  	exit
  else
    echo " * multiple desktop files found."

    # do we have a desktop file which has the same name as qmlproject
    if [[ -f ${QMLPROJECT}.desktop ]]; then
    	# found one lets use it
    	echo "   - found a desktop file which matches the qmlproject."
    	DESKTOPFILE=${QMLPROJECT}.desktop
    else
    	# it seems that we did not have one, lets use the latest desktop file.
    	echo "   - lets use the latest desktop file available."
    	DESKTOPFILE=`ls -t *.desktop|head -n1`
    fi
  fi

  echo
  echo "The desktop file is ${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}

  # lets read the manifest description and set that to desktop comment - fix bug #1223388
  #MANIFEST_DESCRIPTION=`${SCRIPTPATH}/manifest_description manifest.json`
  #sed -i "s/^Comment=.*/Comment=${MANIFEST_DESCRIPTION}/g" ${DESKTOPFILE}

  # lets replace the Exec line
  #sed -i "s/^Exec=.*/Exec=qmlscene \$@ ${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 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
