# output <link> for "file" pages
proc file_link {} {
    upvar 1 current_page current_page
    set name [getParameter name]
    set ci [getParameter ci]%
    set top [globalState top]
    set sql_byname  {
       SELECT uuid         FROM blob LEFT JOIN mlink ON blob.rid=mlink.fid         LEFT JOIN filename ON mlink.fnid=filename.fnid
        WHERE name = $name        ORDER BY rid DESC LIMIT 1
    }
    set sql_bynameci {
       SELECT uuid FROM blob
        WHERE rid IN (
              SELECT fid
                FROM blob LEFT JOIN mlink ON mid=blob.rid
                LEFT JOIN filename on mlink.fnid=filename.fnid
               WHERE uuid LIKE $ci AND name = $name
               ORDER BY rid DESC
        )
    }
    html "<!-- FL($current_page,name=$name,ci=$ci) -->\n"
    if {[regexp  "^doc/(tip|trunk)/.+" $current_page]} {
        # doc/tip/file.txt
        set sql $sql_byname
    } 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_byname"
            #ci 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 "<!--nomatch-->" }
    if {[info exists sql]} {
        html "<!-- = $sql -->"
        query $sql {
            html "<link rel='alternative vcs raw' href='$top/raw/$uuid'>"
        }
    }
}
