-
Notifications
You must be signed in to change notification settings - Fork 34
/
makefile.sh
executable file
·195 lines (175 loc) · 4.82 KB
/
makefile.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
SHARE_DIR="/usr/local/share/twgit"
BIN_DIR="/usr/local/bin"
ROOT_DIR=$(pwd)
CONF_DIR="${ROOT_DIR}/conf"
INSTALL_DIR="${ROOT_DIR}/install"
USER_NAME=$(logname)
USER_HOME=$(sudo -u ${USER_NAME} -H sh -c 'echo "$HOME"')
CURRENT_SHELL=$(if [ ! -z $ZSH_NAME ]; then echo 'zsh'; else echo 'bash'; fi)
CURRENT_SHELL_CMD=${SHELL}
CURRENT_USER=${USER}
CURRENT_BRANCH=$(git branch --no-color | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
CURRENT_OS=$(uname -s)
if [ "${CURRENT_SHELL}" = "bash" ]; then
if [ "${CURRENT_OS}" = "Darwin" ] || [ "${CURRENT_OS}" = "FreeBSD" ]; then
BASH_RC="${USER_HOME}/.profile"
else
BASH_RC="${USER_HOME}/.${CURRENT_SHELL}rc"
fi
else
BASH_RC="${USER_HOME}/.${CURRENT_SHELL}rc"
fi
#
# Main method
#
main() {
case $1 in
install)
check_stable
check_root
install
;;
uninstall)
check_root
uninstall
;;
help)
help
;;
*)
echo "Uknown method '$1'"
;;
esac
}
#
# Check if current git branch is stable
#
check_stable() {
if [ "${CURRENT_BRANCH}" != "stable" ]; then
echo "You must be on 'stable' branch, not on '${CURRENT_BRANCH}' branch! Try: git checkout --track -b stable origin/stable"
exit 2
fi
}
#
# Check if current user is root
#
check_root() {
if [ "${CURRENT_USER}" != "root" ]; then
echo "Sorry, you are not root."
exit 1
fi
}
#
# Display help
#
help() {
echo "Usage:"
echo " sudo make install: to install Twgit in ${BIN_DIR}, ${CURRENT_SHELL} completion, config file and colored git prompt"
echo " sudo make uninstall: to uninstall Twgit from ${BIN_DIR} and ${CURRENT_SHELL} completion"
echo " make doc: to generate screenshots of help on command prompt"
}
#
# Uninstall twgit
#
uninstall() {
echo ""
echo "1 - Remove executable"
cmd="rm -f ${BIN_DIR}/twgit 2> /dev/null"
echo '$> '$cmd
eval $cmd
opt=""
for line in $(grep -in twgit ${BASH_RC} | cut -d: -f1); do
opt="${opt} -e ${line}d "
done
if [ ! -z "${opt}" ]; then
echo ""
echo "2 - Remove completion"
cmd="sed -i ${opt} ${BASH_RC}"
echo '$> '$cmd
eval $cmd
fi
return 0
}
#
# Install twgit
#
install () {
install_executable
install_completion
install_config
install_prompt
}
#
# Install executable
#
install_executable () {
echo ""
echo "1 - Install executable"
echo "Check for previous install in '${BIN_DIR}/twgit'"
if [ -f ${BIN_DIR}/twgit ]; then
echo "Previous install found : clean"
rm -f ${BIN_DIR}/twgit
fi
echo "Make twgit executable"
ln -s ${ROOT_DIR}/twgit ${BIN_DIR}/twgit
return 0
}
#
# Install completion
#
install_completion () {
echo ""
echo "2 - Install completion"
echo "Do you want to install the completion ? [Y/n]"
read answer
if [ "${answer}" != "Y" ] && [ "${answer}" != "y" ]; then
echo "(i) Skip"
return 0
fi
if [ $(cat ${BASH_RC} | grep -E "/${CURRENT_SHELL}_completion.sh" | grep -vE '^#' | wc -l) -gt 0 ]; then
echo "Twgit Bash completion already loaded by '${BASH_RC}'."
else
echo "Add line '. ${INSTALL_DIR}/${CURRENT_SHELL}_completion.sh' at the bottom of the script '${BASH_RC}'."
echo "" >> ${BASH_RC}
echo "# Added by Twgit makefile:" >> ${BASH_RC}
echo ". ${INSTALL_DIR}/${CURRENT_SHELL}_completion.sh" >> ${BASH_RC}
echo "(i) Restart ${CURRENT_SHELL} session to enable configuration."
fi
}
#
# Install config
#
install_config () {
echo ""
echo "3 - Check Twgit config file:"
if [ -f "${CONF_DIR}/twgit.sh" ]; then
echo "Config file '${CONF_DIR}/twgit.sh' already existing."
else
echo "Copy config file from '${CONF_DIR}/twgit-dist.sh' to '${CONF_DIR}/twgit.sh'"
sudo cp -p -n "${CONF_DIR}/twgit-dist.sh" "${CONF_DIR}/twgit.sh"
fi
}
#
# Install git prompt
#
install_prompt () {
echo ""
echo "4 - Install colored git prompt:"
if [ $(cat ${BASH_RC} | grep -E "\.bash_twgit" | grep -vE '^#' | wc -l) -gt 0 ]; then
echo "Colored Git prompt already loaded by '${BASH_RC}'."
else
echo "Add colored Git prompt to '${BASH_RC}' ? [Y/n] "
read answer
if [ "${answer}" = "Y" ] || [ "${answer}" = "y" ]; then
echo "Install git prompt: ${USER_HOME}/.bash_twgit"
sudo install -m 0644 -o ${SUDO_UID} -g ${SUDO_GID} "${INSTALL_DIR}/bash_git.sh" "${USER_HOME}/.bash_twgit"
echo "Add line '. ~/.bash_twgit' at the end of the script '${BASH_RC}'."
echo "" >> ${BASH_RC}
echo "# Added by Twgit makefile:" >> ${BASH_RC}
echo ". ~/.bash_twgit" >> ${BASH_RC}
fi
fi
}
# Run
main "$@"