set timeout 20
set IP [lindex $argv 0]
set LOGIN [lindex $argv 1]
set PASSWORD [lindex $argv 2]
set METHOD [lindex $argv 3]
set PON_INT [lindex $argv 4]
set ONU_INT [lindex $argv 5]
set ONU_NUM [lindex $argv 6]
set VLAN [lindex $argv 7]
set SVLAN [lindex $argv 8]
set SN [lindex $argv 9]
set DESCRIPTION [lindex $argv 10]
set DHCPSNOOPING [lindex $argv 11]
set LOOPDETECT [lindex $argv 12]
set VPORT_INT [lindex $argv 13]

if {[string match "TELNET" $METHOD]} {
    spawn telnet $IP
    expect "Username:*"
    send "$LOGIN\r"
} elseif {[string match "SSH" $METHOD]} {
    spawn ssh $LOGIN@$IP
    expect {
    "(yes/no" {
        send "yes\r"
        }
    }
}

expect "Password:*"
send "$PASSWORD\r"
expect "*#"
send "configure terminal\r"

expect "*(config)#"
send "interface $PON_INT\r"
expect "*(config-if-$PON_INT)#"
send "onu $ONU_NUM type 4ETH sn $SN\r"
expect "*(config-if-$PON_INT)#"
send "exit\r"

expect "*(config)#"
send "interface $ONU_INT:$ONU_NUM\r"

if {![string match "__empty" $DESCRIPTION]} {
    expect "*(config-if-$ONU_INT:$ONU_NUM)#"
    send "description $DESCRIPTION\r"
}

expect "*(config-if-$ONU_INT:$ONU_NUM)#"
send "tcont 1 profile unlim\r"
expect "*(config-if-$ONU_INT:$ONU_NUM)#"
send "gemport 1 name unlim tcont 1\r"
expect "*(config-if-$ONU_INT:$ONU_NUM)#"
send "exit\r"

expect "*(config)#"
send "interface $VPORT_INT.$ONU_NUM:1\r"

expect "*(config-if-$VPORT_INT.$ONU_NUM:1)#"
send "service-port 1 user-vlan $VLAN vlan $VLAN svlan $SVLAN\r"
expect "*(config-if-$VPORT_INT.$ONU_NUM:1)#"
send "exit\r"

expect "*(config)#"
send "pon-onu-mng $ONU_INT:$ONU_NUM\r"

if {![string match "__empty" $LOOPDETECT]} {
    expect "*(config-gpon-onu-mng)#"
    send "loop-detect ethuni eth_0/1 enable\r"
    expect "*(config-gpon-onu-mng)#"
    send "loop-detect ethuni eth_0/2 enable\r"
    expect "*(config-gpon-onu-mng)#"
    send "loop-detect ethuni eth_0/3 enable\r"
    expect "*(config-gpon-onu-mng)#"
    send "loop-detect ethuni eth_0/4 enable\r"
}

expect "*(config-gpon-onu-mng)#"
send "service 1 gemport 1 vlan $VLAN\r"
expect "*(config-gpon-onu-mng)#"
send "vlan port eth_0/1 mode tag vlan $VLAN\r"
expect "*(config-gpon-onu-mng)#"
send "vlan port eth_0/2 mode tag vlan $VLAN\r"
expect "*(config-gpon-onu-mng)#"
send "vlan port eth_0/3 mode tag vlan $VLAN\r"
expect "*(config-gpon-onu-mng)#"
send "vlan port eth_0/4 mode tag vlan $VLAN\r"
expect "*(config-gpon-onu-mng)#"
send "end\r"
expect "*#"
send "exit\r"
