#!/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: David Barth <david.barth@canonical.com>

SCRIPTPATH=`dirname $0`

CORDOVA_PACKAGE="ubuntu-html5-platform-3.4-dev"
PAYLOAD_DIR="usr/share/ubuntu-html5-platform-3.4/"
CORDOVA_CACHE=${HOME}/.cache/ubuntu-html5-platform
DOWNLOAD_DIR=${CORDOVA_CACHE}/download

# based on the blog post of Andreas Olsson
# http://blog.bogosity.se/2012/11/03/fully-using-apt-get-download/
setup_apt_conf () {
    mkdir -p ${DOWNLOAD_DIR}/apt/{apt.conf.d,preferences.d,trusted.gpg.d,cache/lists/partial}
    touch ${DOWNLOAD_DIR}/apt/status
    ln -s /usr/share/keyrings/debian-archive-keyring.gpg \
        ${DOWNLOAD_DIR}/apt/trusted.gpg.d/
    ln -s /usr/share/keyrings/ubuntu-archive-keyring.gpg \
        ${DOWNLOAD_DIR}/apt/trusted.gpg.d/
    apt-key --keyring ${DOWNLOAD_DIR}/apt/trusted.gpg.d/ppa.gpg adv \
        --keyserver keyserver.ubuntu.com --recv-keys C7122F9B 1>/dev/null
    echo

    cat > ${DOWNLOAD_DIR}/apt/downloader.conf <<EOF
Dir::Cache "${DOWNLOAD_DIR}/apt/cache";
Dir::Etc "${DOWNLOAD_DIR}/apt";
Dir::State::Lists "${DOWNLOAD_DIR}/apt/cache/lists";
Dir::State::status "${DOWNLOAD_DIR}/apt/status";
Acquire::Languages "none";
EOF
}

setup_apt_conf_for_release () {
    RELEASE=$1
    ARCH=$2

    [ -z "$RELEASE" ] && exit "setup_apt_conf_for_release"

    # echo "Apt::Architecture \"${ARCH}\";" >> \
    #    ${DOWNLOAD_DIR}/apt/downloader.conf

    if [ "$ARCH" == "armhf" ]; then
        cat > ${DOWNLOAD_DIR}/apt/sources.list <<EOF
# Do not use the archive for now
# deb http://ports.ubuntu.com/ubuntu-ports/ ${RELEASE} main restricted universe
# deb http://ports.ubuntu.com/ubuntu-ports/ ${RELEASE}-updates main restricted universe
# deb http://ports.ubuntu.com/ubuntu-ports/ ${RELEASE}-security main restricted universe

# use the SDK PPA instead
deb http://ppa.launchpad.net/ubuntu-sdk-team/ppa/ubuntu ${RELEASE} main
EOF
    else
        cat > ${DOWNLOAD_DIR}/apt/sources.list <<EOF
# Do not use the archive for now
# deb http://archive.ubuntu.com/ubuntu ${RELEASE} main restricted universe
# deb http://archive.ubuntu.com/ubuntu ${RELEASE}-updates main restricted universe
# deb http://security.ubuntu.com/ubuntu ${RELEASE}-security main restricted universe
# deb http://ppa.launchpad.net/ubuntu-sdk-team/ppa/ubuntu ${RELEASE} main

# use the SDK PPA instead
deb http://ppa.launchpad.net/ubuntu-sdk-team/ppa/ubuntu ${RELEASE} main
EOF
    fi

    APT_CONFIG=${DOWNLOAD_DIR}/apt/downloader.conf apt-get -qq update
}

