#!/bin/bash

if [ "x$1" == "x--create" ] ; then
   package=$2
   icon=$3
   displayname=$4
   description=$5
   launch=$6
   platform=$7
   app_files_name=${launch%.launch}
   cp $icon ${icon##*/}
   cat > $app_files_name.interface << EOF
published_topics: {}
subscribed_topics: {}
EOF
   cat > $app_files_name.installed << EOF
apps:
  - app: $package/$app_files_name
    display: $displayname
EOF
   if [ -d ~/local_apps ] && [ $platform == "pr2" ]; then
      cp $app_files_name.installed ~/local_apps
   elif [ $platform == "turtlebot" ] && [ -d `rospack find turtlebot_bringup` ]; then
      sudo cp $app_files_name.installed `rospack find turtlebot_bringup`/mock_apps
   fi
   cat > $app_files_name.app << EOF
display: $displayname
description: $description
platform: $platform
launch: $package/$launch
interface: $package/$app_files_name.interface
icon: $package/${icon##*/}
EOF

echo "Add the following to the .rosinstall file:"
echo "- other: {local-name: $package}"
echo "And then run 'rosinstall . ' in the ROS install directory."
echo "Add the following lines to the .bashrc: 'echo \"Sourcing /u/applications/ros/setup.bash\"'"
echo "And: '. /u/applications/ros/setup.bash'"

else
   echo "Usage: $0 --create <packagename> <iconpath> <displayname> <description>"
   echo "                   <launchfilename> <platform>:"
   echo "              creates the .app, .installed, .interface files to launch your app from Android"
   echo "              WARNING: will overwrite existing files!"
   echo "              Run this inside of a pre-existing directory inside"
   echo "              your ROS package path."
   echo "Example: $0 --create pr2_tuck_arms_app /usr/local/bin/icon.png \"Tuck Arms\" \"Tucks Pr2's arms.\" pr2_tuck_arms_app.launch pr2"
fi


