#! /usr/bin/gawk -f # Last edited on 2012-04-30 01:18:34 by stolfi # Usage: "make-fix-rel-link-command.gawk -v link={LINK} -v target={TARGET}" # Given a symbolic link pathname and its target pathname, # both relative to the same root directory. # outputs the command that creates a relative symbolic link using "../" # If data is invalid, complains to {stderr} and outputs nothing. BEGIN { if (link ~ /^[/]/) { printf "invalid link\n" > "/dev/stderr"; } if (target ~ /^[/]/) { printf "invalid target\n" > "/dev/stderr"; } dir=""; tdir = ""; while (1) { match(link, /[/]/); ap = RSTART; match(target, /[/]/); bp = RSTART; if (ap == 0) { printf "cd %s && ln -s %s%s %s \n", dir, tdir, target, link; exit(0); } link_head = substr(link,1,ap); if (link_head == "./") { link = substr(link,ap+1); } else if (link_head == "../") { printf "invalid link\n" > "/dev/stderr"; } else { dir = (dir link_head); link = substr(link,ap+1); target_head = substr(target,1,bp); if (target_head == "./") { target = substr(target,bp+1); } else if (target_head == "../") { printf "invalid target\n" > "/dev/stderr"; } else { if (link_head == target_head) { target = substr(target,bp+1); } else { tdir = (tdir "../"); } } } } }