QuietEngineer's dotfiles: just add water
1#!/bin/sh
2
3# When the environment variable DRYRUN is non-empty, do not
4# actually make any changes, but only show what would be done
5
6set -e
7
8echodo() {
9 echo $@
10 [ 0"$DRYRUN" = "0" ] && $@ || true
11}
12
13linkToHome() {
14 if [ $# -lt 2 ]; then
15 echo Usage: $0 SOURCE_NAME DEST_NAME
16 else
17 SOURCE_NAME=$1
18 DEST_NAME=$HOME/$2
19
20 if [ -h $DEST_NAME ]; then
21 if [ "$(readlink $DEST_NAME)" != "$SOURCE_NAME" ]; then
22 echo "$DEST_NAME is already a symlink which doesn't point here"
23 else
24 echo "OK: $DEST_NAME -> $SOURCE_NAME"
25 fi
26
27 elif [ -d $DEST_NAME ]; then
28 echo $DEST_NAME already exists and is a directory
29
30 elif [ -b $DEST_NAME ]; then
31 echo $DEST_NAME already exists as a block special
32
33 elif [ -c $DEST_NAME ]; then
34 echo $DEST_NAME already exists as a character special
35
36 elif [ -p $DEST_NAME ]; then
37 echo $DEST_NAME already exists as a named pipe
38
39 elif [ -S $DEST_NAME ]; then
40 echo $DEST_NAME already exists as a socket
41
42 elif [ -f $DEST_NAME ]; then
43 echo $DEST_NAME already exists as a regular file
44
45 elif [ -e $DEST_NAME ]; then
46 echo $DEST_NAME already exists
47 else
48 echodo ln -s $SOURCE_NAME $DEST_NAME
49 fi
50 fi
51}
52
53removeLink() {
54 DEST_NAME=$HOME/$1
55
56 if ! [ -h $DEST_NAME ]; then
57 echo "'$DEST_NAME' is not a symlink, skipping..."
58 else
59 echodo rm $DEST_NAME
60 fi
61}
62
63# make sure that $HOME is defined
64if [ 0"$HOME" = "0" ]; then
65 echo "HOME is empty or unset!"
66 exit 1
67fi
68
69if [ 0"$DRYRUN" != "0" ]; then
70 echo ====================
71 echo THIS IS A DRY RUN!!!
72 echo ====================
73 echo
74fi
75
76if [ 0"$1" = 0"-r" ]; then
77 # Clean up old symlinks
78 removeLink .bash
79 removeLink .bashrc
80 removeLink .bash_profile
81else
82 # Resolve the location of this script
83 HERE=$(dirname $(readlink -f $0))
84
85 # Link these files and directories into $HOME
86 linkToHome $HERE/.bash .bash
87 linkToHome $HERE/.bashrc .bashrc
88 linkToHome $HERE/.bash_profile .bash_profile
89fi