Last active
November 22, 2020 14:25
-
-
Save paulojeronimo/3464617b9253d890fdf6c2afc3f0f928 to your computer and use it in GitHub Desktop.
Script create-my-links #bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eou pipefail | |
! [[ `uname` = Darwin ]] || { | |
command -v greadlink &> /dev/null || { | |
echo "Install greadlink! ($ brew install coreutils)" | |
exit 1 | |
} | |
readlink() { greadlink "${@:-}"; } | |
} | |
BASE_DIR=`dirname "$(readlink -m "$(which "$0")")"` | |
source ~/.create-my-links 2>&- || : | |
ORIGIN_DIR=${ORIGIN_DIR:-/mnt/pj} | |
MY_LINKS_FILE=${MY_LINKS_FILE:-$HOME/.my-links} | |
field() { cut -d'|' -f$1 <<< $2; } | |
remove-link() { rm -f ~/"$1"; } | |
create-link() { | |
local link=$1; shift | |
[ -e "$link" ] && { | |
[ "${@:-}" ] && { | |
rm -f "${@##*/}/$link" | |
ln -sf "$link" "$@" | |
} || { | |
rm -f "${link##*/}" | |
ln -sf "$link" | |
} | |
} || { | |
echo "Link \"$link\" could no be read!" >&2 | |
} | |
} | |
link-not-created() { | |
echo "A link to \"$1\" could not be created in \"$2\"! Directory \"$3\" does not exists!" | |
} | |
create=true | |
while getopts ":ro:" opt | |
do | |
case $opt in | |
r) create=false;; | |
o) ORIGIN_DIR=$OPTARG;; | |
:) echo "Option \"-$OPTARG\" requires an argument!" >&2;; | |
\?) | |
echo "Usage: $0 [-r] [-o] \"origin-dir\"" >&2 | |
exit 0 | |
;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
[ -e "$MY_LINKS_FILE" ] || { | |
echo "File \"$MY_LINKS_FILE\" does not exists! Linking it to \"${BASE_DIR#$HOME/}/my-links\" ..." | |
(cd; ln -sf "${BASE_DIR#$HOME/}"/my-links .my-links) | |
} | |
while IFS= read -r link | |
do | |
link=$(echo -n $link) | |
[ ! "$link" ] || [[ "$link" =~ ^# ]] && continue | |
[[ "$link" =~ '|' ]] && { | |
target=`field 2 "$link"` | |
link=`field 1 "$link"` | |
[[ "$link" =~ '/' ]] && { | |
$create && (cd ~/"${link%/*}" 2>&- && create-link ~/"$target" "${link##*/}" || | |
link-not-created "~/$target" "~/${link%/*}" "${link%/*}") || remove-link "$link" | |
} || { | |
$create && (cd; create-link "$target" "$link") || remove-link "$link" | |
} | |
} || { | |
$create && { | |
[ -d "$ORIGIN_DIR" ] || { | |
link-not-created "$ORIGIN_DIR/$link" "~" "$ORIGIN_DIR" | |
continue | |
} | |
(cd; create-link "$ORIGIN_DIR/$link") | |
} || remove-link "$link" | |
} | |
done < "$MY_LINKS_FILE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configuration file used by the script "create-my-links". | |
# Copy it to "$HOME/" and configure it as your needs. | |
# "Origin links" (links to existing directories in $ORIGIN_DIR and created inside $HOME): | |
.m2 | |
cs | |
labs | |
tmp | |
My Projects | |
Projects | |
# "Local links" (links to "Origin links" or to other existing "Local links"): | |
log|My Projects/github.com/log | |
Desktop/log|log | |
# vim: syntax=off |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment