#!/bin/bash
# Copyright 2014 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: Benjamin Zeller <benjamin.zeller@canonical.com>

USAGE="Usage: $0 targetArch targetFramework targetSeries projectDir projectName"

if [ $# -lt 3 ]; then
        echo "$USAGE"
        exit 1
fi

ARCH=$1
shift
FRAMEWORK=$1
shift
SERIES=$1
shift
PROJECTDIR=$1
shift
PROJECTNAME=$1
shift

echo "Building package for ${PROJECTNAME}"

SCRIPTPATH=$(dirname $0)
BUILDDIR="${FRAMEWORK}-${ARCH}"
INSTALLDIR="click_package"

#make sure the project was already built, if not build it
if [ ! -d ${BUILDDIR} ]
then
    mkdir ${BUILDDIR}
fi

cd ${BUILDDIR}

#make sure the binaries are up to date and installed in our click directory
set -e
${SCRIPTPATH}/qtc_chroot_cmake ${ARCH} ${FRAMEWORK} ${SERIES} ${PROJECTDIR}
${SCRIPTPATH}/qtc_fixmoc ${ARCH} ${FRAMEWORK} ${SERIES}
${SCRIPTPATH}/qtc_chroot_make ${ARCH} ${FRAMEWORK} ${SERIES}

#cleanup the install dir
rm -rf ${INSTALLDIR}

#install the click package tree
${SCRIPTPATH}/qtc_chroot_make ${ARCH} ${FRAMEWORK} ${SERIES} DESTDIR=${INSTALLDIR} install

#make sure the manifest file contains our arch
sed -i 's/"architecture".*,/"architecture": "'${ARCH}'",/g' ${INSTALLDIR}/manifest.json
set +e

pushd .
cd ${INSTALLDIR}

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

echo "Found a desktopfile: ${DESKTOPFILE}"

${SCRIPTPATH}/qtc_fix_desktop_comment ${DESKTOPFILE}
popd

#create the package
click build ${INSTALLDIR} > /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 $(pwd)/CLICKPACKAGE`
    echo "$PACKAGENAME"
else
    echo
    echo "There was some failure when creating the package."
    echo "See /tmp/click.err for details."
    echo

    cd ..
    exit 1
fi

#back into working dir
cd ..
