#!/usr/bin/env bash

###########################
# Functions
###########################

show_help ()
{
  echo ""
  echo "Pulls the installspace from the server to /opt/yujin/indigo."
  echo "Make sure you call this with sudo or set the correct permissions"
  echo "on /opt/yujin."
  echo ""
  echo "    Usage:"
  echo "        sudo groot_binaries <required-options> <options>"
  echo ""
  echo "    Required Option (one of):"
  echo "        --stable          : work with the stable binaries"
  echo "        --devel           : work with the devel binaries"
  echo ""
  echo "    Options:"
  echo "        --help            : this help message"
  echo "        --external        : use the external server instead of the internal one"
  echo "        --install-rosdeps : install all rosdeps required by the groot binary installspace"
  echo ""
  echo "    Admin Options:"
  echo "        --push            : push local /opt/yujin/indigo installspace with the server"
  echo ""
  exit 0
}

###########################
# Command Line
###########################

for i in "$@"
do
case $i in
    --help)
    SHOW_HELP=1
    shift
    ;;
    --install-rosdeps)
    INSTALL_ROSDEPS=1
    shift
    ;;
    --external)
    EXTERNAL=1
    shift
    ;;
    --stable)
    STABLE=1
    shift
    ;;
    --devel)
    DEVEL=1
    shift
    ;;
    --push)
    PUSH=1
    shift
    ;;
    *)
       # unknown option
    ;;
esac
done

###########################
# Aborts
###########################

if [ ! -z "$SHOW_HELP" ]; then
  show_help
  exit 0
fi

if [ -z "$STABLE" ] && [ -z "$DEVEL" ]; then
  echo ""
  echo "ERROR : you must set one of either --stable or --devel"
  echo ""
  exit 1
fi

###########################
# Variables
###########################

if [ ! -z "$STABLE" ]; then
  RELEASE="stable"
else
  RELEASE="devel"
fi

# put the <rosdistro> folder here, should eventually move it to /opt/yujin/amd64
export local_dir=/opt/yujin/amd64

if [ ! -z "$EXTERNAL" ]; then
  export remote_url=files@files.yujinrobot.com
  export remote_dir=${remote_url}:rsync_repositories/yujin/amd64
else
  export remote_url=files@files.yujin.com
  export remote_dir=${remote_url}:shared/installspaces/yujin/amd64
fi

# Options
# --delete    : files from the destination directories
# --recursive : recurse into directories
# --links     : copy symlinks as symlinks

export OPTIONS="--delete --recursive --links --verbose --compress"
#EXCLUDES=(
#  --exclude '*.pyc'
#)

if [ ! -z "$INSTALL_ROSDEPS" ]; then
    echo ""
    echo "Installing all rosdeps required by the binaries installspace in ${local_dir}/indigo-${RELEASE}"
    echo ""
    source ${local_dir}/indigo-${RELEASE}/setup.bash
    rosdep install -r --default-yes --from-paths ${local_dir}/indigo-${RELEASE} --ignore-src
    exit 0   
fi

if [ ! -z "$PUSH" ]; then
    echo ""
    echo "Pushing changes to the file server '${remote_dir}'."
    # Need to automate this script on jenkins.
    #echo "Do you really want to push changes (if you aren't sure, you definitely do not)?[y/N]:"
    #read answer
    #if [ "$answer" == "y" ]; then
        rsync ${OPTIONS} ${local_dir}/indigo-${RELEASE} ${remote_dir}
    #else
    #    echo "Aborting."
    #fi
    echo ""
else
    rsync ${OPTIONS} ${remote_dir}/indigo-${RELEASE} ${local_dir}
    echo ""
    echo "You may need an additional 'groot_binaries --${RELEASE} --install-rosdeps' if there are new dependencies."
    echo ""
fi

