# config.tcl
# Copyright (c) 2003 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
# ***************************************************************************

namespace eval Config {
    variable arrSettings
    variable boolDHCPSettable 1
}

proc Config::Init {} {
    variable arrSettings

    ## These are the special mentry textvariables
    foreach name {IP Submask Gateway} {
        upvar #0 ::Config::arr$name Arr
        set Arr(0) {}
        set Arr(1) {}
        set Arr(2) {}
        set Arr(3) {}
    }
    foreach name [array names arrSettings] {
        set arrSettings($name) {}
    }
    set arrSettings(lstNSettable) {IP Submask Gateway}
    set arrSettings(lstWSettable) {AutoSSID AuthMode DesiredSSID EncryptionMode EncryptionKey}
}

proc Config::NetPreset {} {
    variable arrSettings
    foreach name {IP Submask Gateway} {
        if {![string equal $::Discover::arrSettings($name) "NA"] && \
            ![string equal $::Discover::arrSettings($name) "(null)"]} {
            catch {
            upvar #0 ::Config::arr$name Arr
            set lstAddress [split $::Discover::arrSettings($name) .]
            for {set i 0} { $i < 4} {incr i} {
                set Arr($i) [lindex $lstAddress $i]
            }
            }; #end catch
        }
    }
    foreach name {Domain Name DHCP} {
        if {![string equal $::Discover::arrSettings($name) "NA"] && \
            ![string equal $::Discover::arrSettings($name) "(null)"]} {
            catch {
            set arrSettings($name) $::Discover::arrSettings($name)
            }; #end catch
        } elseif {$name == "DHCP"} {
            ## Legacy devices are shipped with DHCP=On but if they get here
            ## we'll assume their network does not have it enabled.
            set arrSettings(DHCP) Off
            set boolDHCPSettable 0
        }
    }
}

proc Config::Arrays2Strings {} {
    variable arrSettings
    ##Trim any extra white space
    set arrSettings(IP) [string trim [$::Network::arrEntries(IP) getstring]]
    set arrSettings(Submask) [string trim [$::Network::arrEntries(Submask) getstring]]
    set arrSettings(Gateway) [string trim [$::Network::arrEntries(Gateway) getstring]]
}

proc Config::WlessPreset {} {
    variable arrSettings
    set ::Wireless::lstCountry [::Wireless::GetCountryList]
    
    foreach name $arrSettings(lstWSettable) {
        catch {
        set arrSettings($name) $::Discover::arrSettings($name)
        }; #end catch
    }
    if {[info exists arrSettings(EncryptionMode)] && \
        [string match WEP* $arrSettings(EncryptionMode)]} {
            set arrSettings(EncryptionMode) "WEP"
    } else {
        set arrSettings(EncryptionMode) "Disabled"
    }
}

