#!/usr/bin/env python
#
# Software License Agreement (BSD)
#
# @author    Mike Purvis <mpurvis@clearpathrobotics.com>
# @copyright (c) 2014, Clearpath Robotics, Inc., All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that
# the following conditions are met:
# * Redistributions of source code must retain the above copyright notice, this list of conditions and the
#   following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
#   following disclaimer in the documentation and/or other materials provided with the distribution.
# * Neither the name of Clearpath Robotics nor the names of its contributors may be used to endorse or
#   promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.

import cookielib
import cStringIO
import logging
import shutil
import subprocess
import sys
import tarfile
import urllib
import urllib2
import os
import glob

logging.basicConfig(level=logging.INFO)

LOGIN_URL = 'https://www.ptgrey.com/login'
LOGIN_DATA = {
    'Email': 'code@clearpathrobotics.com',
    'Password': 'uNjRxoH6NMsJvi6hyPCH'
    }

ARCHS = {
    'i386': (
        'https://www.ptgrey.com/support/downloads/11047/',
        'spinnaker-1.13.0.31-i386',
        'libSpinnaker.so.1.13.0.31'),
    'x86_64': (
        'https://www.ptgrey.com/support/downloads/11048/',
        'spinnaker-1.13.0.31-amd64',
        'libSpinnaker.so.1.13.0.31'),
    'armv7': (
        'https://www.ptgrey.com/support/downloads/11046/',
        'spinnaker-1.13.0.31-armhf',
        'libSpinnaker.so.1.13.0.31'),
    'armv8': (
        'https://www.ptgrey.com/support/downloads/11045/',
        'spinnaker-1.13.0.31-arm64',
        'libSpinnaker.so.1.13.0.31')
    }


archive_url, folder_name, shared_library = ARCHS[sys.argv[1]]
destination_folder = sys.argv[2]


if not os.path.exists(os.path.join(os.getcwd(), "usr/lib/")):
    if not os.path.exists(os.path.join(os.getcwd(), folder_name)):
        logging.info("Logging into ptgrey.com.")
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        opener.addheaders = [
            ('User-agent', 'Mozilla/5.0'),
            ('Referer', 'https://www.ptgrey.com')]
        opener.open(LOGIN_URL, urllib.urlencode(LOGIN_DATA))

        logging.info("Downloading SDK archive.")
        resp = opener.open(archive_url)

        logging.info("Unpacking tarball.")
        with tarfile.open(mode="r:gz", fileobj=cStringIO.StringIO(resp.read())) as tar:
            tar.extractall()

    logging.info("Unpacking debs.")
    debs = glob.glob(os.path.join(os.getcwd(), folder_name, "libspinvideoencoder-*.deb"))
    debs += glob.glob(os.path.join(os.getcwd(), folder_name, "*spinnaker-*.deb"))
    for deb in debs:
        subprocess.call(['dpkg', '-x', deb, os.path.join(os.getcwd(), folder_name)])

    # Fix for 1.13.0.31
    os.remove(os.path.join(os.getcwd(), folder_name, "usr/lib/libspinnaker.so.1"))
    os.symlink(shared_library, os.path.join(os.getcwd(), folder_name, "usr/lib/libSpinnaker.so.1"))

    if not os.path.exists(os.path.join(os.getcwd(), "usr")):
        os.mkdir(os.path.join(os.getcwd(), "usr"))

    # For every folder/file, copy it into the /usr folder we just created.
    for filename in os.listdir(os.path.join(os.getcwd(), folder_name, "usr")):
        if not os.path.exists(os.path.join(os.getcwd(), "usr", filename)):
            shutil.move(os.path.join(os.getcwd(), folder_name, "usr", filename), os.path.join(os.getcwd(), "usr", filename))

    # now we do some thing for XML files; copy them next to the catkin binary
    xml_path = os.path.join(os.getcwd(), "usr/lib/")
    xml_files = os.listdir(xml_path)

    try:
        os.makedirs(destination_folder)
    except:
        logging.info("Error XML folder either exists or can't be created.")

    for files in xml_files:
        if files.endswith(".xml"):
            origin_file = os.path.join(xml_path, files)
            print('Copying ' + origin_file + ' to ' + destination_folder)
            shutil.copy(origin_file, destination_folder)
