#!/bin/bash
# Desc: Moves sybolic link
# Usage: bkmvln
# Input: arg1: path of existing symbolic link
#        arg2: path where link should be moved
# Output: None
# Depends: gnucoreutils 8.30
# Ref/Attrib:
#  1. Christopher Neylan. https://stackoverflow.com/questions/8523159/how-do-i-move-a-relative-symbolic-link#8523293
#  2. Tomáš Pospíšek. https://stackoverflow.com/a/30508868

help() {
   echo 'usage: mv_ln src_ln dest_dir'
   echo '       mv_ln --help'
   echo
   echo '  Move the symbolic link src_ln into dest_dir while'
   echo '  keeping it relative'
   exit 1
}

# Show help if `--help` flag given, if arg1 isn't a symbolic link, or
#   if arg2 isn't a directory.
[ "$1" == "--help" ] || [ ! -L "$1" ] || [ ! -d "$2" ] && help

set -e # exit on error

orig_link="$1"
orig_name=$( basename    "$orig_link" )
orig_dest=$( readlink -f "$orig_link" )
dest_dir="$2"

ln -r -s "$orig_dest" "$dest_dir/$orig_name"
rm "$orig_link"