#!/bin/bash

TARBALL_NAME='pcl'

PCP_DIR="`rospack find pcl`/../"
VERSION="`cat ${PCP_DIR}/CMakeLists.txt | grep rosbuild_make_distribution | tr -d '_a-zA-Z()'`"

MSG_HEADERS_DIR='/include/pcl/msg'
EXTRA_MSGS_DIR="`rospack find sensor_msgs`/msg_gen/cpp/include/sensor_msgs"

function copy_files
{
    SOURCE="${1}"
    DEST="${2}"
    cp -r "${1}" "${2}"
}

function copy_all_files
{
    DEST="${1}"
    # Copy source
    mkdir "${DEST}/src"
    #cp -r src/* "${DEST}/src/"
    rsync -aC --exclude '*.py*' src/* "${DEST}/src/"
    # Copy headers
    mkdir "${DEST}/include"
    #cp -r include/* "${DEST}/include/"
    rsync -aC --exclude '*.py*' include/* "${DEST}/include/"
    mkdir -p "${DEST}/include/sensor_msgs"
    cp msg/headers/PointField.h "${DEST}/include/sensor_msgs"
    cp msg/headers/PointCloud2.h "${DEST}/include/sensor_msgs"
    cp msg/headers/Image.h "${DEST}/include/sensor_msgs"
    cp msg/headers/RegionOfInterest.h "${DEST}/include/sensor_msgs"
    cp msg/headers/image_encodings.h "${DEST}/include/sensor_msgs"
    mkdir -p "${DEST}/include/roslib"
    cp msg/headers/Header.h "${DEST}/include/roslib"
    mkdir -p "${DEST}/include/pcl"
    cp msg/headers/ModelCoefficients.h "${DEST}/include/pcl"
    cp msg/headers/CameraInfo.h "${DEST}/include/pcl"
    cp msg/headers/PointIndices.h "${DEST}/include/pcl"
    cp msg/headers/PolygonMesh.h "${DEST}/include/pcl"
    cp msg/headers/Vertices.h "${DEST}/include/pcl"
    cp cmake/ros_macros.h.in "${DEST}/include/pcl/ros_macros.h"
    # Copy test
    mkdir "${DEST}/test"
    #cp -r test/* "${DEST}/test"
    rsync -aC --exclude '*.py*' test/* "${DEST}/test/"
    # Copy misc
    cp pcl_config.h.in "${DEST}"
    cp README "${DEST}"
}

function copy_macro
{
    SRC="${1}"
    TARGET="${2}"
    MACRO="${3}"

    cat "${1}" | sed -n "/macro(${MACRO}/,/endmacro(${MACRO}/p" >> "${TARGET}"
    echo >> "${TARGET}"
}

function copy_cmake
{
    DEST="${1}"
    ROSBUILD_DIR="$(rospack find rosbuild)"

    mkdir -p "${DEST}/cmake"

    cp cmake/rosbuild.cmake "${DEST}/cmake/"
    copy_macro "${ROSBUILD_DIR}/public.cmake" "${DEST}/cmake/rosbuild.cmake" \
        rosbuild_check_for_sse
    copy_macro "${ROSBUILD_DIR}/public.cmake" "${DEST}/cmake/rosbuild.cmake" \
        rosbuild_add_executable
    copy_macro "${ROSBUILD_DIR}/public.cmake" "${DEST}/cmake/rosbuild.cmake" \
        rosbuild_add_compile_flags
    copy_macro "${ROSBUILD_DIR}/public.cmake" "${DEST}/cmake/rosbuild.cmake" \
        rosbuild_add_link_flags
    copy_macro "${ROSBUILD_DIR}/public.cmake" "${DEST}/cmake/rosbuild.cmake" \
        rosbuild_add_openmp_flags
    copy_macro "${ROSBUILD_DIR}/private.cmake" "${DEST}/cmake/rosbuild.cmake" \
        _rosbuild_list_to_string

    sed -i 's/add_dependencies(${exe} rosbuild_precompile)//g' \
        "${DEST}/cmake/rosbuild.cmake"

    cp cmake/find_eigen3.cmake "${DEST}/cmake/"
    cp cmake/find_qhull.cmake "${DEST}/cmake/"
    cp cmake/find_cminpack.cmake "${DEST}/cmake/"
    cp cmake/find_flann.cmake "${DEST}/cmake/"
    cp cmake/find_tbb.cmake "${DEST}/cmake/"
    cp cmake/find_boost.cmake "${DEST}/cmake/"
    cp cmake/windows.cmake "${DEST}/cmake/"
}

function make_cmakelists
{
    DEST="${1}"
    cat ./CMakeLists.txt | \
        sed '/include/s/$ENV{ROS_ROOT}\/core\/rosbuild/cmake/' | \
        sed 's/cmake_minimum_required (VERSION 2.4.6)/cmake_minimum_required (VERSION 2.8)/g' | \
        sed 's/include_directories (src)/include_directories (src include include\/roslib include\/sensor_msgs)/g' | \
        sed 's/add_subdirectory (test)//g' | \
        sed '20i include(cmake/find_eigen3.cmake)\ninclude_directories(${eigen3_INCLUDE_DIRS})' | \
        sed '20i include(cmake/find_flann.cmake)\ninclude_directories(${flann_INCLUDE_DIRS})' | \
        sed '20i include_directories(${CMINPACK_INCLUDE_DIRS}' \
        > ${DEST}/CMakeLists.txt
}

if [ -n "${1}" ]
then
    VERSION="${1}"
fi

if [ -n "${2}" ]
then
    TARBALL_NAME="${2}"
fi

TEMP_DIR="${TARBALL_NAME}-${VERSION}"
echo ${EXTRA_MSGS_DIR}

echo "Making temporary directory ${TEMP_DIR}"
mkdir -p "${TEMP_DIR}"
echo "Copying all files"
copy_all_files "${TEMP_DIR}"
echo "Copying CMake macros"
copy_cmake ${TEMP_DIR}
echo "Making CMakeLists.txt"
make_cmakelists "${TEMP_DIR}"
echo "Creating tarball"
tar -czf ${TEMP_DIR}.tar.gz --exclude-vcs ${TEMP_DIR}
echo "Deleting temporary directory"
rm -rf ${TEMP_DIR}
echo "Done"

