-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sh
executable file
·139 lines (119 loc) · 2.88 KB
/
init.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# Author: Guillermo Pérez <[email protected]>
#
# Initialization script for sweethome
#
# It backups the present files and replaces them with
# links to the sweethome repository.
if [ -z "$BASH_VERSION" ]; then
echo "Execute this script with bash, please"
exit 1;
fi
# Files in the repo that should not be linked directly into the home
EXCLUDES="^\(dotfiles\|.*\.sh\|.*\.md\)$"
function listRepoFiles {
listDirFiles "dotfiles/"
listDirFiles "private/dotfiles/"
listDirFiles "" "$EXCLUDES"
}
function listHomeFiles {
listRepoFiles | calculateFileDestination
}
function listDirFiles {
local DIR=$1
local EXCLUDES=$2
local name
ls "$REPO_PATH/$DIR" |
( test -n "$EXCLUDES" && grep -ve "$EXCLUDES" || cat - ) |
while read name; do
echo "$DIR$name"
done
}
function calculateFileDestination {
local PRINTBOTH=$1
local name
while read name; do
[ -n "$PRINTBOTH" ] && echo "$name"
if [ -d $REPO_PATH/$name ]; then
# For directories:
# Replace all ocurrences of '.' with '/'
name="${name//\./\/}"
fi
# Replace starting substring 'dotfiles/' with '.'
name="${name/#dotfiles\//.}"
# Replace starting substring 'private/dotfiles/' with '.'
name="${name/#private\/dotfiles\//.}"
# Replace starting substring 'private' with '.private'
name="${name/#private/.private}"
echo "$name"
done
}
function linkFiles {
local BACKUP=$(tempfile -d ~/ -p 'sweet' -s '.tgz')
local src
local dest
echo
echo "* Creating backup..."
tar cfz $BACKUP $(listHomeFiles) 2>/dev/null
echo " You can find old files in $BACKUP"
echo
echo "* Linking files..."
listRepoFiles | calculateFileDestination 1 | while read src; do
read dest
echo " - Linking ~/$dest -> $SHORT_REPO_PATH/$src"
src="$REPO_PATH/$src"
dest="$HOME/$dest"
rm -rf $dest
mkdir -p "$(dirname $dest)"
ln -s "$src" "$dest"
done
}
function preInstall {
git submodule init
git submodule update
}
function postInstall {
echo "Done!"
echo
echo "Now we will launch vim bundle installation."
echo -n "Press ENTER to continue..."
read
vim +BundleInstall! +BundleClean +q +q
echo
}
function define_repo_path() {
# Extract script path (it's the repo path)
REPO_PATH=$(dirname "${BASH_SOURCE[0]}");
REPO_PATH=$(readlink -f "$REPO_PATH");
SHORT_REPO_PATH="${REPO_PATH/#$HOME/~}"
}
define_repo_path
echo
echo "** Initializing home directory with 'sweethome' repo in $REPO_PATH"
echo
echo "The following files will be REPLACED by symbolic links to this"
echo "sweethome repo:"
echo
listRepoFiles | calculateFileDestination 1 | while read src; do
read dest
echo " - ~/$dest -> $SHORT_REPO_PATH/$src"
done
echo
echo "Note: A Backup of the original files will be created."
echo "Do you want to continue?"
while true; do
echo -n "(y/n): ";
read response;
case "$response" in
[yY])
preInstall
linkFiles
postInstall
break;
;;
[nN])
echo "Aborted!";
break;
;;
esac
done