-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·403 lines (390 loc) · 12.6 KB
/
run.sh
File metadata and controls
executable file
·403 lines (390 loc) · 12.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/bash
loglevel=0
# get data dir
file="$(which "$0")"
while :
do
if [[ -L "$file" ]]
then [[ $loglevel -ge 2 ]] && echo "Symlink! '$file'"
file="$(readlink "$file")"
elif [[ -d "$file" ]]
then echo "error while finding data directory"; exit 1
elif [[ -f "$file" ]]
then [[ "$loglevel" -ge 2 ]] && echo "found orginal! '$file'"
break
fi
done
data="$(dirname "$file")"
cd "$data" || exit
data="$(pwd)"
[[ $loglevel -ge 2 ]] && echo "data found in '$data'"
[[ -v HOME ]] || HOME="$data/../../"
# set $COLUMNS if unset
if ! [[ "$COLUMNS" > /dev/null ]]
then COLUMNS="$(tput cols)"
fi
# ___ __ _
# \__\ \___ \ \__\ \/
# \ \ \___ \___ \ 0
# function.
function help {
echo "usage: $0 -[arguments] command query"
if [ "$COLUMNS" -ge 70 ]
then
echo "
,-------------------------------------------------------------------,
| commands : discription |
|-------------------------------------------------------------------|
| -h --help : show this help |
| -R --refresh : refresh the cache |
| -g --get : download and install a program |
| -r --remove : remove a program |"
echo "
'-------------------------------------------------------------------'
"
else
echo "
command : description
-h --help : show this help
"
fi
[[ "$1" -ne 0 ]] && exit "$1"
}
# test if there is any argument
# [ "$@" == '' ] && {
# help 1
# }
function unreachable {
info="no further info"
exit="255"
[[ "$1" == '' ]] && info="$1"
[[ "$2" == '' ]] && exit="$2"
echo -e "\e[1;31;40mreached unreachable code:\n\e[0m${FUNCNAME[*]}\ninfo provided by function: $info\n"
[[ $exit -ge 0 ]] && exit "$exit"
}
function info {
_INFO_ECHO="$1"
if [[ ${#_INFO_ECHO} -ge $COLUMNS ]]
then echo "${_INFO_ECHO:0:$((COLUMNS-3))}..."
else echo "${_INFO_ECHO:0:$COLUMNS}"
fi
}
# .vpmscript handeling
# shellcheck disable=SC2034,SC2162
function script {
line=1
vpmscript_uid="$(date +%s_%N)"
mkdir -p "$data/bash/"
mkfifo "$data/bash/$vpmscript_uid"
{ tail -f "$data/bash/$vpmscript_uid" | bash & } 2>/dev/null
eval "procid_$vpmscript_uid='$!'"
while read cmd a1 a2 a3 a4 a5 a6 arg
do
case "$cmd" in
R | run ) echo "eval \"$a1\" \"$a2\" \"$a3\" \"$a4\" \"$a5\" \"$a6\" \"$arg\"" >> "$data/bash/$vpmscript_uid";;
r | rm ) echo "rm -rf \"$a1\"" >> "$data/bash/$vpmscript_uid";;
t | mf ) echo "echo -n '' >> \"$a1\"" >> "$data/bash/$vpmscript_uid";;
d | md ) echo "mkdir \"$a1\"" >> "$data/bash/$vpmscript_uid";;
l | lk ) echo "ln -s \"$a1\" \"$a2\"" >> "$data/bash/$vpmscript_uid";;
c | cp ) echo "cp -r \"$a1\" \"$a2\"" >> "$data/bash/$vpmscript_uid";;
m | mv ) echo "cp -r \"$a1\" \"$a2\" && rm -rf \"$a1\"" >> "$data/bash/$vpmscript_uid";;
e | ex ) echo "exit $a1" >> "$data/bash/$vpmscript_uid";;
s | scr ) script "$a1";;
* ) echo "error: '$1'@'$line': '$cmd' not found."
esac
((line++))
done < "$1"
echo "exit" >> "$data/bash/$vpmscript_uid"
eval "wait \$procid_$vpmscript_uid"
rm -f "$data/bash/$vpmscript_uid"
}
# http[s] downloader
if command -v axel >/dev/null
then
function dl {
axel -o "$2" -- "$1"
}
elif command -v curl >/dev/null
then
function dl {
echo "downloading \"$1\""
curl -# -o "$2" -- "$1"
}
elif command -v wget -nv --show-progress >/dev/null
then
function dl {
wget -O "$2" -- "$1"
}
else
echo -e "-------------\nno supported downloader found. make sure to have one of the following installed and in the PATH:\n"\
"axel: faster downloads\n"\
"curl: clean output (using -# argument)\n"\
"wget: great alternative\n"\
"-------------"
exit 1
fi
#
# pager
if command -v less
then PAGER="$(which less) -E" && export PAGER
fi
#
# set functions
function resolvefile {
[[ $loglevel -ge 2 ]] && echo "resolving $1"
linenr=1
for var in NAME TYPE FORMAT URL ID INFO README
do eval "$var=''"
done
[[ "$1" == '' ]] && return
# shellcheck disable=SC2034,SC2162
while read name eq value
do
# [[ "$eq" == "=" ]] && {
# [[ "$name" == "/#" ]] || {
# echo -e "error on line $linenr:\nexpected '$name = $value' but got '$name $eq $value'."
# return 1
# }
# }
case $name in
/# ) [[ "$loglevel" -ge 2 ]] && echo "$linenr: comment!" ;;
name ) NAME="$value" ;;
type ) TYPE="$value" ;;
format ) FORMAT="$value" ;;
url ) URL="$value" ;;
id ) ID="$value" ;;
info ) INFO="$value" ;;
readme ) README="$value" ;;
install ) :;;
protected ) PROTECTED="$value" ;;
'' ) : ;;
* ) echo -e "error on line $linenr:\n$line\n'$name' not found. see the github for more info on writing these files."; return 1 ;;
esac
((linenr++))
done < "$1"
}
function listfile {
resolvefile "$1" && \
entry="name: $NAME | desciption: $INFO | type: $TYPE"
echo "${entry:0:$COLUMNS}"
unset entry
}
function searchfile {
sresult=''
result=''
results=''
nresults=''
cd "$data/repo" || return 1
[[ "$loglevel" -ge 2 ]] && pwd
for repo in *
do
cd "$repo" || return 1
[[ "$loglevel" -ge 2 ]] && pwd
for file in *.vpmfile
do [[ "$file" == *"$1"*.vpmfile ]] && {
# results="${results}repo/$repo/$file "
results+=("repo/$repo/$file")
}
done
cd ..
done
cd ..
[[ "$2" == s ]] && {
resultcount=0
for result in "${results[@]}"
do resolvefile "$result"
[[ "$NAME" == "$1" ]] && nresults+=("$result") && ((resultcount++))
done
if [[ $resultcount -gt 1 ]]
then
echo "there are more than 1 results! which one do you want?"
cnt=1
for result in "${results[@]}"
do resolvefile "$result"
echo "$cnt: $NAME" ; ((cnt++))
done
# shellcheck disable=SC2162
read -p ' > ' in
cnt=1
for result in "${nresults[@]}"
do [[ $cnt -eq $in ]] && sresult="$result"
done
else sresult="${nresults[1]}"; [[ $loglevel -ge 2 ]] && echo "only one result found."
fi
}
}
function install {
skip=1
searchfile "$1" s
cd "$data" || return 1
[[ "$sresult" == '' ]] && echo "no results found. run $0 -R to refresh the cache, and then try again."
resolvefile "$sresult"
cd "$data" || return 1
[[ "$TYPE" == program ]] || {
echo "OOPS! file is not a program."
return
}
download "$URL"
[[ -f install.vpmscript ]] && script install.vpmscript
# shellcheck disable=SC2164
cd "$data"
cp "$sresult" "$HOME/.vosjedev/$ID/info.vpmfile"
echo "done."
}
function download {
case $FORMAT in
git ) cd "$HOME/.vosjedev/" && git -p clone "$URL" && cd "$ID" && info "download done." ;;
* ) echo "error: unkown format."
esac
}
function remove {
cd "$HOME/.vosjedev" || return 1
for program in *
do
resolvefile "$program/info.vpmfile"
[[ "$PROTECTED" == true ]] && echo "can't remove protected program '$NAME'."
if [[ "$NAME" == "$1" ]]
then read -rp "are you sure you want to remove program $NAME with id $ID? [Y/n] " in
case $in in
y | yes )
[[ -f "remove.vpmscript" ]] && script remove.vmpscript
rm -rf "$program"
;;
n | no ) return 0;;
esac
fi
done
skip=1
}
function search {
skip=1
searchfile "$1"
[[ "${#results[@]}" -eq 0 ]] && {
echo "no results found for search $1."
return 1
}
for result in "${results[@]}"
do listfile "$result"
done
}
function list {
case $1 in
cache )
cd "$data/repo" || return 1
for repo in *
do
cd "$repo"|| continue
for program in *.vpmfile
do listfile "$program"
done
cd ..
done
cd "$data" || return
;;
installed )
cd "$HOME/.vosjedev" || return 1
for dir in *
do listfile "$dir/info.vpmfile"
done
;;
esac
skip=1
}
function refresh {
rm -rf repo/*
for repo in "$data/repolist/"*".vpmfile"
do
info "'$repo' found!"
resolvefile "$repo"
code=$?
[[ $code == 1 ]] && echo "error! resolvefile exited with code $code" && continue
info "TYPE='$TYPE' NAME='$NAME' FORMAT='$FORMAT' URL='$URL'"
[[ "$TYPE" == repo ]] || {
info "OOPS! file is not a repository! skipping repo '$NAME' ..."
continue
}
cd repo || return 1
[[ -d "$ID" ]] && {
info "repo $NAME already here. deleting it's cache..."
rm -rf "$ID"
}
mkdir "$ID"
# shellcheck disable=SC2164
cd "$ID"
case $FORMAT in
http/zip )
dl "$URL" repofiles.zip
if command -v sha256sum >/dev/null
then
dl "$URL.checksum" checksum.txt >/dev/null 2>&1
read -r CHECKSUM < "checksum.txt"
if [[ "$CHECKSUM" == "$(sha256sum "repofiles.zip")" ]]
then info "checksum: $CHECKSUM matched"
else echo -e "checksum $CHECKSUM failed to match.\ndo you want to continue?"
read -rsn1 -p "[y for yes, anything else for no]" i
case $i in
y ) echo '';;
* ) cd ../.. ;continue
esac
fi
else echo "no sha256sum binary found in PATH."
fi
unzip -q repofiles.zip
echo "done!"
;;
git )
cd ..
rm -rf "$ID"
git -p clone "$URL"
cd "$ID" || { unreachable "if you see this, contact the owner of repo $NAME with id $ID because the id of the repo does not match the git destination."; cd ..; continue; }
esac
cd ../..
done
}
function update-all {
cd "$HOME/.vosjedev" || { echo "you should download and install vpm using the commands in the README."; exit 255;}
for program in *
do
[[ ! -d "$program" ]] && continue
cd "$program" || unreachable "error while cding to \"$HOME/.vosjedev/$program\". if the program is uninstalled, please run vpm again."
resolvefile info.vpmfile
echo "updating:"
listfile "info.vpmfile"
case $FORMAT in
git ) git -p pull --no-rebase
esac
cd "$HOME/.vosjedev" || { echo "you should download and install vpm using the commands in the README."; exit 255;}
done
}
PS2='+ ${FUNCNAME[*]}: '
[[ -v debug ]] && set -x
skip=0
# parse arguments
until [[ $# -le 0 ]]
do
[[ "$skip" == 1 ]] && {
shift
skip=0
continue
}
if [[ "$2" == "-"* ]] || [[ "$2" == '' ]]
then echo "[=======| $1 |=======]"
else echo "[======| $1 $2 |======]"
fi
case $1 in
-g | --get ) install "$2" ;;
-r | --remove ) remove "$2" ;;
-l | --list ) list "$2" ;;
-h | --help ) help 0 ;;
-R | --refresh ) refresh ;;
-U | --update-all ) update-all ;;
-s | --search ) search "$2" ;;
"-"* ) echo "option $1 not found... run '$0 -h' to get help."
[[ "$2" == -"*" ]] || skip=1
;;
* ) help 0
esac
shift
cd "$data" || exit 1
done
[[ -v debug ]] && set +x