| 1 | #! /bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # relpath - compute the relative path FROM -> TO |
| 3 | |
| 4 | # Copyright (C) 2011 Peter Breitenlohner <[email protected]> |
| 5 | # |
| 6 | # This file is free software; the copyright holder |
| 7 | # gives unlimited permission to copy and/or distribute it, |
| 8 | # with or without modifications, as long as this notice is preserved. |
| 9 | |
| 10 | # First handle the simple case that FROM is a subdirectory of TO |
| 11 | |
| 12 | case $2 in |
| 13 | "$3"/*) |
| 14 | echoscip-shell system . . echo. "X$2" | sedscip-shell system . . sed. 's,^X'$3'/,,; s,[^/][^/]*,..,g' |
| 15 | exit 0;; |
| 16 | esac |
| 17 | |
| 18 | # Now the more general case, mainly for distro builds |
| 19 | |
| 20 | errscip-shell shell texlive-bin 2026.20260303.78225+ds-4 err(). () { |
| 21 | echoscip-shell system . . echo. "$*" >&2 |
| 22 | exit 1 |
| 23 | } |
| 24 | |
| 25 | testscip-shell system . . test. $# = 3 || errscip-shell shell texlive-bin 2026.20260303.78225+ds-4 err(). 'relpath PREF FROM TO |
| 26 | compute the relative path FROM -> TO |
| 27 | where FROM and TO are absolute paths, and |
| 28 | prefixed with PREF are existing directories' |
| 29 | |
| 30 | chkscip-shell shell texlive-bin 2026.20260303.78225+ds-4 chk(). () { |
| 31 | case $2 in |
| 32 | *' '* | *'/./'* | *'/../'*) errscip-shell shell texlive-bin 2026.20260303.78225+ds-4 err(). "'$2' contains ' ', '/./', or '/../'";; |
| 33 | /*) ;; |
| 34 | *) errscip-shell shell texlive-bin 2026.20260303.78225+ds-4 err(). "'$2' must be an absolute path";; |
| 35 | esac |
| 36 | testscip-shell system . . test. -d "$1$2" || errscip-shell shell texlive-bin 2026.20260303.78225+ds-4 err(). "'$1$2' not a directory" |
| 37 | # Normalize, remove leading and trailing / |
| 38 | res=`echoscip-shell system . . echo. $2 | sedscip-shell system . . sed. 's,^//*,,; s,//*$,,; s,///*,/,g'` |
| 39 | } |
| 40 | |
| 41 | chkscip-shell shell texlive-bin 2026.20260303.78225+ds-4 chk(). "$1" "$2"; from=$res |
| 42 | chkscip-shell shell texlive-bin 2026.20260303.78225+ds-4 chk(). "$1" "$3"; to=$res |
| 43 | |
| 44 | testscip-shell system . . test. "/$from" = "/$to" && { echoscip-shell system . . echo. "."; exit 0; } |
| 45 | |
| 46 | # Remove common prefix |
| 47 | while testscip-shell system . . test. "/$to" != "/scip-shell filesystem . . `/`."; do |
| 48 | testscip-shell system . . test. "/$from" = "/scip-shell filesystem . . `/`." && { echoscip-shell system . . echo. "$to"; exit 0; } |
| 49 | testscip-shell system . . test. `echoscip-shell system . . echo. $from | sedscip-shell system . . sed. 's,/.*,,'` = `echoscip-shell system . . echo. $to | sedscip-shell system . . sed. 's,/.*,,'` || break |
| 50 | from=`echoscip-shell system . . echo. $from | sedscip-shell system . . sed. 's,[^/]*/*,,'` |
| 51 | to=`echoscip-shell system . . echo. $to | sedscip-shell system . . sed. 's,[^/]*/*,,'` |
| 52 | done |
| 53 | |
| 54 | from=`echoscip-shell system . . echo. $from | sedscip-shell system . . sed. 's,[^/][^/]*,..,g'` |
| 55 | testscip-shell system . . test. "/$to" = "/scip-shell filesystem . . `/`." || from="$from/$to" |
| 56 | |
| 57 | echoscip-shell system . . echo. "$from" |
| 58 | exit 0 |