download_arch () {
    ARCH2=$1
    ARCH3=$2
    RELEASE=$3

    [ -z "$ARCH2" -o -z "$ARCH3" ] && exit "download_arch"
    LOCAL_ARCH=`apt-config dump | grep "APT::Architecture " | cut -d\" -f2`

    echo "Downloading runtime platform files for $ARCH2"
    if [ -z "$RELEASE" -o "$RELEASE" == "`lsb_release -c | cut -f 2`" ]; then
        RELEASE=`lsb_release -c | cut -f 2`
    else
        echo "(specifically from release $RELEASE)"
    fi

    setup_apt_conf_for_release $RELEASE # $ARCH2
    URI=`APT_CONFIG=${DOWNLOAD_DIR}/apt/downloader.conf apt-get -qq download --print-uris ${CORDOVA_PACKAGE} | grep ^\' | cut -d\' -f2`
    PACKAGE_FILENAME=`APT_CONFIG=${DOWNLOAD_DIR}/apt/downloader.conf apt-get -qq download --print-uris ${CORDOVA_PACKAGE} | grep ^\' | cut -d\  -f2 | sed "s/_${LOCAL_ARCH}.deb$/_${ARCH2}.deb/"`

    if [ $? -eq 1 ]; then
        exit "$CORDOVA_PACKAGE not found ($URI)"
    fi

    echo $PACKAGE_FILENAME
    
    mkdir -p ${CORDOVA_CACHE}/lib/${ARCH3}

    (
        cd /tmp
        wget -q `echo $URI | sed "s/_${LOCAL_ARCH}.deb$/_${ARCH2}.deb/"`

	    # plain extraction since the packages are already configured w/ arch specific install paths
        dpkg-deb -x ${PACKAGE_FILENAME} ${CORDOVA_CACHE}/download

        rm -f ${PACKAGE_FILENAME}
    )

    echo
}

install_arch () {
    ARCH3=$1

    [ -z "$ARCH3" ] && exit "install_arch"

    mkdir -p ${CORDOVA_CACHE}/lib/${ARCH3}
    mkdir -p ${CORDOVA_CACHE}/www/cordova

    rm -rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/cordova-ubuntu

    cp -Rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/CordovaUbuntu.?.? ${CORDOVA_CACHE}/lib/${ARCH3}
    cp -Rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/qml ${CORDOVA_CACHE}
    cp -Rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/www/plugins ${CORDOVA_CACHE}/www/cordova
    cp -Rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/www/cordova.js ${CORDOVA_CACHE}/www/cordova
    cp -Rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/www/cordova_plugins.js ${CORDOVA_CACHE}/www/cordova

    cp ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}/www/libcoreplugins.so ${CORDOVA_CACHE}/www/libcoreplugins-${ARCH3}.so

    rm -rf ${CORDOVA_CACHE}/download/${PAYLOAD_DIR}/${ARCH3}
}

copy_to_project () {

    if [ -d ${PROJECTPATH}/www ]; then
        echo "Copying runtime platform files to ${PROJECTPATH}"

        cp -Rf ${CORDOVA_CACHE}/lib ${PROJECTPATH}
        cp -Rf ${CORDOVA_CACHE}/qml ${PROJECTPATH}
        cp -Rf ${CORDOVA_CACHE}/www ${PROJECTPATH}

        # find ${PROJECTPATH}/www
    else
        echo "No www/ subfolder in ${PROJECTPATH} to install the runtime"
        return 1
    fi
}


SKIP_DOWNLOAD=0

if [ "$1" == "-h" -o "$1" == "--help" ]; then
    echo "Usage: $SCRIPTPATH [-n] [project_path]"
    exit
fi

if [ "$1" == "-n" ]; then
    SKIP_DOWNLOAD=1
    PROJECTPATH=`pwd`/$2
else
    PROJECTPATH=`pwd`/$1
fi

if [ -x /usr/bin/nm-tool ]; then
    if [ `nm-tool | grep State|cut -f2 -d' '` != "connected" ]; then
        echo "No network link detected, skipping the download step..."
        echo
        SKIP_DOWNLOAD=1
    fi
fi

if [ $SKIP_DOWNLOAD -eq 0 ]; then
    echo
    echo "Trying to download a pre-built Cordova runtime..."
    echo

    rm -rf ${CORDOVA_CACHE}

    setup_apt_conf

    download_arch "i386" "i386-linux-gnu"
    install_arch "i386-linux-gnu"
    download_arch "amd64" "x86_64-linux-gnu"
    install_arch "x86_64-linux-gnu"
    download_arch "armhf" "arm-linux-gnueabihf" trusty
    install_arch "arm-linux-gnueabihf"

    rm -rf ${CORDOVA_CACHE}/download/apt
fi

copy_to_project
