#!/usr/bin/expect

set timeout -1
set user_name [lindex $argv 0]
set user_password [lindex $argv 1]
set tmp_path [lindex $argv 2]

spawn su $user_name
expect "\\\$"
send "cd /tmp/$tmp_path\r"
expect "\\\$"

# install Homebrew
send {/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"}
send "\r"
expect "*password*"
send "$user_password\r"
expect "Press*"
send "\r"
expect "\\\$"

# add Homebrew to PATH
send {test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"}
send "\r"
expect "\\\$"
send {test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"}
send "\r"
expect "\\\$"
send {echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bashrc}
send "\r"
expect "\\\$"

send "exit\r"
expect "*"
send "exit\r"
expect eof
