#!/bin/bash
################################################################################
#
# Copyright (c) 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

CURRENT_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")

set -e

. "${CURRENT_DIR}"/product.sh


install_deps_to_rootfs()
{
  # Install dependencies
  read -r -a array <<< "$(get_packages_list)"

  RELEASE_DIR=$(get_rdk_ws_dir)/rdk_release

  # Execute install_deps.sh with --release in each packages/scripts
  for pkg in "${array[@]}"
  do
    info "Copy files to rootfs for [$pkg]"

    local deps_dir
    deps_dir=$(get_packages_dir)/${pkg}/deps
    local target_dir
    target_dir=$(get_current_product_deps_dir)

    ## find *.deps and do execution
    dep_list=$(find "${deps_dir}" -name '*.deps' | sort)
    info "deps list: \n${dep_list}"
    for file in ${dep_list[@]}
    do
      info "Execute $file $target_dir --release $RELEASE_DIR"
      bash "$file" "$target_dir" "--release" "$RELEASE_DIR"
    done
  done
}

install_ros2_to_rootfs()
{
  # Install ROS2 packages
  ROOTFS=$(get_rdk_ws_dir)/rdk_release/rootfs
  echo "Install ROS2 packages to $ROOTFS"
  sudo mkdir -p "$ROOTFS"/opt/robot_devkit
  sudo cp -rf /opt/robot_devkit/* "$ROOTFS"/opt/robot_devkit

  echo "# Install rootfs to system" >> "$ROOTFS"/../install.sh
  echo "sudo cp -rf ./rootfs/* /" >> "$ROOTFS"/../install.sh
}

generate_tarball()
{
  RDK_TOP=$(get_root_dir)
  RELEASE_DIR=$(get_rdk_ws_dir)/rdk_release
  cp "${RDK_TOP}"/demo/run_listener.sh "${RELEASE_DIR}"/run_listener.sh
  cp "${RDK_TOP}"/demo/run_talker.sh "${RELEASE_DIR}"/run_talker.sh

  cd "$RELEASE_DIR"/..
  # TODO: fix tar: Exiting with failure status due to Permission denied"
  if [[ -d "$ROOTFS"/opt/intel/.scripts/ ]];then
    sudo chmod 644 "$ROOTFS"/opt/intel/.scripts/*
  fi

  version=$(date +'%Y%m%d%H%M')
  tar czvf rdk_release_${version}.tar.gz rdk_release
  md5sum rdk_release_${version}.tar.gz > rdk_release_${version}.tar.gz.md5
  info "Succeed to generate tarball to $RELEASE_DIR/../rdk_release_${version}.tar.gz"
}

#######################################
# Create a binary tarball package for target device deployment
#######################################
release_rdk()
{
  info "Release rdk ...\n"

  if [ ! -f /opt/robot_devkit/robot_devkit_setup.bash ]; then
    echo "Error: Not found installed RDK packages, please build and install RDK first"
    exit
  fi

  # create target install.sh
  RELEASE_DIR=$(get_rdk_ws_dir)/rdk_release

  # Delete old release directory if exist
  if [ -d "$RELEASE_DIR" ]; then
    echo "Delete $RELEASE_DIR"
    sudo rm -rf "$RELEASE_DIR"
  fi

  mkdir -p "${RELEASE_DIR}"
  cat << EOF > "${RELEASE_DIR}"/install.sh
#!/bin/bash
# Auto-generated by "./rdk.sh release"
CURRENT_DIR=\$(dirname "\$(readlink -f "\${BASH_SOURCE[0]}")")
EOF
  chmod +x "${RELEASE_DIR}"/install.sh

  install_deps_to_rootfs
  install_ros2_to_rootfs
  generate_tarball

  ok "Successfully installled files on ${RELEASE_DIR}\n"
}

unset CURRENT_DIR
