#!/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: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
# Author: Benjamin Zeller <benjamin.zeller@canonical.com>

set -e

. $(dirname $0)/functions.inc

PACKAGENAME=$2
TARGET_DEVICE=$3
TARGET_DEVICE_PORT=$4
TARGET_DEVICE_HOME=$5
TARGET_DEVICE_USERNAME=$6

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

if [[ -z ${TARGET_DEVICE_PORT} ]]; then
    TARGET_DEVICE_PORT=2222
fi

if [[ -z ${TARGET_DEVICE_USERNAME} ]]; then
    TARGET_DEVICE_USERNAME=phablet
fi

if [[ -z ${TARGET_DEVICE} ]]; then
    TARGET_DEVICE=${TARGET_DEVICE_USERNAME}@127.0.0.1
fi

if [[ -z ${TARGET_DEVICE_HOME} ]]; then
    TARGET_DEVICE_HOME=/home/${TARGET_DEVICE_USERNAME}/dev_tmp/
fi



SCP="scp -i ${SSHIDENTITY} -o LogLevel=quiet -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P${TARGET_DEVICE_PORT}"
SSH="ssh -i ${SSHIDENTITY} -o LogLevel=quiet -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p${TARGET_DEVICE_PORT} ${TARGET_DEVICE}"


# remove old files
if [[ ! -z ${TARGET_DEVICE_HOME} ]]; then
        echo "Lets clear the device tmp folder at ${TARGET_DEVICE}:${TARGET_DEVICE_PORT}${TARGET_DEVICE_HOME}"
        $SSH rm -rf ${TARGET_DEVICE_HOME}/* 
fi

# make sure that the device has the target directory
echo "Lets create the device tmp folder to ${TARGET_DEVICE}:${TARGET_DEVICE_PORT}${TARGET_DEVICE_HOME}"
$SSH mkdir -vp ${TARGET_DEVICE_HOME} 

# transfer click package to device
PCK="$(pwd)/${PACKAGENAME}"
echo "Transfer the click package ${PCK} to the device"
$SCP ${PACKAGENAME} ${TARGET_DEVICE}:${TARGET_DEVICE_HOME} 
if [[ ${?} -eq 0 ]]; then
        echo "..transfer complete!"
else
        echo " /!\\ transfer failed /!\\"
        exit -1
fi

echo "List all available click packages on the device"
FILES=`$SSH -q "cd ${TARGET_DEVICE_HOME}; ls -1 *.click"`
echo $FILES
for FILE in $FILES;
do
 echo "Installing $FILE to device.."
 # note adb shell and adb_shell are two different things, adb_shell comes from functions.inc
 LOG="$(mktemp)"
 adb_shell sudo -H -u ${TARGET_DEVICE_USERNAME} pkcon -p install-local --allow-untrusted ${TARGET_DEVICE_HOME}/${FILE} 2>&1 | tee $LOG
 # adb shell does not return the exit code of what was run which is horrible
 # (https://code.google.com/p/android/issues/detail?id=3254)
 if grep "^Fatal error:" $LOG; then
     echo "pkcon failed"
     rm -f $LOG
     exit 1;
 fi
 rm -rf $LOG
done
echo "All files installed succesfully"
