forked from dagon666/napi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
napi.sh
executable file
·2769 lines (2201 loc) · 70.7 KB
/
napi.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# force indendation settings
# vim: ts=4 shiftwidth=4 expandtab
########################################################################
########################################################################
########################################################################
# Copyright (C) 2014 Tomasz Wisniewski aka
# DAGON <[email protected]>
#
# http:/dagon666
# http://pcarduino.blogspot.co.ul
#
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
########################################################################
########################################################################
########################################################################
# verify presence of the napi_common library
declare -r NAPI_COMMON_PATH=
if [ -z "$NAPI_COMMON_PATH" ] || [ ! -e "${NAPI_COMMON_PATH}/napi_common.sh" ]; then
echo
echo "napi.sh i subotage.sh nie zostaly poprawnie zainstalowane"
echo "uzyj skryptu install.sh (install.sh --help - pomoc)"
echo "aby zainstalowac napi.sh w wybranym katalogu"
echo
exit -1
fi
# source the common routines
. "${NAPI_COMMON_PATH}/"napi_common.sh
################################################################################
#
# @brief abbreviation
# - string added between the filename and the extension
#
# @brief conversion abbreviation
# - string added between the filename and the extension
# only for the converted subtitles
#
declare -a g_abbrev=( "" "" )
#
# @brief prefix for the original file - before the conversion
#
declare g_orig_prefix='ORIG_'
#
# 0 - system - detected system type
# - linux
# - darwin - mac osx
#
# 1 - numer of forks
#
# 2 - id
# - pynapi - identifies itself as pynapi
# - other - identifies itself as other
# - NapiProjektPython - uses new napiprojekt3 API - NapiProjektPython
# - NapiProjekt - uses new napiprojekt3 API - NapiProjekt
#
declare -a g_system=( 'linux' '1' 'NapiProjektPython' )
#
# @brief minimum size of files to be processed
#
declare g_min_size=0
#
# @brief whether to download cover or not
#
declare g_cover=0
#
# @brief whether to download nfo or not
#
declare g_nfo=0
#
# @brief whether to skip downloading if file is already present
#
declare g_skip=0
#
# @brief whether to delete the original file after conversion
#
declare g_delete_orig=0
#
# @brief defines the charset of the resulting file
#
declare g_charset='default'
#
# @brief default subtitles language
#
declare g_lang='PL'
#
# @brief default subtitles extension
#
declare g_default_ext='txt'
#
# @brief subtitles format
#
declare g_sub_format='default'
#
# @brief preferred fps detection tool
#
declare g_fps_tool='default'
#
# @brief external script
#
declare g_hook='none'
#
# @brief napiprojekt.pl user credentials
# 0 - user
# 1 - password
#
declare -a g_cred=( '' '' )
#
# global paths list
#
declare -a g_paths=()
#
# global files list
#
declare -a g_files=()
#
# @brief prepare all the possible filename combinations
#
declare -a g_pf=()
#
# @brief processing stats
# 0 - downloaded
# 1 - unavailable
# 2 - skipped
# 3 - converted
# 4 - covers downloaded
# 5 - covers unavailable
# 6 - covers skipped
# 7 - nfos downloaded
# 8 - nfos unavailable
# 9 - nfos skipped
# 10 - total processed
declare -a g_stats=( 0 0 0 0 0 0 0 0 0 0 0 )
#
# controls whether to print statistics on exit or not
#
g_stats_print=0
#
#
#
#
g_clean_xml=1
################################### TOOLS ######################################
#
# @brief global tools array
# =1 - mandatory tool
# =0 - optional tool
#
declare -a g_tools=( 'tr=1' 'printf=1' 'mktemp=1' 'wget=1' \
'wc=1' 'dd=1' 'grep=1' 'seq=1' 'sed=1' \
'cut=1' 'base64=0' 'unlink=0' 'stat=1' \
'basename=1' 'dirname=1' 'cat=1' 'cp=1' \
'mv=1' 'awk=0' 'file=0' 'subotage.sh=0' \
'7z=0' '7za=0' '7zr=0' 'iconv=0' 'mediainfo=0' \
'mplayer=0' 'mplayer2=0' 'ffmpeg=0' 'ffprobe=0' )
# fps detectors
declare -a g_tools_fps=( 'ffmpeg' 'ffprobe' 'mediainfo' 'mplayer' 'mplayer2' )
#
# @brief wget details
# 0 - cmd
# 1 - flag defining if wget supports post requests
g_cmd_wget=( 'wget -q -O' '0' )
g_cmd_stat='stat -c%s'
g_cmd_base64_decode="base64 -d"
g_cmd_md5='md5sum'
g_cmd_cp='cp'
g_cmd_unlink='unlink'
g_cmd_7z=''
################################ languages #####################################
# language code arrays
declare -ar g_Language=( 'Albański' 'Angielski' 'Arabski' 'Bułgarski' \
'Chiński' 'Chorwacki' 'Czeski' 'Duński' \
'Estoński' 'Fiński' 'Francuski' 'Galicyjski' \
'Grecki' 'Hebrajski' 'Hiszpanski' 'Holenderski' \
'Indonezyjski' 'Japoński' 'Koreański' 'Macedoński' \
'Niemiecki' 'Norweski' 'Oksytański' 'Perski' \
'Polski' 'Portugalski' 'Portugalski' 'Rosyjski' \
'Rumuński' 'Serbski' 'Słoweński' 'Szwedzki' \
'Słowacki' 'Turecki' 'Wietnamski' 'Węgierski' 'Włoski' )
declare -ar g_LanguageCodes2L=( 'SQ' 'EN' 'AR' 'BG' 'ZH' 'HR' 'CS' 'DA' 'ET' 'FI' \
'FR' 'GL' 'EL' 'HE' 'ES' 'NL' 'ID' 'JA' 'KO' 'MK' \
'DE' 'NO' 'OC' 'FA' 'PL' 'PT' 'PB' 'RU' 'RO' 'SR' \
'SL' 'SV' 'SK' 'TR' 'VI' 'HU' 'IT' )
declare -ar g_LanguageCodes3L=( 'ALB' 'ENG' 'ARA' 'BUL' 'CHI' 'HRV' 'CZE' \
'DAN' 'EST' 'FIN' 'FRE' 'GLG' 'ELL' 'HEB' \
'SPA' 'DUT' 'IND' 'JPN' 'KOR' 'MAC' 'GER' \
'NOR' 'OCI' 'PER' 'POL' 'POR' 'POB' 'RUS' \
'RUM' 'SCC' 'SLV' 'SWE' 'SLO' 'TUR' 'VIE' 'HUN' 'ITA' )
#
# @brief: list all the supported languages and their respective 2/3 letter codes
#
list_languages() {
local i=0
while [ "$i" -lt "${#g_Language[@]}" ]; do
echo "${g_LanguageCodes2L[$i]}/${g_LanguageCodes3L[$i]} - ${g_Language[$i]}"
i=$(( i + 1 ))
done
}
#
# @brief verify that the given language code is supported
#
verify_language() {
local lang="${1:-}"
local i=0
declare -a l_arr=( )
[ ${#lang} -ne 2 ] && [ ${#lang} -ne 3 ] && return $RET_PARAM
local l_arr_name="g_LanguageCodes${#lang}L";
eval l_arr=\( \${${l_arr_name}[@]} \)
# this function can cope with that kind of input
# shellcheck disable=SC2068
i=$(lookup_key "$lang" ${l_arr[@]})
local found=$?
echo "$i"
[ "$found" -eq $RET_OK ] && return $RET_OK
return $RET_FAIL
}
#
# @brief set the language variable
# @param: language index
#
normalize_language() {
local i=${1:-0}
i=$(( i + 0 ))
local lang=${g_LanguageCodes2L[$1]}
# don't ask me why
[ "$lang" = "EN" ] && lang="ENG"
echo $lang
return $RET_OK
}
#################################### ENV #######################################
cleanup_tmp_files() {
_debug $LINENO "usuwam pliki tymczasowe (jesli istnieja)"
$g_cmd_unlink napisy.7z.*
$g_cmd_unlink napi.*
$g_cmd_unlink ipc.*
$g_cmd_unlink stats.*
}
trap_control_c() {
_msg $LINENO "przechwycono CTRL+C, koncze wykonywanie...";
cleanup_tmp_files
exit $?
}
#
# @brief configure external commands
#
configure_cmds() {
local k=''
_debug $LINENO "konfiguruje stat i md5"
# verify stat & md5 tool
if [ "${g_system[0]}" = "darwin" ]; then
g_cmd_md5="md5"
g_cmd_stat="stat -f%z"
g_cmd_base64_decode="base64 -D"
else
g_cmd_md5="md5sum"
g_cmd_stat="stat -c%s"
g_cmd_base64_decode="base64 -d"
fi
# g_tools+=( "$g_cmd_md5=1" )
g_tools=( "${g_tools[@]}" "$g_cmd_md5=1" )
_debug $LINENO "sprawdzam czy wget wspiera opcje -S"
local s_test=$(wget --help 2>&1 | grep "\-S")
[ -n "$s_test" ] &&
g_cmd_wget[0]='wget -q -S -O' &&
_info $LINENO "wget wspiera opcje -S"
_debug $LINENO "sprawdzam czy wget wspiera zadania POST"
local p_test=$(wget --help 2>&1 | grep "\-\-post\-")
g_cmd_wget[1]=0
[ -n "$p_test" ] &&
g_cmd_wget[1]=1 &&
_info $LINENO "wget wspiera zadania POST"
# check unlink command
_debug $LINENO "sprawdzam obecnosc unlink"
# this function can cope with that kind of input
# shellcheck disable=SC2068
[ "$(lookup_value 'unlink' ${g_tools[@]})" = "0" ] &&
_info $LINENO 'brak unlink, g_cmd_unlink = rm' &&
g_cmd_unlink='rm -rf'
return $RET_OK
}
#
# @brief verify system settings and gather info about commands
#
verify_system() {
local cores=1
_debug $LINENO "weryfikuje system"
# detect the system first
g_system[0]="$(get_system)"
# establish the number of cores
cores=$(get_cores ${g_system[0]})
# sanity checks
[ "${#cores}" -eq 0 ] && cores=1
[ "$cores" -eq 0 ] && cores=1
# two threads on one core should be safe enough
g_system[1]=$(( cores * 2 ))
}
#
# @brief perform tools presence verification
#
verify_tools() {
declare -a ret=()
local rv=$RET_OK
local tool=''
local p=0
local m=0
local t=''
for t in "$@"; do
p=1
tool=$(get_key "$t")
m=$(get_value "$t")
! verify_tool_presence "$tool" && p=0
# ret+=( "$tool=$p" )
ret=( "${ret[@]}" "$tool=$p" )
# break if mandatory tool is missing
[ "$m" -eq 1 ] && [ "$p" -eq 0 ] && rv=$RET_FAIL && break
done
echo ${ret[*]}
return $rv
}
#
# @brief get extension for given subtitle format
#
get_sub_ext() {
local status=0
declare -a fmte=( 'subrip=srt' 'subviewer2=sub' )
# this function can cope with that kind of input
# shellcheck disable=SC2068
lookup_value "$1" ${fmte[@]}
status=$?
[ "$status" -ne $RET_OK ] && echo $g_default_ext
return $RET_OK
}
##################################### fps ######################################
#
# @brief returns the number of available fps detection tools in the system
#
count_fps_detectors() {
local c=0
local t=""
local v=''
for t in "${g_tools_fps[@]}"; do
# this function can cope with that kind of input
# shellcheck disable=SC2068
v=$(lookup_value "$t" ${g_tools[@]})
[ "$v" = "1" ] && c=$(( c + 1 ))
done
echo $c
return $RET_OK
}
#
# @brief detect fps of the video file
# @param tool
# @param filename
#
get_fps() {
local fps=0
local tbr=0
local tbr2=0
local t="${1:-default}"
local tmp=''
declare -a atmp=()
# don't bother if there's no tool available or not specified
if [ -z "$t" ] || [ "$t" = "default" ]; then
echo $fps
return $RET_PARAM
fi
# this function can cope with that kind of input
# shellcheck disable=SC2068
local tool=$(lookup_value "$1" ${g_tools[@]})
# prevent empty output
tool=$(( tool + 0 ))
if [ "$tool" -ne 0 ]; then
case "$1" in
'mplayer' | 'mplayer2' )
fps=$($1 -identify -vo null -ao null -frames 0 "$2" 2> /dev/null | grep ID_VIDEO_FPS | cut -d '=' -f 2)
;;
'mediainfo' )
fps=$($1 --Output='Video;%FrameRate%' "$2")
;;
'ffmpeg' )
tmp=$($1 -i "$2" 2>&1 | grep "Video:")
tbr=$(echo "$tmp" | sed 's/, /\n/g' | tr -d ')(' | grep tbr | cut -d ' ' -f 1)
fps=$(echo "$tmp" | sed 's/, /\n/g' | grep fps | cut -d ' ' -f 1)
[ -z "$fps" ] && fps="$tbr"
;;
'ffprobe' )
tmp=$(ffprobe -v 0 -select_streams v -print_format csv -show_entries stream=avg_frame_rate,r_frame_rate -- "$2" | tr ',' ' ')
atmp=( $tmp )
local i=0
for i in 1 2; do
local a=$(echo "${atmp[$i]}" | cut -d '/' -f 1)
local b=$(echo "${atmp[$i]}" | cut -d '/' -f 2)
[ "${atmp[$i]}" != "0/0" ] && fps=$(float_div "$a" "$b")
done
;;
*)
;;
esac
fi
# just a precaution
echo "$fps" | cut -d ' ' -f 1
return $RET_OK
}
#################################### ARGV ######################################
#
# @brief parse the cli arguments
#
parse_argv() {
_debug $LINENO "parsowanie argumentow"
# command line arguments parsing
while [ $# -gt 0 ]; do
unset varname
# when adding a new option, please remember to maintain the same order as generated by usage()
case "$1" in
# abbrev
"-a" | "--abbrev") varname="g_abbrev[0]"
msg="nie określono wstawki"
;;
"-b" | "--bigger-than") varname="g_min_size"
msg="nie okreslono minimalnego rozmiaru"
;;
# cover download
"-c" | "--cover" ) g_cover=1 ;;
# nfo download
"-n" | "--nfo" ) g_nfo=1 ;;
# orig prefix
"-d" | "--delete-orig") g_delete_orig=1 ;;
# skip flag
"-s" | "--skip") g_skip=1 ;;
# log overwrite option
"-lo" | "--log-overwrite") g_output[$___LOG_OWR]=1 ;;
# don't delete the xml (debugging purposes)
"-px" | "--preserve-xml") g_clean_xml=0 ;;
# stats flag
"--stats") g_stats_print=1 ;;
# move instead of copy
"-M" | "--move") g_cmd_cp='mv' ;;
# charset conversion
"-C" | "--charset") varname="g_charset"
msg="nie podano docelowego kodowania"
;;
# extension
"-e" | "--ext") varname="g_default_ext"
msg="nie okreslono domyslnego rozszerzenia dla pobranych plikow"
;;
# identification
"-I" | "--id") varname="g_system[2]"
msg="okresl typ narzedzia jako pynapi/other (legacy API) albo NapiProjekt/NapiProjektPython (API-3)"
;;
# logfile
"-l" | "--log") varname="g_output[$___LOG]"
msg="nie podano nazwy pliku loga"
;;
# languages
"-L" | "--language") varname="g_lang"
msg="wybierz jeden z dostepnych 2/3 literowych kodow jezykowych (-L list - zeby wyswietlic)"
;;
# password
"-p" | "--pass") varname="g_cred[1]"
msg="nie podano hasla"
;;
# external script
"-S" | "--script") varname="g_hook"
msg="nie okreslono sciezki do skryptu"
;;
# user login
"-u" | "--user") varname="g_cred[0]"
msg="nie podano nazwy uzytkownika"
;;
# verbosity
"-v" | "--verbosity") varname="g_output[$___VERBOSITY]"
msg="okresl poziom gadatliwosci (0 - najcichszy, 3 - najbardziej gadatliwy, 4 - insane)"
;;
# destination format definition
"-f" | "--format") varname="g_sub_format"
msg="nie określono formatu docelowego"
;;
"-P" | "--pref-fps") varname="g_fps_tool"
msg="nie określono narzedzia do detekcji fps"
;;
# orig prefix
"-o" | "--orig-prefix") varname="g_orig_prefix"
msg="nie określono domyslnego prefixu"
;;
# abbrev
"--conv-abbrev") varname="g_abbrev[1]"
msg="nie określono wstawki dla konwersji"
;;
"-F" | "--forks") varname="g_system[1]"
msg="nie określono ilosci watkow"
;;
# parameter is not a known argument, probably a filename
*)
# g_paths+=( "$1" )
g_paths=( "${g_paths[@]}" "$1" )
;;
esac
# set the global var for simple switches
# not requiring any further verification
if [ -n "$varname" ]; then
shift
[ -z "$1" ] && _error "$msg" && return $RET_FAIL
eval "${varname}=\$1"
fi
shift
done
return $RET_OK
}
#
# @brief validate username and password
#
verify_credentials() {
local user="${1:-}"
local passwd="${2:-}"
local rv=$RET_OK
if [ -z "$user" ] && [ -n "$passwd" ]; then
_warning "podano haslo, brak loginu. tryb anonimowy."
rv=$RET_PARAM
fi
if [ -n "$user" ] && [ -z "$passwd" ]; then
_warning "podano login, brak hasla. tryb anonimowy."
rv=$RET_PARAM
fi
return $rv
}
#
# @brief checks if the given encoding is supported
#
verify_encoding() {
[ "$1" = 'default' ] && return $RET_OK
echo test | iconv -t "$1" > /dev/null 2>&1
return $?
}
#
# @brief checks id
#
verify_id() {
local rv=$RET_OK
case ${g_system[2]} in
'pynapi' | 'other' | 'NapiProjektPython' | 'NapiProjekt' ) ;;
*) # any other - revert to napi projekt 'classic'
rv=$RET_PARAM
g_system[2]='pynapi'
;;
esac
# 7z check
if [ "${g_system[2]}" = 'other' ] ||
[ "${g_system[2]}" = 'NapiProjektPython' ] ||
[ "${g_system[2]}" = 'NapiProjekt' ]; then
if [ -z "$g_cmd_7z" ]; then
_error "7z nie jest dostepny. zmieniam id na 'pynapi'. PRZYWRACAM TRYB LEGACY"
g_system[2]='pynapi'
return $RET_UNAV
fi
fi
# check for necessary tools for napiprojekt3 API
if [ "${g_system[2]}" = 'NapiProjektPython' ] ||
[ "${g_system[2]}" = 'NapiProjekt' ]; then
declare -a t=( 'base64' 'awk' )
local p=''
local k=''
for k in "${t[@]}"; do
# this function can cope with that kind of input
# shellcheck disable=SC2068
p=$(lookup_value "$k" ${g_tools[@]})
p=$(( p + 0 ))
if [ "$p" -eq 0 ]; then
_error "$k nie jest dostepny. zmieniam id na 'pynapi'. PRZYWRACAM TRYB LEGACY"
g_system[2]='pynapi'
return $RET_UNAV
fi
done
fi
return $rv
}
#
# @brief format verification
#
verify_format() {
# format verification if conversion requested
if [ "$g_sub_format" != 'default' ]; then
# this function can cope with that kind of input
# shellcheck disable=SC2068
local sp=$(lookup_value 'subotage.sh' ${g_tools[@]})
# make sure it's a number
sp=$(( sp + 0 ))
if [ "$sp" -eq 0 ]; then
_error "subotage.sh nie jest dostepny. konwersja nie jest mozliwa"
return $RET_PARAM
fi
declare -a formats=()
formats=( $(subotage.sh -gf) )
# this function can cope with that kind of input
# shellcheck disable=SC2068
if ! lookup_key $g_sub_format ${formats[@]} > /dev/null; then
_error "podany format docelowy jest niepoprawny [$g_sub_format]"
return $RET_PARAM
fi
fi
return $RET_OK
}
#
# @brief verify fps tool
#
verify_fps_tool() {
local t=''
local sp=0
local n=0
# verify selected fps tool
if [ "$g_fps_tool" != 'default' ]; then
# this function can cope with that kind of input
# shellcheck disable=SC2068
if ! lookup_key "$g_fps_tool" ${g_tools_fps[@]} > /dev/null; then
_error "podane narzedzie jest niewspierane [$g_fps_tool]"
return $RET_PARAM
fi
# this function can cope with that kind of input
# shellcheck disable=SC2068
sp=$(lookup_value "$g_fps_tool" ${g_tools[@]})
# make sure it's a number
sp=$(( sp + 0 ))
if [ "$sp" -eq 0 ]; then
_error "$g_fps_tool nie jest dostepny"
return $RET_PARAM
fi
else
local v=''
# choose first available as the default tool
n=$(count_fps_detectors)
if [ "$n" -gt 0 ]; then
for t in "${g_tools_fps[@]}"; do
# this function can cope with that kind of input
# shellcheck disable=SC2068
v=$(lookup_value $t ${g_tools[@]})
[ "$v" -eq 1 ] &&
g_fps_tool=$t &&
break
done
fi
fi
return $RET_OK
}
#
# @brief verify presence of any of the 7z tools
#
verify_7z() {
local rv=$RET_OK
local lv=''
# check 7z command
_debug $LINENO "sprawdzam narzedzie 7z"
# use 7z or 7za only, 7zr doesn't support passwords
declare -a t7zs=( '7za' '7z' )
g_cmd_7z=''
for k in "${t7zs[@]}"; do
# this function can cope with that kind of input
# shellcheck disable=SC2068
lv=$(lookup_value "$k" ${g_tools[@]})
[ "$lv" = "1" ] &&
_info $LINENO "7z wykryty jako [$k]" &&
g_cmd_7z="$k" &&
break
done
[ -z "$g_cmd_7z" ] &&
rv=$RET_FAIL &&
_info $LINENO 'brak 7z/7za albo 7zr'
return $rv
}
#
# @brief verify correctness of the argv settings provided
#
verify_argv() {
local status=0
# make sure first that the printing functions will work
case "${g_output[$___VERBOSITY]}" in
0 | 1 | 2 | 3 )
;;
4 )
_debug_insane
;;
*)
_error "poziom gadatliwosci moze miec jedynie wartosci z zakresu (0-3)"
return $RET_BREAK
;;
esac
_debug $LINENO "weryfikacja argumentow"
# verify credentials correctness
_debug $LINENO 'sprawdzam dane uwierzytelniania'
if ! verify_credentials "${g_cred[0]}" "${g_cred[1]}"; then
g_cred[0]='' && g_cred[1]=''
fi
# make sure we have a number here
_debug $LINENO 'normalizacja parametrow numerycznych'
g_min_size=$(( g_min_size + 0 ))
g_output[$___VERBOSITY]=$(( g_output[$___VERBOSITY] + 0 ))
g_system[1]=$(( g_system[1] + 0 ))
# verify encoding request
_debug $LINENO 'sprawdzam wybrane kodowanie'
if ! verify_encoding "$g_charset"; then
_warning "charset [$g_charset] niewspierany, ignoruje zadanie"
g_charset='default'
fi
# check the 7z tool presence
verify_7z
# verify the id setting
_debug $LINENO 'sprawdzam id'
verify_id
status=$?
case $status in
$RET_OK )
_debug $LINENO "id zweryfikowane pomyslnie [${g_system[2]}]"
;;
$RET_PARAM )
_warning "nieznany id, przywrocono TRYB LEGACY (id = pynapi lub other)"
;;
$RET_UNAV )
_warning "nie wszystkie narzedzia sa dostepne. Zainstaluj brakujace narzedzia by korzystac z nowego API napiprojekt"
;;
*)
_error "nieznany blad, podczas weryfikacji id..."
return $RET_BREAK
;;
esac
# logfile verification
_debug $LINENO 'sprawdzam logfile'
if [ -e "${g_output[$___LOG]}" ] &&
[ "${g_output[$___LOG]}" != "none" ]; then
# whether to fail or not ?
if [ "${g_output[$___LOG_OWR]}" -eq 0 ]; then
_error "plik loga istnieje, podaj inna nazwe pliku aby nie stracic danych"
return $RET_BREAK
else
_warning "plik loga istnieje, zostanie nadpisany"
fi
fi
# language verification
_debug $LINENO 'sprawdzam wybrany jezyk'
local idx=0
idx=$(verify_language "$g_lang")
status=$?
if [ "$status" -ne $RET_OK ]; then
if [ "$g_lang" = "list" ]; then
list_languages
return $RET_BREAK
else
_warning "nieznany jezyk [$g_lang]. przywracam PL"
g_lang='PL'
fi
else
_debug $LINENO "jezyk znaleziony, index = $idx"
g_lang=$(normalize_language "$idx")
fi
unset idx
# format verification
_debug $LINENO 'sprawdzam format'
! verify_format && return $RET_PARAM
# fps tool verification
_debug $LINENO 'sprawdzam wybrane narzedzie fps'
! verify_fps_tool && return $RET_PARAM
# verify external script
_debug $LINENO 'sprawdzam zewnetrzny skrypt'
if [ "$g_hook" != 'none' ]; then
[ ! -x "$g_hook" ] &&
_error "podany skrypt jest niedostepny (lub nie ma uprawnien do wykonywania)" &&
return $RET_PARAM
fi
return $RET_OK
}
################################# napiprojekt ##################################
#
# @brief: mysterious f() function
# @param: md5sum
#
f() {
declare -a t_idx=( 0xe 0x3 0x6 0x8 0x2 )
declare -a t_mul=( 2 2 5 4 3 )
declare -a t_add=( 0 0xd 0x10 0xb 0x5 )
local sum="$1"
local b=""
local i=0
# for i in {0..4}; do
# again in order to be compliant with bash < 3.0
for i in $(seq 0 4); do
local a=${t_add[$i]}
local m=${t_mul[$i]}
local g=${t_idx[$i]}