49 lines
887 B
Bash
Executable File
49 lines
887 B
Bash
Executable File
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
function symlink() {
|
|
local target="$1"
|
|
local link="$2"
|
|
|
|
local arrow="$link -> $target"
|
|
|
|
local current_target
|
|
current_target=$(readlink "$link")
|
|
|
|
# link is already correct
|
|
if [ -L "$link" ] && [ "$current_target" == "$target" ]; then
|
|
echo "Symlink ($arrow) already exists"
|
|
return 0
|
|
fi
|
|
|
|
# link exists but is not a symlink
|
|
if [ -e "$link" ] && [ ! -L "$link" ]; then
|
|
echo "Error: $link exists but is not a symlink"
|
|
exit 1
|
|
fi
|
|
|
|
# link points to wrong target
|
|
if [ -L "$link" ]; then
|
|
echo "Symlink $link points to the wrong target: $current_target"
|
|
rm "$link"
|
|
fi
|
|
|
|
ln -s "$target" "$link"
|
|
echo "Symlink ($arrow) created"
|
|
}
|
|
|
|
|
|
if [ $# -ne 2 ]; then
|
|
echo "Usage: $0 <target> <link>"
|
|
exit 1
|
|
fi
|
|
|
|
target="$1"
|
|
link="$2"
|
|
|
|
symlink "$target" "$link"
|
|
|
|
|