-
Notifications
You must be signed in to change notification settings - Fork 27
/
install.sh
executable file
·118 lines (101 loc) · 3.38 KB
/
install.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
#!/usr/bin/env bash
clear
echo " _ _ __ _ _ "
echo " | | | | / _(_) | "
echo " __| | ___ | |_| |_ _| | ___ ___ "
echo " / _\` |/ _ \| __| _| | |/ _ \/ __| "
echo " | (_| | (_) | |_| | | | | __/\__ \ "
echo "(_)__,_|\___/ \__|_| |_|_|\___||___/ "
echo "______________________________________________"
echo
echo "Installing dotfiles into your home directory"
echo
backup_dir=".backup_old_dot_files"
user_info_dir="$PWD/user.info"
user_info="$user_info_dir/user.info.yml"
for name in *; do
target="$HOME/.$name"
# ignore *.md and *.sh files
if [[ ${name: -3} != ".sh" && ${name: -3} != ".md" && $name != 'gitconfig' && $name != 'npmrc' ]]; then
# check if file already exists
if [ -e "$target" ]; then
# check if file is a symlink.
# if it is symlink we just delete it
# if it's a real file we back it up
if [ ! -L "$target" ]; then
# create backup dir if it's not there
if [ ! -d "$HOME/$backup_dir" ]; then
mkdir -p "$HOME/$backup_dir"
fi
echo "Backing up .$name in $HOME/$backup_dir/ directory"
cp "$target" "$HOME/$backup_dir/.$name$(date +"%d-%m-%Y-%H:%M:%S")"
fi
rm -rf "$target"
fi
echo "Creating $target"
ln -s "$PWD/$name" "$target"
fi
done
get_user_info () {
read -p "Your Git Name: " name
read -p "Your Git Email: " email
read -p "Your Github Username: " github_username
read -p "Your NPM Name: " npm_name
read -p "Your NPM Email: " npm_email
read -p "Your NPM Username: " npm_username
cat >"$user_info" <<EOL
name: ${name}
email: ${email}
github_username: ${github_username}
npm_name: ${npm_name}
npm_email: ${npm_email}
npm_username: ${npm_username}
EOL
echo "Stored your user info in $user_info"
}
generate_gitconfig () {
name=$(grep -E '^name:' < "$user_info")
name="${name/name: /''}"
email=$(grep -E '^email:' < "$user_info")
email="${email/email: /''}"
github_username=$(grep -E '^github_username:' < "$user_info")
github_username="${github_username/github_username: /''}"
cat gitconfig | \
sed 's/${name}/'"$name"'/' | \
sed 's/${email}/'"$email"'/' | \
sed 's/${github_username}/'"$github_username"'/' > \
"$HOME/.gitconfig"
}
generate_npmrc () {
name=$(grep -E '^npm_name:' < "$user_info")
name="${name/npm_name: /''}"
email=$(grep -E '^npm_email:' < "$user_info")
email="${email/npm_email: /''}"
github_username=$(grep -E '^github_username:' < "$user_info")
github_username="${github_username/github_username: /''}"
npm_username=$(grep -E '^npm_username:' < "$user_info")
npm_username="${npm_username/npm_username: /''}"
cat npmrc | \
sed 's/${name}/'"$name"'/' | \
sed 's/${email}/'"$email"'/' | \
sed 's/${github_username}/'"$github_username"'/' | \
sed 's/${npm_username}/'"$npm_username"'/' > \
"$HOME/.npmrc"
}
backup_file () {
if [ ! -d "$HOME/$backup_dir" ]; then
mkdir -p "$HOME/$backup_dir"
fi
timestamp=$(date +"%Y-%m-%d-%H-%M-%S")
echo "Backing up old .$1 at '$HOME/$backup_dir/.$1-$timestamp'"
cp "$HOME/.$1" "$HOME/$backup_dir/.$1-$timestamp"
}
if [ ! -f "$user_info" ]; then get_user_info; fi
echo "Creating .gitconfig"
if [ -f "$HOME/.gitconfig" ]; then backup_file gitconfig; fi
rm -rf "$HOME/.gitconfig"
generate_gitconfig
echo "Creating .npmrc"
if [ -f "$HOME/.npmrc" ]; then backup_file npmrc; fi
rm -rf "$HOME/.npmrc"
generate_npmrc