Skip to content

Instantly share code, notes, and snippets.

@sciurus
Created March 18, 2013 03:43
Show Gist options
  • Save sciurus/5184930 to your computer and use it in GitHub Desktop.
Save sciurus/5184930 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo_error () {
echo "Error: $@" 1>&2
}
if [ $(uname) == 'Darwin' ]; then
rlink='greadlink'
else
rlink='readlink'
fi
if [ "$1" = '-n' ]; then
OPERATION='dry_run'
else
OPERATION='go_for_it'
fi
set -u
# detect if current working directory is the same
# as the directory this script is in
if [ "$(dirname $($rlink -e $0))" != "$($rlink -e $(pwd))" ]; then
echo_error 'Your current working directory must be your dotfiles repository'
exit 1
else
REPO="$($rlink -e $(pwd))"
fi
create_links () {
for T in $REPO/*; do
TARGET=$($rlink -e $T)
LINK_NAME="$HOME/.$(basename $TARGET)"
# test if a file, working symlink, or broken symlink already exists
if [ -e $LINK_NAME -o -L $LINK_NAME ]; then
CURRENT_TARGET=$($rlink -m $LINK_NAME)
if [ $CURRENT_TARGET != $TARGET ]; then
echo_error "$LINK_NAME already exists (and is really $CURRENT_TARGET)"
fi
else
if [ $OPERATION = 'dry_run' ]; then
echo ln -s $TARGET $LINK_NAME
else
ln -s $TARGET $LINK_NAME
fi
fi
done
}
remove_stale_links () {
for L in $(find $HOME -maxdepth 1 -type l -name '.*'); do
if [ ! -e $L ]; then
if [ $OPERATION = 'dry_run' ]; then
echo rm $L;
else
rm $L
fi
fi
done
}
create_links
remove_stale_links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment