#
# <20220206.1446.51>
#
package require twapi


#
proc TranslateLnk {link curMountPt} {
  global glob
  set filePrefix {}
  frputs "[pwd] " link curMountPt
  switch -glob $link {
    {?:/*} {
      set first [string toupper [string range $link 0 0]]
      set glob(volume,$first) [file volumes]
      set file $link
    }
    {//*} {
      # Lets assume that it is mounted with the same name
      set file [string range $link 1 end]
    }
    {*.url}   {return $link}
    {./*}     {set file $link }
    {/*}      {
      # Absolute path. To translate we need to add the current wsl (if one)
      lassign [file split [pwd]] wslId
      if {[string match {//wsl$/*} $wslId]} {
	set file [string map $::findUNC::wslMswMap $wslId$link]
      } else {
	set file $link
      }
    }
    default   {set file "./$link" }
  }
  set file [file norm $file]
  # check if wsl and if reference is to a non-wsl file
  if {[string range $file 0 6] == {//wsl$/} && [info exists ::findUNC::wslMswMap]} {
    frputs file
    set file [string map $::findUNC::wslMswMap $file]
  }
  # Check if we found the file...
  frputs file link curMountPt
  if {![file exists $file]} {
    # if this is a ?:/ file and we have a previous working volume, try that
    if {$filePrefix != {} && [info exists glob(volume,$first)]} {
      foreach vol $glob(volume,$first) {     
	if {[file exists [set file $vol/[string range $link 3 end]]]} {
	  return $file
	}
      }
    }
    return {}
  } elseif {$filePrefix != {}} {
    # Allow several prefix to volume translations but only
    # one copy of a given transation.
    if {![info exists glob(volume,$first)] || \
	    $filePrefix ni $glob(volume,$first)} {
      lappend glob(volume,$first) $filePrefix
    }
  }
  return $file
}

# The following tries to make sense of //wsl$/ link files.
# these files, while indicating as 'link' are not decoded by the
# tcl file code. They also return as ! dir for all cases.
# Here we try to make better sense of this by looking at the version
# in the users App directory.
#
proc getWslLink {dir file} {
  # First make sure it is a wsl file and ids as link
  if {![file exists $dir/$file]} {
    return [list "File not findable" "l" ]
  }
  if {[string range $dir 0 6] != "//wsl$/" ||\
	  [file type $dir/$file] != "link" } {
    frputs 
    return -code error "Not a WSL link file."
  }
  # There are two possibilities for a link that points to a MSW file
  # a relative link, in which case the normalized result leads with //wsl$
  # and absolute, in which case the result does not lead with //wsl$
  set fname [string map $findUNC::wslNativeMap $dir]/$file
  frputs fname dir
  set r [catch {read [set fid [open $fname r]]} lnk]
  close $fid
  lassign [file split $dir] wslId
  set testFile $lnk
  if {[string index $lnk 0] == "/"} {
    set testFile $wslId/$lnk
  }
  set testFile [string map $::findUNC::wslMswMap [file norm $testFile]]
  frputs r lnk testFile
  if {$r == 0} {
    return [list $lnk\
		[expr {[file isdir $testFile] ? "wld" : "wl" }]]
  }
  return [list "Could not decode link" "l" ]
}








