-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.sh
executable file
·116 lines (103 loc) · 3.87 KB
/
build.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
#!/bin/env bash
# Check if the user is root (or at least has the necessary permissions)
# This doesn't affect the execution, it just checks other alternatives to install the files to
# Check this comment about permissions (and the whole question by extension)
# https://stackoverflow.com/questions/18215973/how-to-check-if-running-as-root-in-a-bash-script#comment125628861_52586842
# The test can also be "faked" by fakeroot, so let the user run the script
# If the user is not root, append 'sudo' to the commands that require it.
# The following locations are to be used:
# $HOME/.csq/include/csq
# /usr/local/include/csq
# /opt/csq
# /usr/include/csq
# ~/.local/bin
# ~/.local/lib/csq
# ~/.local/share/csq
# Start from the least permission requirement location
is_root() {
[ "${EUID:-$(id -u)}" -eq 0 ]
}
can_write() {
[ -w "$1" ]
}
function install() {
# Make the csq.py executable before doing anything
chmod +x csq.py
if is_root || can_write "/opt"; then
echo "I'll install csq in /opt/csq and create a symlink to /usr/local/bin/csq"
mkdir -p /opt/csq
cp -r Core Compiler "/opt/csq"
cp csq.py "/opt/csq"
ln -sf "/opt/csq/csq.py" "/usr/local/bin/csq"
else
# Check for other locations to install
if can_write "$HOME"; then
echo "I'll install csq in $HOME/.csq/include/csq and create a symlink to ~/.local/bin/csq"
mkdir -p "$HOME/.csq/include/csq"
cp -r Core Compiler "$HOME/.csq/include/csq"
cp csq.py "$HOME/.csq/include/csq"
ln -sf "$HOME/.csq/include/csq/csq.py" "$HOME/.local/bin/csq"
elif can_write "/usr/local/include"; then
echo "I'll install csq in /usr/local/include/csq and create a symlink to /usr/local/bin/csq"
mkdir -p /usr/local/include/csq
cp -r Core Compiler "/usr/local/include/csq"
cp csq.py "/usr/local/include/csq"
ln -sf "/usr/local/include/csq/csq.py" "/usr/local/bin/csq"
elif can_write "/usr/include"; then
echo "I'll install csq in /usr/include/csq and create a symlink to /usr/bin/csq"
mkdir -p /usr/include/csq
cp -r Core Compiler "/usr/include/csq"
cp csq.py "/usr/include/csq"
ln -sf "/usr/include/csq/csq.py" "/usr/bin/csq"
elif can_write "$HOME/.local/bin"; then
echo "I'll install csq in ~/.local/bin and create a symlink to ~/.local/bin/csq"
cp csq.py "$HOME/.local/bin/csq"
elif can_write "$HOME/.local/lib"; then
echo "I'll install csq in ~/.local/lib/csq"
mkdir -p "$HOME/.local/lib/csq"
cp -r Core Compiler "$HOME/.local/lib/csq"
cp csq.py "$HOME/.local/lib/csq"
elif can_write "$HOME/.local/share"; then
echo "I'll install csq in ~/.local/share/csq"
mkdir -p "$HOME/.local/share/csq"
cp -r Core Compiler "$HOME/.local/share/csq"
cp csq.py "$HOME/.local/share/csq"
else
echo "No writable location found. Please install manually"
fi
fi
}
function uninstall() {
# Look for the places to uninstall
dirs=(
"$HOME/.csq/include/csq"
"/usr/local/include/csq"
"/opt/csq"
"/usr/include/csq"
"$HOME/.local/share/csq"
"$HOME/.local/lib/csq"
)
bins=(
"/usr/local/bin/csq"
"$HOME/.local/bin/csq"
)
for dir in "${dirs[@]}"; do
if [ -d "$dir" ]; then
rm -rf "$dir"
echo "Uninstalled from $dir"
fi
done
for bin in "${bins[@]}"; do
if [ -L "$bin" ]; then
unlink "$bin"
echo "Unlink binary from $bin"
fi
done
}
if [ "$1" == "install" ] || [ "$1" == "-i" ]; then
install
elif [ "$1" == "uninstall" ] || [ "$1" == "-u" ]; then
uninstall
else
echo "Usage: build.sh <install|uninstall>"
fi