-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall
executable file
·154 lines (135 loc) · 4.51 KB
/
install
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
declare -r self="${0##*/}"
declare DOTFILES_DIR="$(cd "$(dirname "$0")" && echo "$PWD")"
declare -r LINKS=( \
asdfrc \
bash_profile bashrc \
dircolors gvimrc \
gitignore gitconfig.std \
git_identities \
irbrc \
terminfo \
todo.cfg todo.actions.d \
vim vimrc \
pastebin.d \
railsrc \
tmux tmux.conf tmux.conf.local)
function md5() {
md5sum "$1" | awk '{print $1}'
}
function uppercase_first_letters() {
local result=()
for word in $*; do
declare -c uc_first_letter # undocument bash
uc_first_letter="$word"
result+=( "$uc_first_letter" )
done
echo "${result[@]}"
}
function populate_gitconfig {
if [[ -n "$PS1" ]]; then
local username="${username:-$(git config --global user.name)}"
while [[ -z "$username" ]]; do
read -e -p 'Your user name (ex Yves Blusseau): ' username
if [[ ! -z "$username" && "$username" != *\ * ]]; then
error "Invalid username ! Must be Firstname Lastname.\n"
username=''
fi
done
username="$(uppercase_first_letters "$username")"
git config --global user.name "$username"
email="${email:-$(git config --global user.email)}"
while [[ -z "$email" ]]; do
read -e -p 'Your email (ex [email protected]): ' email
if [[ ! -z "$email" && "$email" != *@* ]]; then
error "Invalid email !\n"
email=''
fi
done
git config --global user.email "$email"
fi
# Add the include
if [[ -z $(git config --global include.path) ]]; then
tmp_file=$(mktemp)
git config -f "$tmp_file" include.path .gitconfig.std
cat "$HOME/.gitconfig" >> $tmp_file
mv "$tmp_file" "$HOME/.gitconfig"
fi
}
# Main
source "$DOTFILES_DIR"/libraries/bash_library.sh
echo "======== SYNC REPOSITORIES ========"
if [[ -n "$(type -P git)" ]]; then
# Do an upgrade first
git pull --rebase --ff-only || exit $?
# Get git version
git_version=$(git --version | sed 's/^.*version *//')
submodule_recursive_option=''
version_test_gt "$git_version" "1.8.5"
[[ $? -eq 0 ]] && submodule_recursive_option='--recursive'
cd "$DOTFILES_DIR" && \
git submodule sync "$submodule_recursive_option" && \
git submodule update --init "$submodule_recursive_option"
# Populate .gitconfig file
populate_gitconfig
else
if [[ "$self" == upgrade ]]; then
printf "%s\n" "$(sayColor yellow 'Git must be installed to upgrade')" >&2
fi
fi
echo
echo "======== CREATE LINKS ========"
YES=0
for link in ${LINKS[@]}; do
source="$HOME/.$link"
source_txt=$(echo "$source" | sed "s@$HOME@~@")
dest="$DOTFILES_DIR/$link"
if [[ -e "$source" ]]; then
# Check if identical
if [[ ( -L "$source" && $(readlink "$source") == "$dest" ) || \
( ! -d "$source" && $(md5 "$source") == $(md5 "$dest") ) ]]; then
printf "%s $source_txt\n" "$(sayColor green identical)"
continue
fi
if [[ "$YES" -eq 0 ]]; then
msg=$(printf "%s $source_txt (ynaq)" "$(sayColor red overwrite)")
answer=$(prompt "$msg" "n" 'N' 'Y' 'Q' 'A')
case $(lc "$answer") in
n) continue ;;
q) exit 0 ;;
a) YES=1 ;;
esac
fi
# Overwrite
backup="${source}.bak"
backup_text="${source_txt}.bak"
# Remove old backup if exists
rm -rf "$backup"
printf "%s $source_txt to $backup_text\n" "$(sayColor yellow backup)"
mv "$source" "$backup"
fi
printf "%s $source_txt\n" "$(sayColor cyan linking)"
ln -sf "$dest" "$source"
done
distribution_id=$(lsb_release -is 2>/dev/null || true)
if [[ -n "$distribution_id" && -d "$DOTFILES_DIR/init/$distribution_id" ]]; then
echo
echo "======== CREATEING DEFAULT FILES for $distribution_id ========"
shopt -s nullglob
for f in "$DOTFILES_DIR/init/$distribution_id"/{*,.??*}; do
file=${f##*/}
dest="$HOME/$file"
if [[ ! -f "$dest" ]]; then
cp -v "$f" "$dest"
else
echo "Warn: file '$dest' already exists. Not overwitting with '$f' file."
fi
done
fi
# Local Variables: #
# mode: ksh #
# tab-width: 4 #
# indent-tabs-mode: nil #
# End: #
#
# vi: set expandtab ts=4 sw=4 sts=4: #