| 1 | #! /bin/bash |
| 2 | |
| 3 | # create a symlink, idempotent |
| 4 | # |
| 5 | # (c) Thomas Lange 2019, [email protected] |
| 6 | # |
| 7 | # if $target is defined, create link relative to $target |
| 8 | |
| 9 | mk-link() { |
| 10 | |
| 11 | |
| 12 | local from=$1 |
| 13 | local to=$2 |
| 14 | local oldto |
| 15 | local vopt |
| 16 | |
| 17 | if [ X$verbose = X1 ]; then |
| 18 | vopt=-v |
| 19 | fi |
| 20 | |
| 21 | # todo: check if $from starts with a slash |
| 22 | |
| 23 | if [ -n "$target" ]; then |
| 24 | from="$target$from" |
| 25 | fi |
| 26 | |
| 27 | if [ ! -e $from ] && [ ! -h $from ]; then |
| 28 | ln -s $vopt $to $from |
| 29 | return |
| 30 | fi |
| 31 | |
| 32 | # check if already same link |
| 33 | if [ -h $from ]; then |
| 34 | oldto=$(readlink $from) |
| 35 | if [ $oldto = $to ]; then |
| 36 | if [ X$verbose = X1 ]; then |
| 37 | printf "fai-link: Nothing to do for link $from -> $to\n" |
| 38 | fi |
| 39 | return |
| 40 | fi |
| 41 | fi |
| 42 | |
| 43 | # if different create new link |
| 44 | ln -sf $vopt $to $from |
| 45 | } |
| 46 | |
| 47 | # make link from to |
| 48 | mk-link $1 $2 |