#!/bin/bash
# Copyright (C) 2013 Tomasz Olszak <olszak.tomasz@gmail.com>
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:

# 1. Redistributions of source code must retain the copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products 
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


function usage {
  echo "This script will create tpk tree, manifest file, CommandLineBuild, build_data"
  echo "Also it will copy needed Qt libraries and plugins into proper directories:"
  echo "Qt libraries directory will be to to lib directory"
  echo "Qt plugins and qml directory will be copied to data directory"
  echo "Application.exe binary will be copied to CommandLineBuild directory"
  echo "After successful execution of $0 you will be asked to run native-packaging"
  echo "tool from Tizen SDK. It will build and sign Tizen tpk from prepared binaries."
  echo "prepare_tpk Qt5INSTALLDIR application/binary/path/executable.exe icon/path/iconname.png ApplicationID [ARCH=armel]"
  exit;
}

if [ $# -lt 4 ]; then
  usage
fi

type chrpath &> /dev/null

if [ "$?" != "0" ]; then
    echo "chrpath is needed for bundling Qt libraries with your application - install chrpath from you package manager"
    exit 1
fi



MANIFEST='<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n
<Manifest xmlns="http://schemas.tizen.org/2012/12/manifest">\n
    <Id>__REPLACE_APP_ID__</Id>\n
    <Version>1.0.0</Version>\n
    <Type>C++App</Type>\n
    <Requirements>\n
        <Feature Name="http://tizen.org/feature/platform.core.cpu.arch.armv7">true</Feature>\n
        <Feature Name="http://tizen.org/feature/platform.core.fpu.arch.vfpv3">true</Feature>\n
    </Requirements>\n
    <Apps>\n
        <ApiVersion>2.1</ApiVersion>\n
        <Privileges/>\n
        <UiApp LaunchingHistoryVisible="True" Main="True" MenuIconVisible="True" Name="__REPLACE_BINARY_NAME__">\n
            <UiScalability BaseScreenSize="Normal" CoordinateSystem="Logical" LogicalCoordinate="720"/>\n
            <UiTheme SystemTheme="White"/>\n
            <DisplayNames>\n
                <DisplayName Locale="eng-GB">__REPLACE_BINARY_NAME__</DisplayName>\n
            </DisplayNames>\n
            <Icons>\n
                <Icon Section="MainMenu">__REPLACE_ICON_NAME__</Icon>\n
            </Icons>\n
            <LaunchConditions/>\n
        </UiApp>\n
    </Apps>\n
</Manifest>\n
'



QTDIR=$1/
BINARY=$2
BINARY_NAME=$(basename $BINARY)
BINARY_NAME=$(echo -n $BINARY_NAME | sed s/\.exe//)
ICON=$3
ICON_NAME=$(basename $ICON)
APP_ID=$4
ARCH=armel

if [ "$5" != "" ]; then
  ARCH=$5
fi

BUILD_DATA="PLATFORM_VER:Tizen 2.2\nARCHITECTURE:$ARCH\nTOOLCHAIN:GCC-4.5\nTYPE:app"

mkdir -p bin CommandLineBuild  data  info  lib res setting  shared/data shared/res/screen-density-xhigh shared/trusted

cp $BINARY CommandLineBuild
cp $ICON shared/res/screen-density-xhigh

echo -e $MANIFEST | sed "s/__REPLACE_APP_ID__/$APP_ID/" | sed "s/__REPLACE_BINARY_NAME__/$BINARY_NAME/" | sed "s/__REPLACE_ICON_NAME__/$ICON_NAME/" > manifest.xml
echo -e $BUILD_DATA > CommandLineBuild/build_data

cp -r $QTDIR/plugins $QTDIR/qml data/
cp $QTDIR/lib/libQt5Core.so.5  $QTDIR/lib/libQt5DBus.so.5  $QTDIR/lib/libQt5Gui.so.5  $QTDIR/lib/libQt5Network.so.5  $QTDIR/lib/libQt5Qml.so.5  $QTDIR/lib/libQt5Quick.so.5  $QTDIR/lib/libQt5V8.so.5 $QTDIR/lib/libQt5Sensors.so.5 lib/

for file in `find . -name "*"`; do
    chrpath -l $file &> /dev/null
    if [ "$?" == "0" ]; then
#if 
set -e
	chrpath --replace /opt/usr/apps/$APP_ID/lib $file 1> /dev/null
set +e
    fi
done

echo build tree prepared. 
echo now invoke in CommandLineBuild:
echo native-packaging  --sign-author-key /your/path/to/certificate/some_cert.p12 --sign-author-pwd somepwd


