#!/usr/bin/expect -f
set timeout -1
if {![info exists env(SUDO_PASSWORD)] || $env(SUDO_PASSWORD) eq ""} {
  puts "Missing SUDO_PASSWORD env variable."
  exit 1
}
set sudo_password $env(SUDO_PASSWORD)
if {![info exists env(CONFIRM_REMOTE_OPS)] || $env(CONFIRM_REMOTE_OPS) ne "YES"} {
  puts "Refusing destructive remote fix. Set CONFIRM_REMOTE_OPS=YES to continue."
  exit 1
}
set jump_host "wooo@192.168.0.110"
set target_host "ollama@192.168.0.188"

spawn ssh -J $jump_host $target_host "echo $sudo_password | sudo -S sed -i 's/\"3000:3000\"/\"3004:3000\"/g' /home/ollama/docker-compose.yml"
expect {
  "*assword:*" {
    send "$sudo_password\r"
    exp_continue
  }
  eof
}

spawn ssh -J $jump_host $target_host "echo $sudo_password | sudo -S docker compose -f /home/ollama/docker-compose.yml stop web && echo $sudo_password | sudo -S docker compose -f /home/ollama/docker-compose.yml up -d web"
expect {
  "*assword:*" {
    send "$sudo_password\r"
    exp_continue
  }
  eof
}

spawn ssh -J $jump_host $target_host "echo $sudo_password | sudo -S docker start tsenyang-website"
expect {
  "*assword:*" {
    send "$sudo_password\r"
    exp_continue
  }
  eof
}
