#! /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"
# Set the timestamp
timestamp=$(date +%Y%m%d-%H%M%S)

# Check if the number of arguments is correct
if (($# != 1))
then
    echo " "
    echo "CANCELED"
    echo "  Wrong number of arguments : $# were provided but 1 is expected (path of the directory to download from your superuser home directory)"
    echo "  Syntax : ./tools/download_from_server.sh path_to_download"
    echo "  Example:"
    echo "      To download the api-server directory _LOGS located in your superuser home directory:"
    echo "      ./tools/download_from_server.sh _LOGS"
    exit
fi

path=$1
path="${path%/}"
path="${path#/}"
base_name=$(basename "$path")
dir_name=$(dirname "$path")

# Check if the path is a directory or a file
path_type=$(ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser "[ -d /home/$api_suuser/$1 ] && echo 'directory' || echo 'file'")
if [ "$path_type" == "directory" ]; then
    echo "/home/$api_suuser/$1 is a directory"
    output_path="$dir_name/$base_name"
else
    echo "/home/$api_suuser/$1 is a file"
    output_path="$dir_name"
fi

# Create the temporary directory on the api-server
echo
echo "API SERVER:"
echo "    (SUPERUSER) CREATING TEMPORARY DIRECTORY AT /tmp/$api_user"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser mkdir /tmp/$api_user

# Copy the source file to the temporary directory
echo "    (SUPERUSER) COPYING /home/$api_suuser/$1 TO /tmp/$api_user"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server sudo -u $api_suuser "cp -r /home/$api_suuser/$1 /tmp/$api_user"

# Set the permissions for the source path
echo "    (SUPERUSER) SETTING ACL FOR $api_user AND MASK ON /tmp/$api_user"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server "sudo -u $api_suuser setfacl -R -m m:rx /tmp/$api_user"
ssh -q -i $pem_path $api_user@$bastion_server ssh -q $api_user@$api_server "sudo -u $api_suuser setfacl -R -m u:$api_user:rx /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" 

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

# Create the local directory to store the downloaded files
echo
echo "LOCAL MACHINE:"
echo "    CREATING DIRECTORY $APIDIR/$output_path/$timestamp"
mkdir -p $APIDIR/$output_path/$timestamp
# Download the files from the bastion server to the local directory
echo
echo "COPYING (BASTION TO LOCAL):"
echo "    COPYING /home/$api_user/tmp/$1 FROM BASTION SERVER INTO $APIDIR/$output_path/$timestamp ON LOCAL MACHINE"
if [ "$path_type" == "directory" ]; then
    scp -i $pem_path -r $api_user@$bastion_server:/home/$api_user/tmp/$base_name/. $APIDIR/$output_path/$timestamp
else
    scp -i $pem_path -r $api_user@$bastion_server:/home/$api_user/tmp/$base_name $APIDIR/$output_path/$timestamp
fi

# Remove the temporary directory on the api-server
echo
echo "API SERVER:"
echo "    (SUPERUSER) REMOVING TEMPORARY DIRECTORY AT /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
# Remove the temporary directory on the bastion server
echo
echo "BASTION SERVER:"
echo "    REMOVING TEMPORARY DIRECTORY AT /home/$api_user/tmp"
ssh -q -i $pem_path $api_user@$bastion_server "rm -r /home/$api_user/tmp"
