#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage: $0 <photo_number>"
    exit 1
fi

PHOTO_NUMBER="$1"
PHOTO_NAME="photo_${PHOTO_NUMBER}.jpeg"
LOCAL_PATH="/home/node/Downloads/ttm18_photoes/${PHOTO_NAME}"

echo "Starting upload process for ${PHOTO_NAME}..."

echo "Downloading ${PHOTO_NAME} from device using SCP..."
# SSH config handles the legacy algorithm settings
sshpass -p 'artosyn' scp -O root@192.168.7.1:/media/${PHOTO_NAME} /home/node/Downloads/ttm18_photoes/

if [ $? -eq 0 ]; then
    echo "Download successful: ${PHOTO_NAME}"
    
    echo "Uploading ${PHOTO_NAME} to web server using SFTP..."
    sshpass -f /home/node/scripts/.ftp_pass sftp -P 221 alupeta@testing.tigerroad.net << EOL
cd html/QA_folder/TTM18/Test_images
put ${LOCAL_PATH}
bye
EOL
    
    if [ $? -eq 0 ]; then
        echo "Upload successful: ${PHOTO_NAME}"
        echo "Photo available at: https://testing.tigerroad.net/QA_folder/TTM18/Test_images/${PHOTO_NAME}"
    else
        echo "Upload failed: ${PHOTO_NAME}"
        exit 1
    fi
else
    echo "Download failed: ${PHOTO_NAME}"
    exit 1
fi

echo "Upload process completed successfully for ${PHOTO_NAME}"
