-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.sh
executable file
·99 lines (88 loc) · 2.17 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
#!/bin/sh
# Bertrand B.
BASHRC=".bashrc";
BASHRC_D="bashrc.d";
HOME_BASHRC="$HOME/$BASHRC";
HOME_BASHRC_D="$HOME/.$BASHRC_D";
usage () {
echo "Usage: $(basename "$0") [OPTIONS]";
echo "Options:";
echo " -s, --symlink: Install as symbolic link.";
echo " -c, --copy: Install as copy.";
echo " -u, --update: Update files in $HOME_BASHRC_D.";
echo " -r, --restore: Rollback to previous $BASHRC.";
echo " -h, --help: Display usage."
}
symlink_files () {
echo "Create symbolic links of $BASHRC_D into $HOME_BASHRC_D.";
ln -s "$BASHRC_D" "$HOME_BASHRC_D";
}
copy_files () {
echo "Copying files from $BASHRC_D into $HOME_BASHRC_D.";
cp -rv "$BASHRC_D" "$HOME";
}
delete_files (){
if [[ -d "$HOME_BASHRC_D" ]]; then
echo "Deleting files from $HOME_BASHRC_D.";
rm -vrf "$HOME_BASHRC_D";
fi
}
backup_current_bashrc () {
if [[ -f "$HOME_BASHRC" ]]; then
echo "Saving previous $BASHRC.";
cp -v "$HOME_BASHRC" "$HOME_BASHRC.save_$(date +"%Y%m%d%H%M%S")";
else
echo "No $BASHRC found in $HOME. Creating new one.";
cp -v "$BASHRC" "$HOME_BASHRC";
fi
}
update_current_bashrc () {
echo "Updating current $BASHRC.";
{ echo "" ; echo "# Custom part." ; echo ". $HOME_BASHRC_D/init.sh"; } >> "$HOME_BASHRC";
}
restore_previous_bashrc () {
LAST=$(find "$HOME" -maxdepth 1 -name ${BASHRC}.save* -print0 | xargs -0 ls -1 -t | head -1);
if [[ -f "$LAST" ]]; then
echo "Most recent save is: $LAST.";
cp -vf "$LAST" "$HOME_BASHRC";
echo "Current $HOME_BASHRC replaced by $LAST.";
else
echo "There is no previous .bashrc.";
fi
}
finalize () {
echo "You may need to reload your current terminal.";
}
run () {
case "$1" in
-s | --symbolic-link)
backup_current_bashrc;
symlink_files;
update_current_bashrc;
finalize;
;;
-c | --copy)
backup_current_bashrc;
copy_files;
update_current_bashrc;
finalize;
;;
-u | --update)
delete_files;
copy_files;
finalize;
;;
-r | --restore)
delete_files;
restore_previous_bashrc;
finalize;
;;
-h | --help)
usage;
;;
*)
usage;
;;
esac
}
run "$*";