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 SN [lindex $argv 8]
set DESCRIPTION [lindex $argv 9]
set DHCPSNOOPING [lindex $argv 10]
set LOOPDETECT [lindex $argv 11]

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)#"
send "onu $ONU_NUM type 4ETH sn $SN\r"
expect "*(config-if)#"
send "exit\r"

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

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

expect "*(config-if)#"
send "tcont 1 name unlim profile unlim\r"
expect "*(config-if)#"
send "gemport 1 name unlim tcont 1\r"
expect "*(config-if)#"
send "gemport 1 traffic-limit downstream unlim \r"
expect "*(config-if)#"
send "switchport mode trunk vport 1\r"
expect "*(config-if)#"
send "service-port 1 vport 1 user-vlan $VLAN vlan $VLAN \r"

if {![string match "__empty" $DHCPSNOOPING]} {
    expect "*(config-if)#"
    send "ip dhcp snooping enable vport 1\r"
}

expect "*(config-if)#"
send "exit\r"

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

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

expect "*(gpon-onu-mng *"
send "flow mode 1 tag-filter vlan-filter untag-filter discard\r"
expect "*(gpon-onu-mng *"
send "flow 1 pri 0 vlan $VLAN\r"
expect "*(gpon-onu-mng *"
send "gemport 1 flow 1\r"
expect "*(gpon-onu-mng *"
send "switchport-bind switch_0/1 iphost 1\r"
expect "*(gpon-onu-mng *"
send "ip-host 1 dhcp-enable enable ping-response enable traceroute-response enable\r"
expect "*(gpon-onu-mng *"
send "vlan-filter-mode iphost 1 tag-filter vlan-filter untag-filter discard\r"
expect "*(gpon-onu-mng *"
send "vlan-filter iphost 1 pri 0 vlan $VLAN\r"
expect "*(gpon-onu-mng *"
send "dhcp-ip ethuni eth_0/1 from-onu\r"
expect "*(gpon-onu-mng *"
send "dhcp-ip ethuni eth_0/2 from-onu\r"
expect "*(gpon-onu-mng *"
send "dhcp-ip ethuni eth_0/3 from-onu\r"
expect "*(gpon-onu-mng *"
send "dhcp-ip ethuni eth_0/4 from-onu\r"
expect "*(gpon-onu-mng *"
send "end\r"
expect "*#"
send "exit\r"

