diff --git a/autodeploy/symlink.sh b/autodeploy/symlink.sh new file mode 100755 index 0000000..569bcbd --- /dev/null +++ b/autodeploy/symlink.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +function symlink() { + local target="$1" + local link="$2" + + local arrow="$link -> $target" + local 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 " + exit 1 +fi + +target="$1" +link="$2" + +symlink "$target" "$link" + +