#!/usr/bin/env bash

#
# Copyright (C) Monterey Bay Aquarium Research Institute (MBARI)
#
# 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.
#
#

# Creates linked LRAUV run folders at a specifed location
# Requires:
#   symlinks

if [ $# -eq 2 ]; then
  TARGET_PATH=$1
  LRAUV_PATH=$2
else
  echo ""
  echo "`basename $0` requires two inputs:"
  echo "	(1) Path to the desired vehicls folder."
  echo "	(2) Path to the lrauv-application folder."
  echo ""
  echo "      usage example: `basename $0` ~/lrauv_ws/vehicles ~/lrauv_ws/src/lrauv-application"
  echo ""
  exit
fi

mkdir -p $TARGET_PATH

# Create a linked vehicle folder for each vehicle folder 
# found in Config/sim/ign
for f in $LRAUV_PATH/Config/sim/gz/*; do
    if [ -d "$f" ]; then
	    
		VH=$(basename $f)
		VH_PATH=$TARGET_PATH/lrauv/$VH
	    
		echo ""
		echo "Creating vehicle folder for $VH at $VH_PATH"

	    mkdir -p $VH_PATH
	    if [ -d "$VH_PATH" ]; then
		    
		    ln -s $LRAUV_PATH/bin $VH_PATH/bin
		    ln -s $LRAUV_PATH/Modules $VH_PATH/Modules
		    ln -s $LRAUV_PATH/Missions $VH_PATH/Missions
		    ln -s $LRAUV_PATH/Config $VH_PATH/Config
		    ln -s $LRAUV_PATH/Resources $VH_PATH/Resources

			# Convert the links to relative paths for portability
		    symlinks -cr $VH_PATH/.
	    fi
    fi
done

echo ""
echo "Copying run scripts to $TARGET_PATH"
cp $LRAUV_PATH/Tools/gz/scripts/run_lrauv $TARGET_PATH/.
cp $LRAUV_PATH/Tools/gz/scripts/clear_logs $TARGET_PATH/.
echo "Done."
