set ds(dataFile) @dbdir@/logbook.dat

set ds(format) {
    {
	{{     Date}   {6 4}   {}    8   {6 16}}
	{{    Start}   {7 4}   {}    4   {7 16}}
	{{      End}   {8 4}   {}    4   {8 16}}
	{{ Callsign}   {9 4}   {}    20  {9 16}}
	{{     Name}   {10 4}  {}    20  {10 16}}
	{{      QTH}   {11 4}  {}    30  {11 16}}
	{{  Country}   {12 4}  {}    30  {12 16}}
	{{Frequency}   {13 4}  {}    10  {13 16}}
	{{     Mode}   {14 4}  {cw}  10  {14 16}}
	{{    Power}   {15 4}  {}    10  {15 16}}
	{{  His RST}   {16 4}  {}    3   {16 16}}
	{{   My RST}   {17 4}  {}    3   {17 16}}
	{{ QSL Sent}   {18 4}  {no}  5   {18 16}}
	{{ QSL Rcvd}   {19 4}  {no}  5   {19 16}}
	{{ Comments}   {20 4}  {}    50  {20 16}}
    }
}

#set at(normal) [setattr -fg WHITE -bg BLUE -at BOLD]
#set at(title) [setattr -fg CYAN -bg BLUE -at BOLD]
#set at(entry) [setattr -fg WHITE -bg BLUE -at BOLD]
#set at(prompt) [setattr -fg YELLOW -bg BLUE -at BOLD]
#set at(info) [setattr -fg GREEN -bg BLUE -at BOLD]

lappend ds(exSwitches) {S} dsInsertStart {E} dsInsertEnd \
	{l} dsLookupCountry {k} dsCheckCountry {d} dsShowCountryData


set ds(qHelp) {
    {p	    {Previous Record}}
    {n	    {Next Record}}
    {<	    {First Record}}
    {>	    {Last Record}}
    {<BS>   {Previous Field}}
    {<TAB>  {Next Field}}
    {<CR>   {Edit Field}}
    {t      {Convert To Title}}
    {u      {Convert To Uppercase}}
    {d	    {Delete Record}}
    {w	    {Write Data}}
    {x	    {Exit Query Mode}}
    {?	    {This Help Page}}
}

set ds(help) {
    {<BS>   {Previous Field}}
    {<TAB>  {Next Field}}
    {<CR>   {Edit Field}}
    {t      {Convert To Title}}
    {u      {Convert To Uppercase}}
    {S	    {Insert Start Time}}
    {E	    {Insert End Time}}
    {a	    {Add record}}
    {c	    {Clear Fields}}
    {i      {Init Fields}}
    {q	    {Query}}
    {Q      {Query Current Field}}
    {l      {Lookup Country}}
    {k      {Check Country}}
    {d      {Show Country Data}}
    {s	    {Sort Records}}
    {w	    {Write Data}}
    {e	    {Export Data}}
    {x	    {Exit Program}}
    {?	    {This Help Page}}
}


proc dsInsertStart {} {
    global ds

    set ds(f0) [clock format [clock seconds] -format "%Y%m%d" -gmt 1]
    set ds(f1) [clock format [clock seconds] -format "%H%M" -gmt 1]
    dsDrawScreen
}


proc dsInsertEnd {} {
    global ds

    set ds(f2) [clock format [clock seconds] -format "%H%M" -gmt 1]
    dsDrawScreen
}


proc dsLookupCountry {} {
    global ds at

    set callsign $ds(f3)

    set matchCountries {}
    while {1} {
	foreach i $ds(prefixes) {
	    if {[string match "*:$callsign:*" [lindex $i 1]]} {
		lappend matchCountries [lindex $i 0]
	    }
	}
	if {[llength $matchCountries]} break
	
	set callsign [string range $callsign 0 end-1]
	if {! [string length $callsign]} {
	    curpos 25 4
	    dsPuts "$at(prompt)No match found"
	    after 1000
	    dsDrawScreen
	    return
	}
    }

    set ndx 0
    if {[llength $matchCountries] > 1} {
	curpos 22 4
	puts -nonewline "$at(normal)"
	set n 1
	foreach i $matchCountries {
	    puts -nonewline "($n) $i  "
	    incr n
	}
	curpos 25 4
	dsPuts "$at(prompt)Multiple choices, please select one: $at(normal)"
	gets stdin n
	set n [string trimleft $n 0]
	if {(! [string is digit -strict $n]) || ($n > $matchCountries)} {
	    dsDrawScreen
	    return
	}
	set ndx [expr {$n - 1}]
    }
    set ds(f6) [lindex $matchCountries $ndx]
    dsDrawScreen
}


proc dsCheckCountry {} {
    global ds at

    set found 0
    set confirmed 0
    foreach rec $ds(recs) {
	if {[string equal $ds(f6) [lindex $rec 6]]} {
	    set found 1
	    if {[lindex $rec 13] == "yes"} {
		set confirmed 1
		break
	    }
	}
    }

    curpos 25 4
    set txt "$at(normal)"
    if {! $found} {
	append txt "'$ds(f6)' is a new country"
    } elseif {! $confirmed} {
	append txt "'$ds(f6)' worked but not confirmed"
    } else {
	append txt "'$ds(f6)' worked and confirmed"
    }
    dsPuts $txt
    getkey
    dsDrawScreen
}


proc dsShowCountryData {} {
    global ds at

    foreach rec $ds(countries) {
	if {[string equal $ds(f6) [lindex $rec 0]]} {
	    cls $at(normal)
	    set titles {Country Continent {ITU Zone} {CQ Zone} \
		    {Time Zone} Latitude Longitude}
	    set n 0
	    curpos 4 1
	    foreach i $titles {
		set line "$at(title)[format "%12s" $i]   "
		append line "$at(normal)[lindex $rec $n]"
		puts $line
		incr n
	    }
	    curpos 25 4
	    dsPuts "$at(prompt)Press any key to exit $at(normal)"
	    getkey
	    dsDrawScreen
	    break
	}
    }
}


proc dsSort {} {
    global ds at

    curpos 25 4
    dsPuts "$at(prompt)Sort on current field, no for normal sort (y/N)? $at(normal)"
    if {[yesno 0]} {
	set n [dsAbsField]
	set ds(recs) [lsort -index $n $ds(recs)]
    } else {
	set ds(recs) [lsort -index 1 $ds(recs)]
	set ds(recs) [lsort -index 0 $ds(recs)]
    }
    dsDrawScreen
}


proc dsExInit {} {
    global ds

    set f [open @dbdir@/prefix.dat r]
    read $f 1
    set ds(prefixes) [read $f]
    close $f    

    set f [open @dbdir@/country.dat r]
    read $f 1
    set ds(countries) [read $f]
    close $f
}
.
