#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

# Create symlink after backing up the target.

[[ $# != 2 ]] && echo "Usage: ${0##*/} source_file target_file" && exit 1
src=$1
tgt=$2
bak=~/.backup

mkdir -p $bak

# Check if the desired link already exists.
[[ -L "$tgt" && "$(readlink $tgt)" = "$src" ]] && exit

# Move an existing file to the back up folder.
[[ -e "$tgt" ]] && mv -v "$tgt" "$bak/${tgt##*/}-$(date '+%Y%m%d-%H%M%S')" || true

ln -sfv "$src" "$tgt"
