#!/usr/bin/expect -f
log_user 0
if { [llength $argv]==0 } {
	puts "Hostname missing\n(all,tv,pc,serverhost,quiethost)"
exit 0
}
set color [lindex $argv 1]
if { $color == "-c" } {
set red "\033\[1;31m"
set blue "\033\[1;34m"
set norm "\033\[1;0m"
} else {
set red ""
set blue ""
set norm ""
}
set ret1 ""
array set host { 0 "serverhost" 1 "quiethost" }
array set user { 0 "lucas" 1 "lucas" }
array set port { 0 "3032" 1 "1504"}
set TARGET [lindex $argv 0]
set start 0
set end -1
if { ${TARGET} == "all" || ${TARGET} == "pc" } {
	set start 0
	set end 1 
} elseif { ${TARGET} == "serverhost" } {
	set start 0
	set end 0
} elseif { ${TARGET} == "quiethost" } {
	set start 1
	set end 1
} else {
	puts "Target ${TARGET} not known"
	exit 0
}
#if {[string match 10.0.0.26* $env(SSH_CLIENT)]} {
#	exec ssh -o ConnectTimeout=4 -p 1504 lucas@quiethost notify-send "Shutdown ${TARGET}"
#}
for {set i $start} {$i <= $end} {incr i 1} {
	spawn ssh -o ConnectTimeout=4 -p  $port($i) $user($i)@$host($i)
	expect {
			"Connection timed out" {
					puts "$host($i)\t\[${red} FAIL ${norm}\]"
			}
			"$ " {
					send "cat /home/lucas/.auto_shutdown\r"
					expect "$ "
					set ret $expect_out(buffer)
					if {[string match *no* $ret ]} {
							puts "Shutdown disabled! $host($i) Shutdown aborted!"
							exit 0
					} else {
						puts "Shutdown enabled"
					}
					send "ps -elf | grep scripts/backup | grep -v grep\r"
					expect "$ "
					set ret $expect_out(buffer)
					if {[string match *bash* $ret ]} {
							puts "Backup in Progress! $host($i) Shutdown aborted!"
							exit 0
					} else {
						puts "No Backup in Progress"
					}
					send "ps -elf | grep scripts/upgrade | grep -v grep\r"
					expect "$ "
					set ret $expect_out(buffer)
					if {[string match *bash* $ret ]} {
							puts "Update in Progress! $host($i) Shutdown aborted!"
							exit 0
					} else {
						puts "No Update in Progress"
					}	
					send "sudo /sbin/shutdown -P 1 +6\r"
					#send "echo Shutdown\r"
					expect "$ "
					set ret $expect_out(buffer)
					send "exit\r"
					expect "$ "
					set ret1 [exec echo $ret | grep -o "Shutdown scheduled"]
				}	
	}
	if {$ret1 == "Shutdown scheduled"} {
		puts "$host($i)\t\[${blue} DOWN${norm} ]"
		if {[string match 10.0.0.26* $env(SSH_CLIENT)]} {
			exec ssh -o ConnectTimeout=4 -p 1504 lucas@quiethost notify-send "Shutdown SUCESSFULL for ${TARGET}"
		}
	} else {
		puts "$host($i)\t\[${red} FAIL${norm} ]"
		if {[string match 10.0.0.26* $env(SSH_CLIENT)]} {
			exec ssh -o ConnectTimeout=4 -p 1504 lucas@quiethost notify-send "Shutdown FAILED for ${TARGET}"
		}
	}
}
if { ${TARGET} == "quiethost-win" } {
	spawn ssh -o ConnectTimeout=6 -p 1504 LF@quiethost
        expect {
			"Connection timed out" {
                       		puts "quiethost\t\[ ${red} FAIL ${norm} ]"
                        }
			"LF>" {
			send "shutdown /p\r"
			expect {
					"LF>Received disconnect" {
						puts "quiethost\t\[${blue} DOWN${norm} ]"
						send "exit\r"
					}
				}
			}
	}
}
if { ${TARGET} == "all" || ${TARGET} == "tv" } {
	spawn bash /home/lucas/Raspberry/scripts/samsung_remote.sh OFF	
	puts "TV\t\[ ${blue} DOWN${norm} ]"
}

