# Provides for deducing the location of Tkabbur config directory depending
# on the current platform.

namespace eval config {}

# Deduces the location of the "Application Data" directory
# (in its wide sense) on the current Windows platform.
# See: http://ru.tkabber.jabe.ru/index.php/Config_dir
proc config::appdata_windows {} {
    global env

    if {[info exists env(APPDATA)]} {
	return $env(APPDATA)
    }

    if {![catch {package require registry}]} {
	set key {HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders}
	if {![catch {registry get $key AppData} dir]} {
	    return $dir
	}
    }

    return {}
}

# Based on the current platform, chooses the location of the Tkabber's
# config dir and sets the "configdir" global variable to its pathname.
# "TKABBER_HOME" env var overrides any guessing.
# NOTE that this proc now tries to copy contents of the "old-style"
# ~/.tkabber config dir to the new location, if needed, to provide
# smooth upgrade for Tkabber users on Windows.
# This behaviour should be lifted eventually in the future.

if {![info exists env(TKABBER_HOME)]} {
    switch -- $tcl_platform(platform) {
	unix {
	    set configdir ~/.tkabbur
	}
	windows {
	    set dir [config::appdata_windows]
	    if {$dir != {}} {
		set configdir [file join $dir Tkabbur]
	    } else {
	    # Fallback default (depends on Tcl's idea about ~):
	    set configdir [file join ~ .tkabbur]
	    }
	}
	macintosh {
	    set configdir [file join ~ Library "Application Support" Tkabbur]
	}
    }

    set env(TKABBER_HOME) $configdir
} else {
    set configdir $env(TKABBER_HOME)
}

if {$tcl_version >= 8.4} {
    set configdir [file normalize $configdir]
}

file mkdir $configdir

# vim:ts=8:sw=4:sts=4:noet
