#!/bin/ksh
#
# Script to construct the arguments for the adx command.
# If Mission directory name is not passed as an argument then
# prompt user for directory name and validate it's existence
# before executing the adx command.  If the portal computer
# changes then edit the portal_name variable.
#
# Mike McCann  6 October 2004
#
# $Id$

# SSDS Portal computer
#portal_name=134.89.13.120
#portal_name=auv-2-control

#portal_name=jeckel.shore.mbari.org
portal_name=134.89.12.45
portal_port=9010
echo "Missions will be sent to AUVPortalService: $portal_name:$portal_port"

# Vehicle Computer attributes
vc_name=mvc-dmo1
vc_login=dorado1
vc_pw=dorado1

# Location of log data files on the vehicle copmuter
log_path=/home/dorado1/auv/altex/onboard/logs

# Set the mission to upload
if test $1
then
   log_dir=$1
   echo "log_dir = $log_dir"
else
   echo "Enter mission to upload (YYYY.DDD.MM): \c"
   read log_dir
fi

# Test that log_dir is a directory that exists
if [ ! -d "$log_path/$log_dir" ]
then
   echo "\nDirectory $log_path/$log_dir does not exist.  Exiting.\n"
   exit -1
fi

# Set campaign name
if test $2
then
   campaign=$2
else
   echo "Enter campaign name (e.g. AUVCTD, BIOLUME, OCCO): \c"
   read campaign
fi

# Test that entered campaign name is valid - add more to this if it needs to be checked
if [ "$campaign" = "AUVCTD" -o "$campaign" = "BIOLUME" -o "$campaign" = "OCCO" ]
then
   echo "campaign = $campaign"
else
   echo "$campaign is not a known name.  Do you still want to use it? (y/n) \c"
   read ans
fi
if [ "$ans" = "n" -o "$ans" = "N" ]
then
   exit -2
fi

# Construct "survey" name from log directory string (remove mission number and periods)
yyyyddd=`echo $log_dir | sed 's/\.[0-9][0-9]$//' | sed 's/\.//g'`;
survey_name="${campaign}${yyyyddd}"

# Offer to not remove existing (e.g. 2004.271.00_data.tar.gz) tarball in mission directory.
tarball="$log_path/$log_dir/${log_dir}_data.tar.gz"
if [ -f $tarball ]
then
   echo "Removing previous tarball: $tarball"
   rm $tarball
fi

adx_string="adx $portal_name $vc_name $vc_login $vc_pw $portal_port $survey_name $log_path/$log_dir "


# Execute adx command (portal must be running)
echo "Executing:  "
echo $adx_string
$adx_string

if [ $? != 0 ]
then
   echo "*** Data not uploaded.  Is the AUV Portal software running on $portal_name? ***"
else
   echo "Done uploading $log_dir."
   echo " "
fi

