#!/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.1256>
#
#  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>.
#
##############################################################################

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

proc timestamp { { theTime "" } } {

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

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

if { ! [ lindex $argv ] } {
    error "Usage: $argv0 comma-separated-list-of-phone-numbers-starting-with-plus-country-code"
}

set fd [ open "/tmp/disk-info.txt" w ]
puts $fd "Subject: Heartbeat"
puts $fd "From: [ file tail $env(HOME) ]@[ info hostname ]\n\n"
puts $fd "[ timestamp ]\ndf -k:"
puts $fd [ join [ runCmd "df -k" ] "\n" ]
puts $fd "ps -ef:"
puts $fd [ join [ runCmd "ps -ef" ] \n" ]
close $fd
puts "[ timestamp ] : Calling send-sms.sh"
runCmd "$env(HOME)/bin/send-sms [ lindex $argv 0 ] /tmp/disk-info.txt"
puts "[ timestamp ] : Back from send-sms"

puts "[ timestamp ] : Done"

return 0
