#!/usr/bin/tclsh8.6

##############################################################################
#
#  			Copyright 2025 <unknown>.
#			      All Rights Reserved
#
#
#  System        : 
#  Module        : 
#  Object Name   : $RCSfile$
#  Revision      : $Revision$
#  Date          : $Date$
#  Author        : $Author$
#  Created By    : <unknown>
#  Created       : Thu Apr 10 12:20:36 2025
#  Last Modified : <250511.1332>
#
#  Description	
#
#  Notes
#
#  History
#	
##############################################################################
#
#  Copyright (c) 2025 <unknown>.
# 
#  All Rights Reserved.
# 
#  This  document  may  not, in  whole  or in  part, be  copied,  photocopied,
#  reproduced,  translated,  or  reduced to any  electronic  medium or machine
#  readable form without prior written consent from <unknown>.
#
##############################################################################


# list of hosts and users on each host. TERMUX_HOSTS must be of the same
# length. User list entries must relate to the IP addresses, i.e. the user
# name must exist on the device at that location.

set TERMUX_HOSTS [ list 192.168.123.60 10.8.0.60 192.168.123.70 10.8.0.70 ]
set TERMUX_USERS [ list u0_a032 u0_a032 u0_a071 u0_a071 ]

proc timestamp { { theTime "" } } {

    if { $theTime == "" } {
        set theTime [ clock seconds ]
    }
    return [ clock format $theTime -format "%Y-%m-%d %H:%M:%S" ]
}

proc timeoutHandler { pdf errMsg } {
    
    catch { close $pfd }
    error "$errMsg"
}

proc runCmd { cmd } {
    
    puts "[ timestamp ] : runCmd: cmd: $cmd:"
    set pfd [ open "| $cmd" ]
    while { ! [ eof $pfd ] } {
        if { [ gets $pfd oneline ] > 0 } {
            puts "[ timestamp ] : runCmd: $oneline"
        }
    }
    catch { close $pfd }
}


set termuxDevice ""
set timeOutAt [ expr [ clock seconds ] + 120 ]
while { $termuxDevice == "" && [ clock seconds ] < $timeOutAt } {
    foreach termuxHost $TERMUX_HOSTS {
        set pfd($termuxHost) [ open "| ping -c 6 -i 1 -W 1 -n -O $termuxHost" ]
    }
    set pingDoneAt [ expr [ clock seconds ] + 8 ]
    while { $termuxDevice == "" && [ clock seconds ] <= $pingDoneAt } {
        foreach termuxHost $TERMUX_HOSTS {
            if { $pfd($termuxHost) != "" } {
                if { ! [ eof $pfd($termuxHost) ] } {
                    if { [ gets $pfd($termuxHost) oneline ] > 0 } {
                        puts "[ timestamp ] : Ping $termuxHost: $oneline"
                        if { [ string first "packet loss" $oneline ] > 0 } {
                            if { [ string first ", 0% packet loss, time" $oneline ] > 0 } {
                                set termuxDevice $termuxHost
                                puts "[ timestamp ] : termuxDevice: $termuxDevice"
                                break
                            }
                        }
                    }
                } else {
                    catch { close $pfd($termuxHost) }
                    set pfd($termuxHost) ""
                }
            }
            if { $termuxDevice != "" } {
                break
            }
        }
    }
    foreach termuxHost $TERMUX_HOSTS {
        if { $pfd($termuxHost) != "" } {
            catch { close $pfd($termuxHost) }
            set pfd($termuxHost) ""
        }
    }
}

puts "Done with ping"

if { $termuxDevice == "" } {
    puts "[ timestamp ] : Done: timeOutAt: $timeOutAt, right now [ clock seconds ]"
    return 0
}

puts "Party time..."

puts "set TERMUX_USER \[ lindex $TERMUX_USERS \[ lsearch -exact $TERMUX_HOSTS $termuxDevice \] \]"
set TERMUX_USER [ lindex $TERMUX_USERS [ lsearch -exact $TERMUX_HOSTS $termuxDevice ] ]

set TARGET [ lindex $argv 0 ]

puts "[ timestamp ] : TARGET: $TARGET, TERMUX_USER: $TERMUX_USER, termuxDevice: $termuxDevice"

foreach arg [ lrange $argv 1 end ] {
    
    if { [ file exists $arg ] } {
        
        set fname [ file tail $arg ]
        
        set timeoutId [ after 10000 "error \"SCP Timeout for $arg\"" ]
        runCmd "/usr/bin/scp -oPort=8022 $arg $TERMUX_USER@$termuxDevice:tmp/$fname"
        after cancel $timeoutId
        
        set timeoutId [ after 20000 "error \"SMS Send Timeout for $arg\"" ]
        runCmd "/usr/bin/ssh -l $TERMUX_USER -oPort=8022 $termuxDevice /bin/sh /data/data/com.termux/files/home/termux-sms-send.sh $TARGET tmp/$fname"
        after cancel $timeoutId
        
        set timeoutId [ after 10000 "error \"RM Timeout for $arg\"" ]
        # runCmd "/usr/bin/ssh -l $TERMUX_USER -oPort=8022 $termuxDevice rm -f tmp/$fname"
        after cancel $timeoutId
    }
}

return 0
