# output <link> for "file" pages
proc file_link {} {
    upvar 1 current_page current_page
    set name [getParameter name]
    set ci [getParameter ci]%
    set sql_bynameci {
      SELECT b_artifact.uuid AS uuid
        FROM blob b_checkin LEFT JOIN mlink ON mid=b_checkin.rid
        LEFT JOIN filename on mlink.fnid=filename.fnid
        LEFT JOIN blob b_artifact ON mlink.fid=b_artifact.rid
       WHERE b_checkin.uuid LIKE $ci AND name = $name
       ORDER BY b_artifact.rid DESC
    }
    #html "<!-- FL($current_page,name=$name,ci=$ci) -->\n"
    if {[regexp  "^doc/(tip|trunk)/.+" $current_page]} {
        # doc/tip/file.txt (same for doc/ access and file?name=fn.txt browsing)
        set sql "$sql_bynameci LIMIT 1"
        if {[regexp  "^(tip/)" $name]} { set name [string range $name 4 200] }
        if {[regexp  "^(trunk/)" $name]} { set name [string range $name 6 200] }
    } elseif {"$current_page" eq "finfo"} {
        if {"$ci" ne "%"} {
            # finfo?name=file.txt&ci=1234f
            set sql $sql_bynameci
        } else {
            # finfo?name=file.txt
            set sql "$sql_bynameci LIMIT 1"
        }
    } elseif {[regexp  "^doc/\\w+/.+" $current_page]} {
        if {"$ci" ne "%"} {
            # doc/1234f/file.txt
            set sql $sql_bynameci
        } else {
            # /info/1234f  # should assert that it refers to a file blob?
            set name "$name%"
            set sql {SELECT uuid FROM blob where uuid LIKE $name}
        }
    } else {
        #html "<!--nocasematch-->"
    }
    if {[info exists sql]} {
        #html "<!-- = $sql -->"
        set top [globalState top]
        query $sql {
            html "<link rel=\"alternative vcs raw\" href=\"$top/raw/$uuid\">"
        }
    }
}
