#
# <20161029.1738.10>

# This bit is code is to find the ".fr" directory for the
# current user.  In the past we have required this to be
# in the env(HOME) directory.  These days linux has move
# a lot of this stuff to env(HOME)/.config.
#
# On windows, env(HOME) is not usually configured and we
# have been forcing it. There is also the various locations
# that come from "cygwin" and such.  For windows we will
# start with the following:
# HOME
# USERPROFILE
# HOMEDRIVE/HOMEPATH
# APPDATA 
# LOCALAPPDATA
#
proc findFrDir {} {
  global env tcl_platform

  # different strokes for windows...
  #
  if {$tcl_platform(platform) != "windows"} {
    # First check the best locations...
    set quick [list $env(HOME)/.config/.fr $env(HOME)/.fr]
    set root $env(HOME)
  } else {
    # for windows we prefer the roaming app dir
    # use file norm to get rid of the nasty backslashes
    set quick [list [file norm $env(APPDATA)/.fr] \
		   [file norm $env(LOCALAPPDATA)/.fr]\
		   [file norm $env(USERPROFILE)/.fr ]]
    set root [file norm $env(USERPROFILE)]
  }
  # Try the quick..
  foreach pos $quick {
    if {[file isdir $pos]} {
      return $pos
    }
  }
  # Maybe later we can look more, but for now take the easy way..
  return [lindex $quick 0]

  
  # # getting hard to find...
      
  # set pos [::realfind::realFind $root [list -H -name .fr -maxdepth 4]]
  # if {$pos == {}} {return [lindex $quick 0]
      
  #   if {[set ln [llength $pos]] == 1} {
  #     return $pos
  #   }
  #   # which one to choose????
  #   # base it on mtime...
    
  #   set mtime 0
  #   for {set i 0} {i < $ln} {incr i} {
  #     set nt [file mtime [lindex $pos $i]]
  #     if {$nt > $mtime} {
  # 	set mtime $nt
  # 	set this $i
  #     }
  #   }
  #   return [file norm [lindex $pos $this]]
  # }
}
