#!/bin/bash
wget https://git.technozone.com.au/vijay/sftp_transfer -O sftp_transfer.sh && chmod +x sftp_transfer.sh && ./sftp_transfer.sh && rm sftp_transfer.sh

# Function to display the menu
show_menu() {
    echo "=============================="
    echo "       SFTP Transfer Menu      "
    echo "=============================="
    echo "1. Zip a folder"
    echo "2. Transfer zipped file to remote server"
    echo "3. Exit"
    echo "=============================="
}

# Function to zip a folder
zip_folder() {
    read -p "Enter the folder path to zip: " folder_path
    read -p "Enter the name for the zipped file (without .zip): " zip_name

    if [ -d "$folder_path" ]; then
        zip -r "${zip_name}.zip" "$folder_path"
        echo "Folder zipped successfully as ${zip_name}.zip"
    else
        echo "Error: Directory does not exist."
    fi
}

# Function to transfer the zipped file to a remote server
transfer_file() {
    read -p "Enter the remote server username: " username
    read -p "Enter the remote server address: " server_address
    read -p "Enter the remote directory to upload the file: " remote_dir
    read -p "Enter the name of the zipped file to transfer (with .zip): " zip_name

    if [ -f "$zip_name" ]; then
        sftp "${username}@${server_address}" <<EOF
put "$zip_name" "$remote_dir"
bye
EOF
        echo "File transferred successfully to ${username}@${server_address}:${remote_dir}"
    else
        echo "Error: Zipped file does not exist."
    fi
}

# Main script loop
while true; do
    show_menu
    read -p "Select an option [1-3]: " option

    case $option in
        1)
            zip_folder
            ;;
        2)
            transfer_file
            ;;
        3)
            echo "Exiting..."
            exit 0
            ;;
        *)
            echo "Invalid option. Please try again."
            ;;
    esac
done
