aboutsummaryrefslogtreecommitdiff
path: root/updatedots
blob: a4d12994eea8d6850ad6c115852c1d98828d8461 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
# Simple dotfile linking script.
# Partly inspired by dotsync (https://github.com/dotphiles/dotsync).
#
# This script takes files in a version-controllable "dotdir" directory
# and creates corresponding symlinks in the user's home directory.
# Any existing files in the home directory are backed up before being
# replaced with a symlink.

# Root directory of dotfiles project
BASEDIR=$(dirname "$(readlink -f "$0")")

# Directory containing all dotfiles that will be linked to $HOMEDIR
DOTDIR=${BASEDIR}/home

# Directory into which files will be linked
HOMEDIR=${HOME}

# Directory into which any pre-existing dotfiles will be copied
BACKUPDIR=${HOMEDIR}/.updatedots-backup

# Dotfiles relative to $DOTDIR
RDOTFILES=$(find ${DOTDIR} -type f -print | sed "s|^${DOTDIR}/||g")

# Command used to create symbolic links
LINK="ln --symbolic --relative"

# Publish dotfiles to home directory, backing up any collisions
create_links() {
    if [ ! -e "$BACKUPDIR" ]; then
	mkdir "$BACKUPDIR"
    fi
    if [ -n "$(ls -A $BACKUPDIR)" ]; then
	echo "Backup directory $BACKUPDIR is not empty, local files could be lost." >&2
	echo "Aborting." >&2
	exit 1
    fi

    local moved=0

    echo "*** Linking dotfiles from $(realpath $DOTDIR) to $HOMEDIR ***"
    for relative in $RDOTFILES; do
	local src=$DOTDIR/$relative
	local dest=$HOMEDIR/$relative

	# backup destination (if the file already exists and is backed up)
	local bdest=$BACKUPDIR/$relative

	mkdir -p $(dirname "$dest")

	if [ -e "$dest" ] && [ $(readlink -f "$dest") = $(readlink -f "$src") ]; then
	    echo "Nothing to be done for $relative"
	else
	    if [ -e "$dest" ]; then
		mkdir -p $(dirname "$bdest")
		mv "$dest" "$bdest"
		moved=1
		echo "Backed up original $relative to $bdest"
	    fi

	    $LINK "$src" "$dest"
	    echo "Linked $relative"
	fi
    done

    echo "*** Finished linking dotfiles ***"
    echo
    if [ "$moved" -gt 0 ]; then
	echo "WARNING: some files were moved to $BACKUPDIR" >&2
    fi
}

create_links