#! /bin/bash

# Change the current directory to the script directory
BASEDIR=$(dirname "$0")
cd $BASEDIR
# Look for the .env file
while [[ "$PWD" != "$HOME" && ! -e .env ]]; do cd ..; done
if [[ -e .env ]]; then
  ENVFILEDIR=$PWD
  set -a; source .env; set +a
  echo "Using .env file found in $PWD"
else
  echo "CANCELED"
  echo "  No .env file found in the directory tree."
  echo "  Please setup the .env file and retry"
fi

# Change the current directory to the home directory
cd $INFAMOUSDIR
echo "EXECUTED FROM $PWD"

# Check if the number of arguments is correct
if (($# != 2))
then
    echo " "
    echo "CANCELED"
    echo "  Wrong number of arguments : $# were provided but 2 are expected :"
    echo "    - path of the local file/directory to upload"
    echo "    - path of the directory (from your superuser home directory) to upload to"
    echo "  Syntax : ./tools/upload_to_apiserver.sh local_path server_path"
    echo "  Example:"
    echo "      To upload the foobar.txt file located in my home directory to a toto directory in your superuser home directory :"
    echo "      ./tools/upload_to_apiserver.sh /Users/comt001/foobar.txt /toto"
    exit
fi

# Create the temporary directory on the api server and set the permissions
echo
echo "API SERVER:"
echo "    CREATING TEMPORARY DIRECTORY AT /tmp/$api_user WITH 777 PERMISSIONS"
#ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser mkdir /tmp/$api_user
#ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser chmod 777 /tmp/$api_user

# Create the temporary directory on the bastion server
echo
echo "BASTION SERVER:"
echo "    CREATING TEMPORARY DIRECTORY AT /home/$api_user/tmp" 
ssh -q -i $pem_path $api_user@$bastion_server "mkdir -p /home/$api_user/tmp" 

# Upload the source directory and its files to the temporary directory on the bastion server
echo
echo "LOCAL:"
echo "    UPLOADING $1 INTO /home/$api_user/tmp ON BASTION SERVER"
scp -i $pem_path -r $1 $api_user@$bastion_server:/home/$api_user/tmp/ 

# Copy the source directory and its files to the temporary directory on the api server 
echo
echo "BASTION SERVER:"
echo "    COPYING CONTENTS FROM /home/$api_user/tmp ON BASTION SERVER TO /tmp/$api_user/ ON API SERVER"
ssh -q -i $pem_path $api_user@$bastion_server "scp -q -r /home/$api_user/tmp/. $api_user@$api_server:/tmp/$api_user/  "

# Create the destination directory on the api server and set the permissions
echo
echo "API SERVER:"
echo "    CREATING DIRECTORY AT /home/$api_suuser/$2"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser "mkdir -p /home/$api_suuser/$2"
echo "    SETTING FILE ACL TO RX FOR $api_suuser (AND MASK) ON /tmp/$api_user/."
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server "setfacl -R -m m:rx /tmp/$api_user/." 
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server "setfacl -R -m u:$api_suuser:rx /tmp/$api_user/." 

# Copy the contents of the temporary directory on the api server to the destination directory on the api server
echo "    COPYING CONTENTS FROM /tmp/$api_user/ TO /home/$api_suuser/$2/"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser "cp -R /tmp/$api_user/. /home/$api_suuser/$2/"

# Remove the temporary directory on the bastion server
echo
echo "BASTION SERVER:"
echo "    REMOVING DIRECTORY AND CONTENTS IN /home/$api_user/tmp"
ssh -q -i $pem_path $api_user@$bastion_server "rm -r /home/$api_user/tmp"

# Remove the temporary directory on the api server
echo
echo "API SERVER:"
echo "    REMOVING DIRECTORY AND CONTENTS IN /tmp/$api_user/"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server rm -r /tmp/$api_user
#ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser rm -r /tmp/$api_user

echo
echo "UPLOAD COMPLETE"


