This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyaa
executable file
·554 lines (495 loc) · 18.4 KB
/
nyaa
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/bin/sh
# nyaa - package manager written in POSIX sh
# Copyright 2022 Kreato
#
# This file is part of Kreato Linux.
#
# Kreato Linux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kreato Linux is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Kreato Linux. If not, see <https://www.gnu.org/licenses/>.
VERSION="v2.1c"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
CONF="/etc/nyaa.conf"
INSTALLED_DIR="/etc/nyaa.installed/"
MAIN_REPO_DIR="/etc/nyaa"
MAIN_BIN_REPO_DIR="/etc/nyaa-bin"
# Root checker
chkroot() {
if [ "$(id -u)" != "0" ]; then
err "${RED}nyaa${NC}: you have to be root"
fi
}
version() {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; # From https://stackoverflow.com/a/37939589
}
# Default values if config file does not exist
defvalues() {
export REPO_PATH="/etc/nyaa;/etc/nyaa-bin"
export DOWNLOADER="wget"
export MIRROR_PATH="https://mirror.kreato.dev"
export CC="gcc"
}
# Make required folders if they dont exist
if [ ! -d "$INSTALLED_DIR/nyaa" ]
then
echo "nyaa: new installation detected, making folders"
chkroot
mkdir -p $INSTALLED_DIR/nyaa
echo "/bin/nyaa" > $INSTALLED_DIR/nyaa/list_files
echo "$VERSION" > $INSTALLED_DIR/nyaa/version
echo "" > $INSTALLED_DIR/nyaa/deps
fi
defvalues
# shellcheck source=/dev/null
[ -f "$CONF" ] && . $CONF # Source the configuration file if it exists
down() {
if [ "$1" != "" ];
then
case $DOWNLOADER in
"aria2")
aria2c "$@" || err "${RED}nyaa${NC}: an error occured while trying to download"
;;
"axel")
axel "$@" || err "${RED}nyaa${NC}: an error occured while trying to download"
;;
"curl")
curl "$@" -O || err "${RED}nyaa${NC}: an error occured while trying to download"
;;
*)
wget "$@" || err "${RED}nyaa${NC}: an error occured while trying to download"
;;
esac
else
err "${RED}nyaa${NC}: an error occured while trying to download"
fi
}
err() {
if [ "$1" != "-n" ]; then
printf "%b\n" "$1"
if [ -f "/tmp/nyaa.lock" ]; then
printf "%bnyaa%b: removing lockfile\n" "${BLUE}" "${NC}"
rm -f /tmp/nyaa.lock
fi
else
printf "%b\n" "$2"
fi
exit 1
}
add_repo_dir() {
for i in $(echo $REPO_PATH | tr ";" "\n")
do
[ -d "$i/$1" ] && export REPO_DIR="$i"
done
[ -z "$REPO_DIR" ] && err "${RED}nyaa${NC}: package does not exist"
}
build_package() {
# Checking if the user is root
chkroot
# Exporting variables required to build the package
export ROOT=/tmp/nyaa_build
export SRCDIR=/tmp/nyaa_srcdir
export DESTDIR="$ROOT"
[ -f "/tmp/nyaa.lock" ] && err -n "${RED}nyaa${NC}: lockfile exists, will not proceed"
touch /tmp/nyaa.lock || err "${RED}nyaa${NC}: couldnt create lockfile, please check permissions"
trap "rm -f /tmp/nyaa.lock ; trap - EXIT;exit" EXIT INT HUP
# Removing the folders if they exist already
[ -d "$SRCDIR" ] && rm -rf $SRCDIR
[ -d "$ROOT" ] && rm -rf $ROOT
# Making the folders again
mkdir -p $ROOT
mkdir -p $SRCDIR
add_repo_dir "$1"
unset -f prepare # Unset prepare before sourcing
# Sourcing the package runfile
# shellcheck source=/dev/null
. "$REPO_DIR"/"$1"/run || err "${RED}nyaa${NC}: couldn't source runfile, please check if it exists"
# Checking if the package is a binary package or not
[ "$IS_BINARY" = "y" ] && err "${RED}nyaa${NC}: this package is a binary package, install through '${BLUE}nyaa ${GREEN}i $1${NC}'"
# Building the package and making it into a nice tarball
cd "$SRCDIR" || err "${RED}nyaa${NC}: $SRCDIR folder for some reason couldn't get created, please check permissions"
echo "$SOURCES" | tr ';' '\n' | while read -r line
do
echo "downloading from $line"
down "$line"
done || exit 1
echo "$SHA256SUM" | tr ';' '\n' | while read -r line
do
if [ "$line" = "SKIP" ]; then
if [ "$SHA256SUM_SKIPS" = "1" ]; then
echo "nyaa: warn: sha256sum skipped"
else
err "${RED}nyaa${NC}: sha256sum skips are disabled, cannot skip"
fi
else
echo "$line" | sha256sum -c || err "${RED}nyaa${NC}: sha256sum -c exited with $?"
fi
done || exit 1
if command -v prepare >/dev/null 2>&1
then
prepare || err "${RED}nyaa${NC}: prepare failed"
else
echo "$SOURCES" | tr ';' '\n' | while read -r line
do
bsdtar -xvf "$(basename "$line")"
done
fi
build || err "${RED}nyaa${NC}: build failed"
cd "$ROOT" || err "${RED}nyaa${NC}: $ROOT folder for some reason couldn't get created, please check permissions"
[ ! -d "/etc/nyaa.tarballs" ] && mkdir -p /etc/nyaa.tarballs
tar -czvf /etc/nyaa.tarballs/nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz ./*
cd /etc/nyaa.tarballs || err "${RED}nyaa${NC}: an error occured (check permissions)"
sha256sum nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz > nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz.sum
unset ROOT
# Installing packaged tarball
if [ ! "$DONT_INSTALL" = "1" ]; then
# shellcheck disable=SC2194
case "-r" in
"$3")
export ROOT="$4"
ROOT="$4" install_package "$1" /etc/nyaa.tarballs/nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz
;;
"$2")
export ROOT="$3"
ROOT="$3" install_package "$1" "/etc/nyaa.tarballs/nyaa-tarball-$1-$VERSION-$RELEASE.tar.gz"
;;
*)
export ROOT="/"
ROOT="/" install_package "$1" /etc/nyaa.tarballs/nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz
;;
esac
fi
rm -f /tmp/nyaa.lock || err "${RED}nyaa${NC}: couldn't remove lockfile, please check permissions"
}
install_package() {
# Install tarball
add_repo_dir "$1"
if [ ! "$ROOT" = "" ]; then
cd /etc/nyaa.tarballs || err "${RED}nyaa${NC}: unknown error (check permissions)"
sha256sum -c "$2.sum" || err "${RED}nyaa${NC}: checksum failed"
cd "$ROOT" || err "${RED}nyaa${NC}: unknown error"
tar -hxvf "$2" > /tmp/"$1".log
[ ! -d "$ROOT/$INSTALLED_DIR/$1" ] && mkdir -p "$ROOT/$INSTALLED_DIR/$1"
cp -rf "$REPO_DIR/$1/"* "$ROOT/$INSTALLED_DIR/$1"
mv /tmp/"$1".log "$ROOT/$INSTALLED_DIR/$1/list_files"
else
tar -xvf "$2" -C /
[ ! -d "$INSTALLED_DIR/$1" ] && mkdir -p "$INSTALLED_DIR/$1"
cp -rf "$REPO_DIR/$1/"* "$INSTALLED_DIR/$1"
mv /tmp/"$1".log "$INSTALLED_DIR/$1/list_files"
fi
#shellcheck source=/dev/null
. "$ROOT/$INSTALLED_DIR/$1/run"
if command -v post_install >/dev/null 2>&1
then
post_install || err "${RED}nyaa${NC}: post installation failed"
fi
}
# Install binaries from the repository
# A feature long-waited, shipped with nyaa v1.4
install_binaries() {
chkroot
add_repo_dir "$1-bin"
#shellcheck source=/dev/null
. "$REPO_DIR"/"$1-bin"/run
[ ! -d "/etc/nyaa.tarballs" ] && mkdir -p /etc/nyaa.tarballs
TARBALL="nyaa-tarball-$1-$VERSION-$RELEASE.tar.gz"
CHKSUM="$TARBALL.sum"
cd /etc/nyaa.tarballs || err "${RED}nyaa${NC}: an error occured"
for i in $(echo $MIRROR_PATH | tr ";" "\n")
do
if [ ! -f "/etc/nyaa.tarballs/$CHKSUM" ]; then
down "$i"/"$CHKSUM" || err "${RED}nyaa${NC}: chksum download failed with error"
else
break
fi
if [ ! -f "/etc/nyaa.tarballs/$TARBALL" ]; then
down "$i"/"$TARBALL" || err "${RED}nyaa${NC}: tarball download failed with error"
else
break
fi
done
# shellcheck disable=SC2194
case "-r" in
"$3")
export ROOT="$4"
ROOT="$4" install_package "$1" /etc/nyaa.tarballs/nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz
;;
"$2")
export ROOT="$3"
ROOT="$3" install_package "$1" "/etc/nyaa.tarballs/nyaa-tarball-$1-$VERSION-$RELEASE.tar.gz"
;;
*)
export ROOT="/"
ROOT="/" install_package "$1" /etc/nyaa.tarballs/nyaa-tarball-"$1"-"$VERSION"-"$RELEASE".tar.gz
;;
esac
}
# Removing packages
# Basically removes everything in the list_files file of the package.
remove_packages() {
chkroot
packages="$(echo "$@" | sed '1s/^.//')"
for i in $packages
do
[ ! -f "$ROOT/$INSTALLED_DIR/$i/list_files" ] && err "${RED}nyaa${NC}: package $i is not installed"
unset -f custom_uninstall # Unset custom_uninstall before sourcing
unset -f post_uninstall # unset post_uninstall before sourcing
#shellcheck source=/dev/null
. "$INSTALLED_DIR/$i/run"
if ! command -v custom_uninstall >/dev/null 2>&1
then
while read -r file; do
rm -f "${ROOT:?}/${file:?}" 2>/dev/null
if [ -z "$(ls -A "${ROOT:?}/$file" 2>/dev/null)" ]
then
rm -rf "${ROOT:?}/${file:?}" 2>/dev/null
fi
done < "${ROOT:?}/$INSTALLED_DIR/$i/list_files"
rm -rf "${ROOT:?}/${INSTALLED_DIR:?}/${i:?}/"
if command -v post_uninstall >/dev/null 2>&1
then
post_uninstall || err "${RED}nyaa${NC}: post uninstall script failed"
fi
else
custom_uninstall || err "${RED}nyaa${NC}: custom uninstall script failed"
fi
done || exit 1
}
# Update repositories
# A basic git pull and a check to see if the repositories actually exist.
update_packages() {
chkroot
[ ! -d "$MAIN_REPO_DIR" ] && git clone https://github.com/kreatolinux/nyaa-repo $MAIN_REPO_DIR
[ ! -d "$MAIN_BIN_REPO_DIR" ] && git clone https://github.com/kreatolinux/nyaa-repo-bin $MAIN_BIN_REPO_DIR
for i in $(echo $REPO_PATH | tr ";" "\n")
do
cd "$i" || err "${RED}nyaa${NC}: couldn't cd onto $i, exiting"
git pull
done
}
# Package upgrader
# Basically a simple for loop that checks if local package version equals to the repositories' package version.
upgrade_packages() {
cd "$INSTALLED_DIR" || err "${RED}nyaa${NC}: couldn't find nyaa installed directory"
for w in *
do
# shellcheck source=/dev/null
. "$INSTALLED_DIR/$w"/run
LOCAL_VERSION=$VERSION
LOCAL_RELEASE=$RELEASE
LOCAL_EPOCH=$EPOCH
add_repo_dir "$w"
#shellcheck source=/dev/null
. "$REPO_DIR/$w"/run
REPO_VERSION=$VERSION
REPO_RELEASE=$RELEASE
REPO_EPOCH=$EPOCH
if [ "$(version "$LOCAL_EPOCH")" -lt "$(version "$REPO_EPOCH")" ] || [ "$(version "$LOCAL_VERSION")" -lt "$(version "$REPO_VERSION")" ] || [ "$(version "$LOCAL_RELEASE")" -lt "$(version "$REPO_RELEASE")" ]
then
printf "%bnyaa%b: upgrading %s" "${BLUE}" "${NC}" "$w"
if [ -z "$REPO_EPOCH" ]; then
printf "%bnyaa%b: new version of %s: %s-%s" "${BLUE}" "${NC}" "$w" "$REPO_VERSION" "$REPO_RELEASE"
else
printf "%bnyaa%b: new version of %s: %s-%s-%s" "${BLUE}" "${NC}" "$w" "$REPO_VERSION" "$REPO_RELEASE" "$REPO_EPOCH"
fi
if [ "$1" = "-b" ]; then
build_package "$w"
else
install_binaries "$w"
fi
fi
done
}
# Get information about the package from the runfile
get_info() {
add_repo_dir "$1"
[ ! -f "$REPO_DIR/$1/run" ] && echo "nyaa: package does not exist" && exit 1
#shellcheck source=/dev/null
. "$REPO_DIR/$1"/run
[ -d "$INSTALLED_DIR/$1" ] && ISINSTALLED=yes
[ ! -d "$INSTALLED_DIR/$1" ] && ISINSTALLED=no
printf "package %bname%b: %s\n" "${BLUE}" "${NC}" "$NAME"
printf "package %bdesc%b: %s\n" "${BLUE}" "${NC}" "$DESCRIPTION"
printf "package %bver%b: %s\n" "${BLUE}" "${NC}" "$VERSION"
printf "package %brel%b: %s\n" "${BLUE}" "${NC}" "$RELEASE"
[ -n "$EPOCH" ] && printf "package %bepoch%b: %s" "${BLUE}" "${NC}" "$EPOCH"
printf "is %binstalled%b: %s\n" "${BLUE}" "${NC}" "$ISINSTALLED"
}
case $1 in
"h")
printf "nyaa %s
${BLUE}->${NC} h display help
${BLUE}->${NC} b build package
${BLUE}->${NC} i install binary packages
${BLUE}->${NC} I get info about a package
${BLUE}->${NC} r remove package
${BLUE}->${NC} u update packages
${BLUE}->${NC} U upgrade packages
${BLUE}->${NC} v display version
" "$VERSION"
;;
"b")
packages="$(echo "$@" | sed '1s/^.//;s/-y//;s/-o//;s/-r.*//;/^$/d;s/-n//')" # Get rid of flags and stuff like `b`, `-y`, `-r`, etc.
[ "$packages" = "" ] && err "${RED}nyaa${NC}: please enter a package name"
# -r flag is for setting the ROOTFS variable.
# This checks where the -r flag is (or isn't) and sets the variables accordingly
#shellcheck disable=SC2194
case "-r" in
"$3")
export ROOT="$4"
;;
"$4")
export ROOT="$5"
;;
"$5")
export ROOT="$6"
;;
*)
export ROOT="/"
;;
esac
for i in $packages
do
pkg="$i"
add_repo_dir "$i"
deps="$(depper --pkg="$pkg" --repo="$REPO_DIR")"
done
for i in $deps
do
packages="$(echo "$packages" | sed "s/$i//g")"
done
if [ "$3" != "-y" ] && [ "$4" != "-y" ] && [ "$3" != "-n" ] && [ "$4" != "-n" ]; then
printf "${BLUE}Packages${NC}:${GREEN}%s ${BLUE}%s${NC}\nDo you want to continue to the installation? (${BLUE}y${NC}/${RED}N${NC}) " "$deps" "$packages"
read -r yn
else
printf "${BLUE}Packages${NC}: ${GREEN}%s ${BLUE}%s${NC}\n" "$deps" "$2"
fi
# Need to refactor this code
# It just looks too long and has a lot of checks that i think can just be done much more efficiently.
if [ "$yn" = "y" ] || [ "$yn" = "Y" ] || [ "$3" = "-y" ] || [ "$4" = "-y" ] || [ "$3" = "-o" ] || [ "$4" = "-o" ]; then
if [ "$deps" != "" ]; then
for word in $deps; do
if [ "$3" = "-o" ] || [ "$4" = "-o" ]; then
[ ! -d "$ROOT/$INSTALLED_DIR/$word" ] && DONT_INSTALL=1 build_package "$word" -r "$(realpath $ROOT)"
else
[ ! -d "$ROOT/$INSTALLED_DIR/$word" ] && build_package "$word" -r "$(realpath $ROOT)"
fi
done
else
printf "%bnyaa%b: dependencies already installed, continuing" "${BLUE}" "${NC}"
fi
for i in $packages
do
build_package "$i" "$3" "$4" "$5" "$6" || err "${RED}nyaa${NC}: an error occured during compilation"
done
else
echo "nyaa: exiting."
exit 0
fi
;;
"i")
packages="$(echo "$@" | sed '1s/^.//;s/-y//;s/-o//;s/-r.*//;/^$/d;s/-n//')" # Get rid of flags and stuff like `b`, `-y`, `-r`, etc.
[ "$packages" = "" ] && err "${RED}nyaa${NC}: please enter a package name"
# -r flag is for setting the ROOTFS variable.
# This checks where the -r flag is (or isn't) and sets the variables accordingly
# shellcheck disable=SC2194
case "-r" in
"$3")
export ROOT="$4"
;;
"$4")
export ROOT="$5"
;;
"$5")
export ROOT="$6"
;;
*)
export ROOT="/"
;;
esac
for i in $packages
do
pkg="$i"
add_repo_dir "$i"
deps="$(depper --pkg="$pkg" --repo="$REPO_DIR")"
done
for i in $deps
do
packages="$(echo "$packages" | sed "s/$i//g")"
done
if [ "$3" != "-y" ] && [ "$4" != "-y" ]; then
printf "${BLUE}Packages${NC}:${GREEN}%s ${BLUE}%s${NC}\nDo you want to continue to the installation? (${BLUE}y${NC}/${RED}N${NC}) " "$deps" "$packages"
read -r yn
else
printf "${BLUE}Packages${NC}: ${GREEN}%s ${BLUE}%s${NC}\n" "$deps" "$2"
fi
if [ "$yn" = "y" ] || [ "$yn" = "Y" ] || [ "$3" = "-y" ] || [ "$4" = "-y" ]; then
if [ "$deps" != "" ]; then
for word in $deps; do
[ ! -d "$ROOT/$INSTALLED_DIR/$word" ] && install_binaries "$word" -r "$(realpath $ROOT)"
done
else
printf "%bnyaa%b: dependencies already installed, continuing" "${BLUE}" "${NC}"
fi
for i in $packages
do
install_binaries "$i" "$3" "$4" "$5" "$6" || err "${RED}nyaa${NC}: an error occured during installation"
done
else
echo "nyaa: exiting."
exit 0
fi
;;
"I")
get_info "$2"
;;
"r")
# -r flag is for setting the ROOTFS variable.
# This checks where the -r flag is (or isn't) and sets the variables accordingly
# shellcheck disable=SC2194
case "-r" in
"$3")
export ROOT="$4"
;;
"$4")
export ROOT="$5"
;;
"$5")
export ROOT="$6"
;;
*)
export ROOT="/"
;;
esac
remove_packages "$@"
;;
"u")
update_packages
;;
"U")
upgrade_packages "$2"
;;
"v")
printf "${BLUE}nyaa${NC} ${GREEN}%s${NC}
Copyright (C) 2022 Kreato Linux
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
" "$VERSION"
;;
*)
printf "run '%bnyaa %bh%b' for help\n" "${BLUE}" "${GREEN}" "${NC}"
;;
esac