-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·228 lines (176 loc) · 5.75 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
# help menu for build.sh
__help() {
echo "Usage: build [-c] [--check-only]";
echo -e "\n-c compress superuser binary using upx utility";
echo -e "\n--check-only perform checks on source code";
exit 0;
};
# check each source file for errors
__check() {
# current working directory
local current_dir="$(pwd)";
# command line arguments to be passed to nim
local arg1="--forceBuild:on --checks:on --stdout:off --hints:off ";
local arg2="--colors:on --assertions:on --filenames:abs ";
local arg3="--warnings:on --styleCheck:error --run compile ./src/Main.nim";
local arg4="${arg1}${arg2}${arg3}";
if [ ! -d "$current_dir/src/" ]; then
echo -e "\n\e[1;31mNo directory named\e[0m\e[1;37m'src'\e[0m";
return 1;
else
echo -e "\n\e[1;32mChecking...\e[0m";
fi;
if ( nim $arg4 ); then
if ( cd ./src/ && rm Main ); then
echo -e "\e[1A\e[2K\e[1;32mCHECK PASSED\e[0m\e[1;37m!\e[0m";
return 0;
else
echo -e "\e[1A\e[2K\e[1;31mCHECK FAILED\e[0m\e[1;37m!\e[0m";
fi;
fi;
return 1;
};
# build the superuser binary file
__build() {
# current working directory
local current_dir="$(pwd)";
# arguments to be passed to nim
local arg1="--gcc.exe:musl-gcc --linker.exe:musl-gcc --passL:-static";
local arg2="--threads:on --opt:speed -d:Release --hints:off --checks:off";
local arg3="--warnings:off --styleCheck:off --stdout:off";
local arg4="--assertions:off --forceBuild:on --out:superuser compile ./src/Main.nim";
local arg5="$arg1 $arg2 $arg3 $arg4";
if [ ! -d "./bin" ]; then
( mkdir "./bin" );
fi
if [ -f "./bin/superuser" ]; then
echo -e "\n\e[1;33m$current_dir/superuser already exists.\e[0m \e[1;37mRemoving...\e[0m";
sleep 2;
if ( rm -r "./bin/superuser" 2>/dev/null ); then
echo -e "\e[1A\e[2K\e[1;37mRemoved $current_dir/bin/superuser\e[0m";
sleep 2;
echo -e "\e[1A\e[2K\e[1;32mBuilding...\e[0m";
fi;
else
echo -e "\n\e[1;32mBuilding...\e[0m";
fi;
if ( nim $arg5 ); then
echo -e "\e[1A\e[2K\e[1;32mBUILD SUCCESSFUL\e[0m\e[1;37m!\e[0m";
if ( mv ./superuser ./bin/superuser ); then
echo -e "\e[1;37mMoved $current_dir/superuser -> $current_dir/bin/superuser\e[0m";
return 0;
else
echo -e "\n\e[1;31mNo file named\e[0m '\e[1;37msuperuser'\e[0m";
fi;
else
echo -e "\e[1A\e[2K\e[1;31mBUILD FAILED\e[0m\e[1;37m!\e[0m";
fi;
return 1;
};
# compress the superuser binary file
__compress() {
# current working directory
local current_dir="$(pwd)";
if [ -f "/usr/bin/upx" ]; then
echo -e "\n\e[1;32mCompressing...\e[0m";
sleep 1;
if ( cd "./bin/" && upx -q --best --ultra-brute "$current_dir/bin/superuser" &>/dev/null ); then
echo -e "\e[1A\e[2K\e[1;32mCOMPRESSION SUCCESSFUL\e[0m\e[1;37m!\e[0m";
return 0;
else
echo -e "\e[1A\e[2K\e[1;31mCOMPRESSION FAILED\e[0m\e[1;37m!\e[0m";
sleep 1;
fi;
fi;
return 1;
}
# install the superuser binary into specified location
__install() {
# current working directory
local current_dir="$(pwd)";
# superuser binary file (default) destination
local local_install=0;
# superuser binary file (fallback) destination
local global_install=0;
if [ -d "$HOME/.local/bin" ]; then
local_install=1;
fi;
if [ "$local_install" == 1 ] && [ -f "$HOME/.local/bin/superuser" ]; then
echo -e "\n\e[1;33m$HOME/.local/bin/superuser already exists.\e[0m \e[1;37mRemoving...\e[0m";
sleep 2;
if ( rm -r "$HOME/.local/bin/superuser" ); then
echo -e "\e[1A\e[2K\e[1;37mRemoved $HOME/.local/bin/superuser\e[0m";
sleep 2;
echo -e "\e[1A\e[2K\e[1;32mInstalling...\e[0m";
sleep 2;
fi;
elif [ -f "/usr/local/bin/superuser" ]; then
echo -e "\n\e[1;33m/usr/local/bin/superuser already exists.\e[0m \e[1;37mRemoving...\e[0m";
sleep 2;
if ( sudo rm -r "/usr/local/bin/superuser" ); then
echo -e "\e[1A\e[2K\e[1;37mRemoved /usr/local/bin/superuser\e[0m";
sleep 2;
echo -e "\e[1A\e[2K\e[1;32mInstalling...\e[0m";
sleep 2;
fi;
else
echo -e "\n\e[1;32mInstalling...\e[0m";
sleep 2;
fi;
if [ "$local_install" == 1 ]; then
if ( cd "./bin/" && cp -r "./superuser" "$HOME/.local/bin/superuser" ); then
echo -e "\e[1A\e[2K\e[1;32mINSTALL SUCCESSFUL\e[0m\e[1;37m!\e[0m";
echo -e "\e[1;37mCopied $current_dir/superuser -> $HOME/.local/bin/superuser\e[0m";
return 0;
elif ( cd "./bin/" && sudo cp -r "./superuser" "/usr/local/bin/superuser" ); then
echo -e "\e[1A\e[2K\e[1;32mINSTALL SUCCESSFUL\e[0m\e[1;37m!\e[0m";
echo -e "\n\e[1;37mCopied $current_dir/superuser -> /usr/local/bin/superuser\e[0m";
return 0;
else
echo -e "\e[1A\e[2K\e[1;31mINSTALL FAILED\e[0m\e[1;37m!\e[0m";
fi;
fi;
return 1;
};
__main() {
local arg="$1";
local system="$(uname -s)";
local arch="$(uname -m)";
local hasnim=0;
local hasmusl=0;
if [ "$system" == "Linux" ] && [ "$arch" == "x86_64" ]; then
if [ -f "${HOME}/.nimble/bin/nim" ]; then
hasnim=1;
else
echo -e "\e[1;31m'nim' must be installed\e[0m";
return 1;
fi;
if [ -f "/usr/bin/musl-gcc" ]; then
hasmusl=1;
else
echo -e "\e[1;31m'musl-gcc' must be installed\e[0m";
return 1;
fi;
if [ $# -eq 0 ]; then
__check && __build && __install;
elif [ $# -eq 1 ]; then
if [ "$arg" == "-h" ]; then
__help;
elif [ "$arg" == "--check-only" ]; then
__check;
elif [ "$arg" == "-c" ]; then
__check && __build && __compress && __install;
else
__help;
fi;
else
__help;
fi;
else
echo -e "\n\e[1;33mWARNING\e[0m\e[1;37m: Platform not supported\e[0m";
exit 1;
fi;
exit 0;
};
__main $@;