-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.sh
120 lines (90 loc) · 2.51 KB
/
config.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
#!/bin/env bash
## Copyright (c) 2023 Onwuta Ebube Gideon, All Rights Reserved ##
## ##
## ##
## This software falls under the MIT LICENSE ##
## Please read the LICENSE file for more information ##
##
## --- Onwuta Ebube Gideon <[email protected]> --- ##
# # # # # # # # # # # # # # # # # # # #
# Make $NAME versatile in the system #
# # # # # # # # # # # # # # # # # # # #
# Name of executable
NAME=monty
# Directory for executable
EXEC_DIR=/usr/bin
SCRIPT=$0
CMD=$1
FLAG=$2
INSTALL_CMD="install"
UNINSTALL_CMD="uninstall"
FORCE_FLAG="force"
# Colors: to be used as ${COLOR_NAME}
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No color. Should end a colored string
# Functions
# Display to stderr
_display () {
echo "$@" >&2
}
install_file () {
FILE=$1
LOCATION=$2
if [ -e "$FILE" ]; then
sudo mv --update "$FILE" "$LOCATION"
_display "${GREEN}$FILE${NC} successfully installed in ${CYAN}$LOCATION${NC}";
else
_display "${RED}No executable $NAME program found in this directory${NC}"
_display "Run '${GREEN}make${NC}' before installing with '${GREEN}$SCRIPT $INSTALL_CMD${NC}'";
fi
}
uninstall_file () {
FILE=$1
LOCATION=$2
if [ -e "$LOCATION/$FILE" ]; then
sudo rm "$LOCATION/$FILE"
_display "${GREEN}$LOCATION/$FILE${NC} successfully uninstalled";
else
_display "No need to uninstall it. ${CYAN}$FILE${NC} doesn't exist in ${CYAN}$LOCATION${NC}";
fi
}
# Ensure binary file is not kept in this directory
clean_up () {
if [ -e "$NAME" ]; then
rm "$NAME"
fi
}
# Main execution
if [ "$CMD" = "$INSTALL_CMD" ]; then
# Ensure previous version is not overwritten without notice
if [ "$FLAG" = "$FORCE_FLAG" ]; then
install_file "$NAME" "$EXEC_DIR"
clean_up
exit
fi
if [ -e "$EXEC_DIR"/"$NAME" ]; then
{
_display "${GREEN}$NAME${NC} is already installed in ${GREEN}$EXEC_DIR${NC}.";
while true; do
read -r -p "Do you want to overwrite existing version of $NAME? (y/n) " yn
case $yn in
[yY] ) install_file "$NAME" "$EXEC_DIR"; break;;
[nN] ) _display "exiting"; break;;
*) _display "invalid response";;
esac
done;
}
clean_up
exit
else
install_file "$NAME" "$EXEC_DIR"
fi
elif [ "$CMD" = "$UNINSTALL_CMD" ]; then
uninstall_file "$NAME" "$EXEC_DIR"
else
_display "${RED}Inappropriate command${NC}"
_dispaly "You can run '${CYAN}$SCRIPT $INSTALL_CMD${NC}' or '${CYAN}$SCRIPT $UNINSTALL_CMD${NC}'";
fi
clean_up