#!/bin/bash
# A quick command to copy sensitive files from development machine to server without involvement of GitHub:

# Read the servers from the file into an array:
mapfile -t SERVERS < servers.txt

# Display the numbered list of server options:
echo "Please select a server:"
for i in "${!SERVERS[@]}"; do
    echo "$((i + 1)). ${SERVERS[i]}"
done

# Prompt the user for their selection
read -rp "Select a server by the number ... : " SELECTION

# Get a port no.:
read -rp "Set a port no. .................. : " PORT

# Validate the selection
if [[ $SELECTION -gt 0 && $SELECTION -le ${#SERVERS[@]} ]]; then

    # Note down the selection in a variable:
    SELECTED_SERVER=${SERVERS[$((SELECTION - 1))]}

    # Ask the user which directory he wants to upload and his username on the server:
    read -rp "Your username on the server ..... : " USER
    read -rp "Directory to send ............... : " SOURCE

    # Run the rsync command:
    PROJECT_DIRECTORY=$(basename "$PWD")
    DESTINATION="$USER@$SELECTED_SERVER://home/$USER/programming/python/$PROJECT_DIRECTORY"
    echo "Trying to send '$SOURCE' to '$DESTINATION'"
    rsync -avz --mkpath --progress -e "ssh -p $PORT" "$SOURCE" "$DESTINATION"

    # Exit with success (assuming that the actual data sending went well):
    exit 0

else

    # In case of a wrong server selection, exit with failure.
    echo "Invalid selection. Please run the script again."
    exit 1

fi
