#!/bin/sh # Symlink is a small script to basically make `ln -s` less of a pain in the ass. # This script automagically figures out which direction you mean, and allows you to # pass in two actual paths (which works with tab completion) and specify if you mean # to make a relative or absolute link (relative is assumed by default). # # Usage: symlink [-a|--absolute] path1 path2 # absolute="n" if [ "$1" = "-a" ] || [ "$1" = "--absolute" ]; then absolute="y" shift fi if [ $# -ne 2 ]; then echo "Usage: symlink [-a|--absolute] " exit 1 fi if [ -e "$1" ] && [ -e "$2" ]; then printf "Both \x1b[1m%s\x1b[0m and \x1b[1m%s\x1b[0m already exist. Please delete one of them before proceeding.\n" "$@" exit 1 elif ! [ -e "$1" ] && ! [ -e "$2" ]; then printf "\x1b[31mWarning: Neither \x1b[1m%s\x1b[22m nor \x1b[1m%s\x1b[22m exist!\x1b[0m\n" "$@" choice=$(printf "%s\n" "1. $1 -> $2" "2. $2 -> $1" "3. Whoops that was a mistake" | fzy -p "Which do you mean? " | awk -F. '{print $1}' || exit 1) case $choice in 1) linkname="$1"; target="$2";; 2) linkname="$2"; target="$1";; *) exit 1;; esac elif [ -e "$1" ]; then linkname="$2"; target="$1" else linkname="$1"; target="$2" fi if [ $absolute = "y" ]; then target="$(realpath "$target")" else target="$(realpath --relative-to="$(dirname "$linkname")" "$target")" fi printf "Linking \x1b[1m%s\x1b[0m -> \x1b[1m%s\x1b[0m Is this okay?" "$linkname" "$target" ask -y || exit 1 echo ln -sv -T "$target" "$linkname"